Spring Cloud Feign 第一次调用超时(Read timed out)的解决办法

时间:2024-11-15 16:56:38
  • feign-client在第一次调用微服务会出现Read timed out异常,提示的报错信息:
: Read timed out
  • 这是由于在调用其他微服务接口前,会去请求该微服务的相关信息(地址、端口等),并做一些初始化操作,由于默认的懒加载特性,导致了在第一次调用时,出现超时的情况,解决方法主要有两种:

    • 第一种办法是设置超时时间,具体设置成多少,因项目而异,配置如下:
    # 全局关闭Hystrix超时,对所有微服务有效
    : false
    
    # 关闭某一个微服务的超时
    .<serviceName>.: false
    
    # 全局设置超时时间为60秒
    : 60000
    
    # 设置某一个微服务的超时时间为60秒
    .<serviceName>.: 60000
    
    # 设置ribbon超时时间
    ribbon:
      ReadTimeout: 20000
      ConnectTimeout: 20000
    
    • 第二种办法,也是比较推荐的方式,配置ribbon立即加载,此处需要注意的是,光配置立即加载是不生效的,还要配置客户端列表,配置如下,详细配置请点击
    ribbon:
        eager-load:
            enabled: true
            clients: distribution, material-product, outer-data
    
  • 最后附上未开启立即加载时,第一次调用打印的日志:

2019-05-27 18:05:33.819|INFO|1986|http-nio-18884-exec-2|clipcloud-configuration|3752c9412075144e|3752c9412075144e|false|.test1-55|time:2019-05-27 18:05:33.819
2019-05-27 18:05:33.826|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|$requestInterceptor$2-51|add request header for feignclient, key:cookie,value:JSESSIONID.8250e395=node08z98wvylx12lwh89c6v09qk92.node0; screenResolution=1440x900; m=2258:cmFiYml0bXE6cmFiYml0bXE%253D
2019-05-27 18:05:33.828|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|-588|Refreshing SpringClientFactory-distribution: startup date [Mon May 27 18:05:33 CST 2019]; parent: @203dd56b
2019-05-27 18:05:33.850|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|.<init>-153|JSR-330 '' annotation found and supported for autowiring
2019-05-27 18:05:33.864|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|$-115|Flipping property:  to use NEXT property:  = 2147483647
2019-05-27 18:05:33.865|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|.<init>-58|Shutdown hook installed for: NFLoadBalancer-PingTimer-distribution
2019-05-27 18:05:33.866|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|-192|Client: distribution instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=distribution,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2019-05-27 18:05:33.884|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|-222|Using serverListUpdater PollingServerListUpdater
2019-05-27 18:05:33.886|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|$-115|Flipping property:  to use NEXT property:  = 2147483647
2019-05-27 18:05:33.887|INFO|1986|hystrix-distribution-1|clipcloud-configuration|3752c9412075144e|405382c2fc629614|false|-150|DynamicServerListLoadBalancer for client distribution initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=distribution,current list of Servers=[192.168.26.103:18887],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;	Instance count:1;	Active connections count: 0;	Circuit breaker tripped count: 0;	Active connections per server: 0.0;]
},Server stats: [[Server:192.168.26.103:18887;	Zone:defaultZone;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
]}ServerList:@173441c2
2019-05-27 18:05:33.906|INFO|1986|http-nio-18884-exec-2|clipcloud-configuration|3752c9412075144e|3752c9412075144e|false|.test1-57|time:2019-05-27 18:05:33.906
2019-05-27 18:05:34.888|INFO|1986|PollingServerListUpdater-0|clipcloud-configuration||||$-115|Flipping property:  to use NEXT property:  = 2147483647