Java FeignClient超时如何解决

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38080283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:00:29  来源:igfitidea点击:

How to solve Timeout FeignClient

javaspring-bootspring-datanetflix-feign

提问by Renan Lalier

My application is getting below error when consuming a service that performs queries in SQL Server using FeignClient.

我的应用程序在使用在 SQL Server 中使用FeignClient.

ERROR:

错误:

Exception in thread "pool-10-thread-14" feign.RetryableException: Read timed out executing GET http://127.0.0.1:8876/processoData/search/buscaProcessoPorCliente?cliente=ELEKTRO+-+TRABALHISTA&estado=SP

线程“pool-10-thread-14”中的异常feign.RetryableException:读取超时执行GET http://127.0.0.1:8876/processoData/search/buscaProcessoPorCliente?cliente=ELEKTRO+-+TRABALHISTA&estado=SP

My Consumer Service:

我的消费者服务:

@FeignClient(url="http://127.0.0.1:8876")
public interface ProcessoConsumer {

@RequestMapping(method = RequestMethod.GET, value = "/processoData/search/buscaProcessoPorCliente?cliente={cliente}&estado={estado}")
public PagedResources<ProcessoDTO> buscaProcessoClienteEstado(@PathVariable("cliente") String cliente, @PathVariable("estado") String estado);

}

My YML:

我的 YML:

server:
  port: 8874

endpoints:
  restart:
    enabled: true
  shutdown:
    enabled: true
  health:
    sensitive: false

eureka:
  client:
  serviceUrl:
    defaultZone: ${vcap.services.eureka-service.credentials.uri:http://xxx.xx.xxx.xx:8764}/eureka/
  instance: 
    preferIpAddress: true

ribbon:
  eureka:
    enabled: true

spring:
  application:
    name: MyApplication
  data:
    mongodb:
      host: xxx.xx.xxx.xx
      port: 27017
      uri: mongodb://xxx.xx.xxx.xx/recortesExtrator
      repositories.enabled: true
    solr:
      host: http://xxx.xx.xxx.xx:8983/solr
      repositories.enabled: true

Anyone know how to solve this?

有谁知道如何解决这个问题?

Thanks.

谢谢。

回答by Marco Tedone

Look at this answer. It did the trick for me. I also did a bit of research and I've found the properties documentation here:

看看这个答案。它对我有用。我也做了一些研究,我在这里找到了属性文档:

https://github.com/Netflix/Hystrix/wiki/Configuration#intro

https://github.com/Netflix/Hystrix/wiki/Configuration#intro

回答by stayfool

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
ribbon.ReadTimeout=60000
ribbon.ConnectTimeout=60000

make sure ribbon's timeout is bigger than hystrix

确保功能区的超时时间大于 hystrix

回答by Sudhakar

just ran into this issue as well. As suggested by @spencergibb here is the workaround I'm using. See the link

刚刚也遇到了这个问题。正如@spencergibb 所建议的,这里是我正在使用的解决方法。见链接

Add these in the application.properties.

在 application.properties 中添加这些。

# Disable Hystrix timeout globally (for all services)
hystrix.command.default.execution.timeout.enabled: false

# Increase the Hystrix timeout to 60s (globally)
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000

Add this in the Java configuration class.

在 Java 配置类中添加这个。

import feign.Request;

@Configuration
@EnableDiscoveryClient
@EnableFeignClients(basePackageClasses = { ServiceFeignClient.class })
@ComponentScan(basePackageClasses = { ServiceFeignClient.class })
public class FeignConfig {

    /**
     * Method to create a bean to increase the timeout value, 
     * It is used to overcome the Retryable exception while invoking the feign client.
     * @param env,
     *            An {@link ConfigurableEnvironment}
     * @return A {@link Request}
     */
    @Bean
    public static Request.Options requestOptions(ConfigurableEnvironment env) {
        int ribbonReadTimeout = env.getProperty("ribbon.ReadTimeout", int.class, 70000);
        int ribbonConnectionTimeout = env.getProperty("ribbon.ConnectTimeout", int.class, 60000);

        return new Request.Options(ribbonConnectionTimeout, ribbonReadTimeout);
    }
}

回答by chen

Add these in the application.properties

在 application.properties 中添加这些

feign.hystrix.enabled=false hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

feign.hystrix.enabled=false hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

回答by chaitanya dalvi

Add below properties in Application.properties file.

在 Application.properties 文件中添加以下属性。

feign.client.config.default.connectTimeout: 160000000
feign.client.config.default.readTimeout: 160000000

回答by Kamal A. SIddiqui

eureka: client: eureka-server-read-timeout-seconds: 30

尤里卡:客户端:尤里卡服务器读取超时秒数:30

回答by yunandtidus

I'm using Feign.builder()to instantiate my Feign clients.

我正在使用Feign.builder()实例化我的 Feign 客户端。

In order to set connectTimeoutand readTimeout, I use the following :

为了设置connectTimeoutand readTimeout,我使用以下内容:

Feign.builder()
     ...
     .options(new Request.Options(connectTimeout, readTimeout))
     .target(MyApiInterface.class, url);

Using this I can configure different timeout for different APIs.

使用它我可以为不同的 API 配置不同的超时。