springCloud中最重要的就是微服务之间的调用,因为网络延迟或者调用超时会直接导致程序异常,因此超时的配置及处理就至关重要。

在开发过程中被调用的微服务打断点发现会又多次重试的情况,测试环境有的请求响应时间过长也会出现多次请求,网上查询了配置试了一下无果,决定自己看看源码。
本人使用的SpringCloud版本是Camden.SR3。

微服务间调用其实走的是http请求,debug了一下默认的ReadTimeout时间为5s,ConnectTimeout时间为2s,我使用的是Fegin进行微服务间调用,底层用的还是Ribbon,网上提到的配置如下

ribbon:
ReadTimeout:
ConnectTimeout:
MaxAutoRetries:
MaxAutoRetriesNextServer:

超时时间是有效的但是重试的次数无效,如果直接使用ribbon应该是有效的。

下面开始测试:
在微服务B中建立测试方法,sleep 8s 确保请求超时

@PostMapping("/testa")
public Integer testee(){
try {
Thread.sleep(8000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ;
}
 

在微服务A中使用fegin调用此方法时看到有异常

看到在SynchronousMethodHandler中请求的方法

Object executeAndDecode(RequestTemplate template) throws Throwable {
Request request = targetRequest(template);
if (logLevel != Logger.Level.NONE) {
logger.logRequest(metadata.configKey(), logLevel, request);
}
Response response;
long start = System.nanoTime();
try {
response = client.execute(request, options);
response.toBuilder().request(request).build();
} catch (IOException e) {
if (logLevel != Logger.Level.NONE) {
logger.logIOException(metadata.configKey(), logLevel, e, elapsedTime(start));
}
//出现异常后抛出RetryableException
throw errorExecuting(request, e);
}

出现异常后调用 throw errorExecuting(request, e) 抛出异常

在调用executeAndDecode的地方catch

@Override
public Object invoke(Object[] argv) throws Throwable {
RequestTemplate template = buildTemplateFromArgs.create(argv);
Retryer retryer = this.retryer.clone();
while (true) {
try {
return executeAndDecode(template);
} catch (RetryableException e) {
//重试的地方
retryer.continueOrPropagate(e);
if (logLevel != Logger.Level.NONE) {
logger.logRetry(metadata.configKey(), logLevel);
}
continue;
}
}
}

retryer.continueOrPropagate(e); 这句就是关键继续跟进

public void continueOrPropagate(RetryableException e) {
//maxAttempts是构造方法传进来的大于重试次数抛出异常,否则继续循环执行请求
if (attempt++ >= maxAttempts) {
throw e;
}

默认的Retryer构造器

public Default() {
this(, SECONDS.toMillis(), );
}

第一个参数period是请求重试的间隔算法参数,第二个参数maxPeriod 是请求间隔最大时间,第三个参数是重试的次数。算法如下:

long nextMaxInterval() {
long interval = (long) (period * Math.pow(1.5, attempt - ));
return interval > maxPeriod ? maxPeriod : interval;
}

新建一个配置类

@Configuration
public class FeginConfig {
@Bean
public Retryer feginRetryer(){
Retryer retryer = new Retryer.Default(, SECONDS.toMillis(), );
return retryer;
}
}
在feginClient是加入configuration的配置
@FeignClient(value = "fund-server",fallback = FundClientHystrix.class,configuration = FeginConfig.class)
public interface FundClient

重启重试,只调用了一次,Fegin重试次数解决。
我们再看看请求超时这里的参数

@Override
public Response execute(Request request, Request.Options options) throws IOException {
try {
URI asUri = URI.create(request.url());
String clientName = asUri.getHost();
URI uriWithoutHost = cleanUrl(request.url(), clientName);
FeignLoadBalancer.RibbonRequest ribbonRequest = new FeignLoadBalancer.RibbonRequest(
this.delegate, request, uriWithoutHost);
//请求参数
IClientConfig requestConfig = getClientConfig(options, clientName);
return lbClient(clientName).executeWithLoadBalancer(ribbonRequest,
requestConfig).toResponse();
}
catch (ClientException e) {
IOException io = findIOException(e);
if (io != null) {
throw io;
}
throw new RuntimeException(e);
}
}

其中ReadTimeout 和 ConnectTimeout 读取的就是ribbon的配置,再来看一眼

ribbon:
ReadTimeout:
ConnectTimeout:
MaxAutoRetries:
MaxAutoRetriesNextServer:

如果想覆盖ribbon的超时设置可以在刚刚写的FeginConfig里注入下面的bean

@Bean
public Request.Options feginOption(){
Request.Options option = new Request.Options(,);
return option;
}

总结:使用开源的东西在弄不清问题出在哪时最好能看看源码,对原理的实现以及自己的编码思路都有很大的提升。

http://www.jianshu.com/p/767f3c72b547

SpringCloud Fegin超时重试源码的更多相关文章

  1. 【springcloud】2.eureka源码分析之令牌桶-限流算法

    国际惯例原理图 代码实现 package Thread; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomi ...

  2. SpringCloud之RefreshScope 源码解读

    SpringCloud之RefreshScope @Scope 源码解读 Scope(org.springframework.beans.factory.config.Scope)是Spring 2. ...

  3. Flask框架 (四)—— 请求上下文源码分析、g对象、第三方插件(flask_session、flask_script、wtforms)、信号

    Flask框架 (四)—— 请求上下文源码分析.g对象.第三方插件(flask_session.flask_script.wtforms).信号 目录 请求上下文源码分析.g对象.第三方插件(flas ...

  4. SpringCloud升级之路2020.0.x版-33. 实现重试、断路器以及线程隔离源码

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在前面两节,我们梳理了实现 Feign 断路器以及线程隔离的思路,并说明了如何优化目前的负 ...

  5. SpringCloud Feign 之 超时重试次数探究

    SpringCloud Feign 之 超时重试次数探究 上篇文章,我们对Feign的fallback有一个初步的体验,在这里我们回顾一下,Fallback主要是用来解决依赖的服务不可用或者调用服务失 ...

  6. SpringCloud 源码系列(1)—— 注册中心 Eureka(上)

    Eureka 是 Netflix 公司开源的一个服务注册与发现的组件,和其他 Netflix 公司的服务组件(例如负载均衡.熔断器.网关等)一起,被 Spring Cloud 整合为 Spring C ...

  7. SpringCloud 源码系列(4)—— 负载均衡 Ribbon

    一.负载均衡 1.RestTemplate 在研究 eureka 源码上篇中,我们在 demo-consumer 消费者服务中定义了用 @LoadBalanced 标记的 RestTemplate,然 ...

  8. SpringCloud 源码系列(5)—— 负载均衡 Ribbon(下)

    SpringCloud 源码系列(4)-- 负载均衡 Ribbon(上) SpringCloud 源码系列(5)-- 负载均衡 Ribbon(下) 五.Ribbon 核心接口 前面已经了解到 Ribb ...

  9. SpringCloud 源码系列(6)—— 声明式服务调用 Feign

    SpringCloud 源码系列(1)-- 注册中心 Eureka(上) SpringCloud 源码系列(2)-- 注册中心 Eureka(中) SpringCloud 源码系列(3)-- 注册中心 ...

随机推荐

  1. xpath教程二 ---- 通过ID和Class检索

    必备知识点 在html中,id是唯一的 在html中,class是可以多处引用的 工具 Python3版本 lxml库[优点是解析快] HTML代码块[从网络中获取或者自己杜撰一个] requests ...

  2. 【Linux】- CentOS安装Mysql 5.7

    CentOS7默认数据库是mariadb,而不是mysql.CentOS7的yum源中默认是没有mysql的.所以不能使用yum install直接安装. 下载mysql的repo源 cd /usr/ ...

  3. 《Effective C#》快速笔记(五)- - C# 中的动态编程

    静态类型和动态类型各有所长,静态类型能够让编译器帮你找出更多的错误,因为编译器能够在编译时进行大部分的检查工作.C# 是一种静态类型的语言,不过它加入了动态类型的语言特性,可以更高效地解决问题. 一. ...

  4. html的head内标签

    ctrl+?:自动注释 ctrl+/:  注释多行,再按一次,取消注释的多行. 一,*********本地测试的方法:1-找到文件路径,直接浏览器打开:2-pycharm打开测试. 二,模板的解释: ...

  5. AD高级培训PPT总结

    AD高级培训PPT总结 https://bbs.sangfor.com.cn/forum.php?mod=viewthread&tid=44905&highlight=     说明: ...

  6. 【刷题】SPOJ 8222 NSUBSTR - Substrings

    You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as ...

  7. POJ3977:Subset——题解(三分+折半搜索)

    http://poj.org/problem?id=3977 题目大意:有一堆数,取出一些数,记他们和的绝对值为w,取的个数为n,求在w最小的情况下,n最小,并输出w,n. ————————————— ...

  8. BZOJ4259:残缺的字符串——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4259 很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度 ...

  9. react事件机制

    1. react的事件是合成事件((Synethic event),不是原生事件 <button onClick={this.handleClick}></button> &l ...

  10. JavaScript闭包的特性

    先看一下代码: 01 <ul> 02     <li>1111</li> 03     <li>2222</li> 04     <l ...