说明
网上很多配置感觉有点乱,版本没有具体说明,我这里是针对我自己使用的Finchley版本进行的配置。
Feign Retry 设置
feign.retryer的超时设置优先级>ribbon超时间设置,而且ribbon的超时时间设置必须要小于hystrix的熔断时间。
feign里ribbon超时设置只有ReadTimeout、ConnectTimeout、SocketTimeout有效,而maxAutoRetries 、maxAutoRetriesNextServer 是无效的
超时连接次数是通过feign.Retryer.Default对象里面的maxAttempts参数进行设置的。
关闭Retry设置
- 创建Retryer对象,设置为NEVER_RETRY
@Bean
public Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
}
开起Retry设置
feign:
hystrix:
enabled: true
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 120000
ribbon:
eureka:
enable: true
eager-load:
enabled: true
clients: service-constant,service-flightcount,service-flightinfo,service-flowcontrol,service-statistic
ReadTimeout: 30000
ConnectTimeout: 10000
SocketTimeout: 10000
OkToRetryOnAllOperations: true
- Java 对象配置,可以写在yml配置文件中,但是个人还是习惯创建config bean对象,主要设置请求连接数之类的
@Bean
public Request.Options options() {
return new Request.Options(3000, 3000);
}
@Bean
public Retryer feignRetryer() {
return new Retryer.Default(100, 1000,3);
}
long nextMaxInterval() {
long interval = (long)((double)this.period * Math.pow(1.5D, (double)(this.attempt - 1)));
return interval > this.maxPeriod ? this.maxPeriod : interval;
}
Zuul Retry设置
zuul的重试默认是关闭的,因此这里只有开启方面的设置
zuul的ribbon超时时间和feign略有不同
ReadTimeout、ConnectTimeout、SocketTimeout、maxAutoRetries 、maxAutoRetriesNextServer都有效
ribbon超时时间 = (ReadTimeout + ConnectTimeout) * (maxAutoRetries + 1) * (maxAutoRetriesNextServer + 1);
但是同样的,ribbon的超时时间需要小于hystrix的熔断时间。
zuul:
retryable: true
ribbon:
eager-load:
enabled: true
host:
connect-timeout-millis: 10000
socket-timeout-millis: 60000
ribbon:
ReadTimeout: 10000
SocketTimeout: 5000
ConnectTimeout: 5000
MaxAutoRetries: 2
MaxAutoRetriesNextServer: 0
OkToRetryOnAllOperations: true
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 60000