spring boot 2.4.x 之前版本(对应spring-cloud-openfeign 3.0.0之前版本)feign请求异常逻辑

目录

feign

SynchronousMethodHandler

第一部分

第二部分

第三部分

spring-cloud-openfeign

LoadBalancerFeignClient

ribbon

AbstractLoadBalancerAwareClient


在之前写的文章配置基础上

https://blog.csdn.net/zlpzlpzyd/article/details/136060312

因为从 spring boot 2.4.x 版本开始,匹配的 spring cloud 版本中去除了负载均衡组件 ribbon,可以看本人写的如下链接

https://blog.csdn.net/zlpzlpzyd/article/details/135696320

如果一定要使用 ribbon 相关功能,需要单独指定版本引入,spring 官方为了 ribbon 被移除的问题,自己开发了 spring-cloud-starter-loadbalancer。

feign设置参数如下

feign:client:config:default:connectTimeout: 1000readTimeout: 1000

服务提供者逻辑

@RestController
@RequestMapping(value = "user")
public class UserController implements Serializable {@GetMapping(value = "{id}")public User getUser(@PathVariable(value = "id") String id) {// int no = 1/0;try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {throw new RuntimeException(e);}User user = new User();user.setId(id);user.setUserName("user_name_"+ id);user.setAge("20");return user;}}

可知,feign 调用此服务会出现超时问题。

异常堆栈调用如下

0 = {StackTraceElement@8542} "java.net.SocketInputStream.socketRead0(Native Method)"
1 = {StackTraceElement@8543} "java.net.SocketInputStream.socketRead(SocketInputStream.java:116)"
2 = {StackTraceElement@8544} "java.net.SocketInputStream.read(SocketInputStream.java:171)"
3 = {StackTraceElement@8545} "java.net.SocketInputStream.read(SocketInputStream.java:141)"
4 = {StackTraceElement@8546} "java.io.BufferedInputStream.fill(BufferedInputStream.java:246)"
5 = {StackTraceElement@8547} "java.io.BufferedInputStream.read1(BufferedInputStream.java:286)"
6 = {StackTraceElement@8548} "java.io.BufferedInputStream.read(BufferedInputStream.java:345)"
7 = {StackTraceElement@8549} "sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)"
8 = {StackTraceElement@8550} "sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)"
9 = {StackTraceElement@8551} "sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1593)"
10 = {StackTraceElement@8552} "sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)"
11 = {StackTraceElement@8553} "java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)"
12 = {StackTraceElement@8554} "feign.Client$Default.convertResponse(Client.java:113)"
13 = {StackTraceElement@8555} "feign.Client$Default.execute(Client.java:109)"
14 = {StackTraceElement@8556} "org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:98)"
15 = {StackTraceElement@8557} "org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:56)"
16 = {StackTraceElement@8558} "com.netflix.client.AbstractLoadBalancerAwareClient$1.call(AbstractLoadBalancerAwareClient.java:103)"
17 = {StackTraceElement@8559} "com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:303)"
18 = {StackTraceElement@8560} "com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:287)"
19 = {StackTraceElement@8561} "rx.internal.util.ScalarSynchronousObservable$3.call(ScalarSynchronousObservable.java:231)"
20 = {StackTraceElement@8562} "rx.internal.util.ScalarSynchronousObservable$3.call(ScalarSynchronousObservable.java:228)"
21 = {StackTraceElement@8563} "rx.Observable.unsafeSubscribe(Observable.java:10327)"
22 = {StackTraceElement@8564} "rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.drain(OnSubscribeConcatMap.java:286)"
23 = {StackTraceElement@8565} "rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.onNext(OnSubscribeConcatMap.java:144)"
24 = {StackTraceElement@8566} "com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:185)"
25 = {StackTraceElement@8567} "com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:180)"
26 = {StackTraceElement@8568} "rx.Observable.unsafeSubscribe(Observable.java:10327)"
27 = {StackTraceElement@8569} "rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:94)"
28 = {StackTraceElement@8570} "rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:42)"
29 = {StackTraceElement@8571} "rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)"
30 = {StackTraceElement@8572} "rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)"
31 = {StackTraceElement@8573} "rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)"
32 = {StackTraceElement@8574} "rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)"
33 = {StackTraceElement@8575} "rx.Observable.subscribe(Observable.java:10423)"
34 = {StackTraceElement@8576} "rx.Observable.subscribe(Observable.java:10390)"
35 = {StackTraceElement@8577} "rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:443)"
36 = {StackTraceElement@8578} "rx.observables.BlockingObservable.single(BlockingObservable.java:340)"
37 = {StackTraceElement@8579} "com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:110)"
38 = {StackTraceElement@8580} "org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:84)"
39 = {StackTraceElement@8581} "feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:125)"
40 = {StackTraceElement@8582} "feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:94)"
41 = {StackTraceElement@8583} "feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100)"
42 = {StackTraceElement@8584} "com.sun.proxy.$Proxy71.getUser(Unknown Source)"
43 = {StackTraceElement@8585} "com.example.feign.consumer.controller.FeignController.test(FeignController.java:22)"
44 = {StackTraceElement@8586} "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)"
45 = {StackTraceElement@8587} "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)"
46 = {StackTraceElement@8588} "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)"
47 = {StackTraceElement@8589} "java.lang.reflect.Method.invoke(Method.java:498)"
48 = {StackTraceElement@8590} "org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)"
49 = {StackTraceElement@8591} "org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)"
50 = {StackTraceElement@8592} "org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)"
51 = {StackTraceElement@8593} "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)"
52 = {StackTraceElement@8594} "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)"
53 = {StackTraceElement@8595} "org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)"
54 = {StackTraceElement@8596} "org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)"
55 = {StackTraceElement@8597} "org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)"
56 = {StackTraceElement@8598} "org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)"
57 = {StackTraceElement@8599} "org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)"
58 = {StackTraceElement@8600} "javax.servlet.http.HttpServlet.service(HttpServlet.java:626)"
59 = {StackTraceElement@8601} "org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)"
60 = {StackTraceElement@8602} "javax.servlet.http.HttpServlet.service(HttpServlet.java:733)"
61 = {StackTraceElement@8603} "org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)"
62 = {StackTraceElement@8604} "org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)"
63 = {StackTraceElement@8605} "org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)"
64 = {StackTraceElement@8606} "org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)"
65 = {StackTraceElement@8607} "org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)"
66 = {StackTraceElement@8608} "org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)"
67 = {StackTraceElement@8609} "org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)"
68 = {StackTraceElement@8610} "org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)"
69 = {StackTraceElement@8611} "org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)"
70 = {StackTraceElement@8612} "org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)"
71 = {StackTraceElement@8613} "org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)"
72 = {StackTraceElement@8614} "org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)"
73 = {StackTraceElement@8615} "org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)"
74 = {StackTraceElement@8616} "org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)"
75 = {StackTraceElement@8617} "org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)"
76 = {StackTraceElement@8618} "org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)"
77 = {StackTraceElement@8619} "org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)"
78 = {StackTraceElement@8620} "org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)"
79 = {StackTraceElement@8621} "org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)"
80 = {StackTraceElement@8622} "org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)"
81 = {StackTraceElement@8623} "org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)"
82 = {StackTraceElement@8624} "org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)"
83 = {StackTraceElement@8625} "org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)"
84 = {StackTraceElement@8626} "org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)"
85 = {StackTraceElement@8627} "org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)"
86 = {StackTraceElement@8628} "org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)"
87 = {StackTraceElement@8629} "org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)"
88 = {StackTraceElement@8630} "org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)"
89 = {StackTraceElement@8631} "org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)"
90 = {StackTraceElement@8632} "java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)"
91 = {StackTraceElement@8633} "java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)"
92 = {StackTraceElement@8634} "org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)"
93 = {StackTraceElement@8635} "java.lang.Thread.run(Thread.java:748)"

feign

SynchronousMethodHandler

首先在 SynchronousMethodHandler 通过反射调用 executeAndDecode() 发起请求。

如果执行正常,直接返回结果跳出循环,否则异常被捕获到,重试次数达到最大值后直接抛出异常跳出循环。

分为三个部分

第一部分

将每次请求时将请求计数重置

对于重试间隔是按照 period * 1.5^attempt 来计算。

period 默认是 100ms,attempt 的值与时间对应关系如下

attempt 值为 1,间隔为 100*1.5^1=150,即150ms

attempt 值为 2,间隔为 100*1.5^2=225,即225ms

attempt 值为 3,间隔为 100*1.5^3=337,即337ms

attempt 值为 4,间隔为 100*1.5^4=506,即506ms

attempt 值为 5,间隔为 100*1.5^5=759,即759ms

第二部分

正常执行返回跳出循环

第三部分

请求异常后重试处理,重试次数达到最大值后直接抛出异常跳出循环

将 IOException 包装到 FeignException

通过类的层次图,可以知道,SocketTimeoutException 是 IOException 的子类

spring-cloud-openfeign

LoadBalancerFeignClient

负责将 feign 原生调用转换为负载均衡调用

将 ClientException 转换为 IOException

ribbon

主要负责负载均衡处理,是客户端侧的实现。从服务注册的单个服务里找到匹配的实例进行调用。

AbstractLoadBalancerAwareClient

内层请求异常返回原始异常,外层异常使用 ribbon 的 ClientException 进行包装

最终通过 feign.Client.Default 来执行实际请求,如果引入了其他依赖(例如 feign-httpclient 或者 feign-hc5 或者 feign-okclient),会使用其他方式。

最终通过 native 函数调用 jvm 层次的 tcp 网络调用,出现异常返回 IOException。

可知,针对 feign 请求过程中出现异常无论如何返回的是 IOException,最终在 feign 的处理下包装为 FeignException。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/526633.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux:kubernetes(k8s)prestop事件的使用(10)

他的作用是在结束pod容器之后进行的操作 apiVersion: v1 # api文档版本 kind: Pod # 资源对象类型 metadata: # pod相关的元数据,用于描述pod的数据name: nginx-po # pod名称labels: # pod的标签type: app #这个是随便写的 自定义的标签version: 1.0.0 #这个…

CTF-PWN-工具配置

pwngdb和pwngdb github上下载pwndbg和pwngdb 最后设置主目录下的.gdbinit文件 source /home/llk/tools/pwndbg/gdbinit.py #pwndbg/gdbinit.py位置而已 source ~/Pwngdb/pwngdb.py source ~/Pwngdb/angelheap/gdbinit.pydefine hook-run python import angelheap angelheap…

亲测:腾讯云8核16G服务器价格1668元一年送3个月,购买需谨慎

腾讯云8核16G轻量服务器CPU性能如何?18M带宽支持多少人在线?轻量应用服务器具有100%CPU性能,18M带宽下载速度2304KB/秒,折合2.25M/s,系统盘为270GB SSD盘,月流量3500GB,折合每天116.6GB流量&…

MyBatis3源码深度解析(七)JDBC单连接事务

文章目录 前言2.7 JDBC单连接事务2.7.1 事务的开启与提交2.7.2 事务隔离级别2.7.2.1 并发访问问题(1)脏读(2)不可重复读(3)幻读 2.7.2.2 事务隔离级别(1)TRANSACTION_NONE&#xff1…

程序员常见的算法介绍

本文将为您详细讲解程序员常见的算法,以及它们的特点、区别和应用场景。算法是计算机科学的核心概念之一,它们是解决问题和执行任务的方法和步骤。在编程中,算法是实现特定功能的基础。 1. 排序算法 特点 - 用于将一组数据按照特定的…

活力up+

持续学习和接触新鲜事物能够有效激发大脑的神经可塑性,这一特性使得大脑能够在面对新信息、新挑战时灵活调整,构建新的神经连接,强化或弱化已有的神经通路,从而优化大脑的功能和结构。 1.神经可塑性 持续学习和不断接触新鲜事物对…

jupyter notebook 调整深色背景与单元格宽度与自动换行

# 安装jupyter主题 pip install jupyterthemes # 列举主题 jt -l # 设置主题 jt -t chesterish设置宽度 打开users 当前用户目录下的custom.css文件 写入.container { width:80% !important; } 即可 设置自动换行 查找创建这个目录以及文件notebook.json 写入配置 “li…

4 个最佳 Windows 文件恢复软件

如何从 Windows 硬盘恢复丢失的数据?您是否获得了最好的 Windows 文件恢复软件?在这里,我们测试并找到最好的 4 个 Windows文件恢复软件,让您轻松快速地从 Windows 硬盘驱动器或任何其他存储设备恢复丢失的数据。 4 个最佳 Windo…

ARM中多寄存内存访问概念和栈的种类和应用

多寄存器内存访问指令 多寄存器内存访问指令 MOV R1,#1 MOV R2,#2 MOV R3,#3 MOV R4,#4 MOV R11,#0x40000020 STM R11,{R1-R4} 将R1-R4寄存器中的数据存储到内存以R11为起始地址的内存中 LDM R11,{R6-R9} 将内存中以R11为起始地址的数据读取到R6-R9寄存器中 当寄存器…

基于决策树实现葡萄酒分类

基于决策树实现葡萄酒分类 将葡萄酒数据集拆分成训练集和测试集,搭建tree_1和tree_2两个决策树模型,tree_1使用信息增益作为特征选择指标,B树使用基尼指数作为特征选择指标,各自对训练集进行训练,然后分别对训练集和测…

linux驱动——中断

1.Cortex-A系列的中断的简介 中断的基本概念:(interrupt) 中断本质上是系统内部的异常机制,当中断产生之后,他会停下当前正在执行的任务,转而去做其他的事情,在停下当前正在执行的任务之前,要先入栈(保护现场,其他的事情做完之后…

基于GAN对抗网进行图像修复

一、简介 使用PyTorch实现的生成对抗网络(GAN)模型,包括编码器(Encoder)、解码器(Decoder)、生成器(ResnetGenerator)和判别器(Discriminator)。…