spring cloud中feign请求超时Read timed out

feign时一个声明式的Web服务客户端。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

Feign 中也使用了 Ribbon。当使用包含Ribbon客户端的Hystrix命令时,必须使Hystrix超时配置为长于配置的Ribbon超时,包括可能进行的任何潜在的重试。例如,如果Ribbon连接超时为一秒钟,并且Ribbon客户端可能会重试该请求三次,那么Hystrix超时应该略超过三秒钟。然而feign请求超时报错时,只需配置feign

  1. 环境:spring cloud,eureka,feign
  2. 版本:spring boot (2.1.3.RELEASE),feign (1.4.4.RELEASE)
  3. 控制台错误信息
java.net.SocketTimeoutException: Read timed out
	at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_191]
	at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[na:1.8.0_191]
	at java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_191]
	at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_191]
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) ~[na:1.8.0_191]
	at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) ~[na:1.8.0_191]
	at java.io.BufferedInputStream.read(BufferedInputStream.java:345) ~[na:1.8.0_191]
	at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735) ~[na:1.8.0_191]
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678) ~[na:1.8.0_191]
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587) ~[na:1.8.0_191]
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) ~[na:1.8.0_191]
	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) ~[na:1.8.0_191]
	at feign.Client$Default.convertResponse(Client.java:143) ~[feign-core-10.1.0.jar:na]
	at feign.Client$Default.execute(Client.java:68) ~[feign-core-10.1.0.jar:na]
	at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:93) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:56) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at com.netflix.client.AbstractLoadBalancerAwareClient$1.call(AbstractLoadBalancerAwareClient.java:104) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
	at com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:303) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
	at com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:287) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]

解决办法。官网提供了详细的配置

feign:
  client:
    config:
      feignName:
        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: full
        errorDecoder: com.example.SimpleErrorDecoder
        retryer: com.example.SimpleRetryer
        requestInterceptors:
          - com.example.FooRequestInterceptor
          - com.example.BarRequestInterceptor
        decode404: false
        encoder: com.example.SimpleEncoder
        decoder: com.example.SimpleDecoder
        contract: com.example.SimpleContract
feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: basic

本着踩坑必填的心态,贴上自己部分代码。以便日后自己查看

  1. feign
    @FeignClient(name = "YULIUSHICORE")
    public interface GoodsFeign {
    
        @RequestMapping( value = "/goods/getGoodsList",method = RequestMethod.POST)
        BaseData getGoodsList(@RequestBody Goods goods);
    
    }

     

  2. application.yml。debug使用的超时配置,时间过长。
    server:
      port: 8081
    spring:
      application:
        name: yuliushiweb
    eureka:
      client:
        service-url:
          defaultZone: http://TIPA:8761/eureka,http://TIPB:8762/eureka
    feign:
      client:
        config:
          default:
            connectTimeout: 50000
            readTimeout: 50000
            loggerLevel: basic

     

  3. pom.xml
    
    
    
        
            1.8
            Greenwich.SR1
        
    
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.springframework.cloud
                spring-cloud-starter-netflix-eureka-client
            
    
            
                org.springframework.boot
                spring-boot-starter-tomcat
                provided
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
            
                org.springframework.cloud
                spring-cloud-starter-config
            
            
                org.springframework.cloud
                spring-cloud-starter-feign
                1.4.4.RELEASE
            
        
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    ${spring-cloud.version}
                    pom
                    import
                
            
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
        
            
                spring-milestones
                Spring Milestones
                https://repo.spring.io/milestone
            
        
    
    

     

你可能感兴趣的:(java代码,spring,cloud,feign,Read,timed,out)