根据之前的Feign 源码分析,知道了Feign的整个配置项的加载,接口的动态代理创建和请求的执行过程。那么Hytrix、Feign、Ribbon三个的超时时间设置都会对请求产生影响,完全根据自己的业务进行判断。
1、Hytrix配置
# hystrix 配置
feign:
hystrix:
enabled: true
hystrix:
command:
default:
execution:
isolation:
strategy: SEMAPHORE
thread:
timeoutInMilliseconds: 60000
shareSecurityContext: true
2、Feign配置
# feign 配置
feign:
client:
config:
default:
connectTimeout: 10000
readTimeout: 10000
compression:
request:
enabled: true
response:
enabled: true
3、Ribbon配置
全局配置,对所有的@FeignClient生效
ribbon:
ReadTimeout: 10000 # 连接超时
ConnectTimeout: 10000 # 读取超时
应用级配置,比如当前【订单微服务】需要调用【商品微服务】、【价格微服务】等则可以对单个进行配置
@FeignClient(value = "user-provider", contextId = "userService",
path = "/api/user", fallback = UserServiceFallback.class)
public interface UserInterface {
String getUser(@PathVariable("id") Long id);
}
# 对【用户微服务】的ribbon配置,user-provider对应注册中心的服务名
user-provider:
ribbon:
OkToRetryOnAllOperations: true # 对所有操作请求都进行重试
MaxAutoRetries: 2 # 对当前实例的重试次数
MaxAutoRetriesNextServer: 0 # 切换实例的重试次数
ConnectTimeout: 3000 # 请求连接的超时时间
ReadTimeout: 3000 # 请求处理的超时时间