springcloud feign 请求超时处理

报错日志:

java.net.SocketTimeoutException: Read timed out

一。没有开启hystrix熔断配置,即配置文件没有配置如下信息

#打开feign-hystrix
feign:
  hystrix:
    enabled: true

处理方式,设置ribbon的请求超时的时间即可:

#请求处理的超时时间 
ribbon:
  ReadTimeout: 4000
#请求连接的超时时间
  ConnectTimeout: 3000

原理:高版本的springcloud-openfeign请求分为两层,先ribbon控制,后hystrix控制,hystrix默认处于关闭状态

二。开启hystrix,配置hystrix的超时时间即可

#打开feign-hystrix
feign:
  hystrix:
    enabled: true
#hystrix 配置
hystrix:
  command:
    default:
      execution:
        timeout:
        #如果enabled设置为false,则请求超时交给ribbon控制
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 5000

三。如何判断feign请求时超时还是被降级处理

1.请求超时会后台会报一下错误

java.net.SocketTimeoutException: Read timed out

2.被降级处理,则请求返回为自定义的降级方法返回的信息

四。配置信息配置技巧,不必死记硬背

如配置hystrix的超时配置:可以通过查看源码发现其配置类在

com.netflix.hystrix 包下的 HystrixCommandProperties 类

查看源码可以知道,hystrix的超时配置项为:

this.executionIsolationThreadInterruptOnTimeout = getProperty(propertyPrefix, key, "execution.isolation.thread.timeoutInMilliseconds", builder.getExecutionIsolationThreadInterruptOnTimeout(), default_executionIsolationThreadInterruptOnTimeout);
则该项的配置信息为:
execution.isolation.thread.timeoutInMilliseconds


你可能感兴趣的:(springcloud)