【报错】Feign Read timed out

国内网站的那些文章都是复制粘贴,不清不楚,我只能说真的垃圾,根本解决 不了问题。

最近遇到由于调取某些接口,等待结果耗时有点长所以遇到了read timed out的问题。

因此根据两种不同的springboot版本进行解决。2.x和1.x两种版本springbooot及其对应的springcloud版本

 

【1】版本:

         springboot:2.1.3

         java:1.8

        spring cloud :Greenwich.SR1

 

       异常:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.RetryableException: Read timed out executing POST http://*******/******] with root cause
java.net.SocketTimeoutException: Read timed out

       解决方法:

      在application.properties中加入以下这段属性

    

feign:
  client:
    config:
      default:
        connectTimeout: 300000
        readTimeout: 300000

    原因是在pom中导入了以下的依赖,此依赖只在springboot2.0以后才有的。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <!--版本是2.1.1 -->
</dependency>

  其中配置属性该依赖下spring-cloud-starter-openfeign-core-2.1.1.jar下的FeignClientProperties.class

这样就搞定了,这个版本。

 

【2】这个就是最折腾的旧版本,也是网上最多复制粘贴,不清不楚同时不能解决问题。

        java:1.7

      springboot:1.5.20.RELEASE

      spring cloud:Camden.SR7

 

异常:类似这样吧,timed-out and no fallback available

 解决方法:引入依赖

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>

在application.properties添加以下属性:

hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: false
        isolation:
          thread:
            timeoutInMilliseconds: 300000
service:
  feign:
    readTimeOut: 300000
    connectTimeout: 300000

具体嘛,我建议可以看看该依赖下的hystrix.javanica-1.5.6.jar的HystrixCommand.class。其实这部分属性可以通过@HystrixCommand这个注解来配置,我没试了。

当然了,最后就是配置Feign的configuration了,

创建一个config bean,是配置ribbon的readTime和connectTime的。

import feign.Request;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServiceFeignConfiguration {

    @Value("${service.feign.connectTimeout}")
    private int connectTimeout;

    @Value("${service.feign.readTimeOut}")
    private int readTimeout;

    @Bean
    public Request.Options options() {
        return new Request.Options(connectTimeout, readTimeout);
    }
}

最后就在feign client中引用啦

@FeignClient(name = "*********-service", configuration = {FeignHeaderInterceptor.class, ServiceFeignConfiguration.class})

搞定这恶心的版本了,你可以运行试试了。

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值