本文介绍了使用restTemplate的带有ribbon/eureka/hystrix的spring-cloud无法设置连接/读取超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 spring-cloud 构建了一个 spring boot 应用程序,并希望在我的客户端应用程序(这也是一个微服务)中使用 RestTemplate,以便我可以继续使用 mockMvc 进行集成测试.我正在使用默认的ribbon/eureka/hystrix客户端设置以及我正在调用的服务中的客户端微服务和eureka客户端.这是有效的(一旦我发现 serviceIds 是在 restTemplate 中标识服务端点的东西).我的问题是我似乎无法将 restTemplate 读取或连接超时从似乎默认的 300 毫秒更改.

I have built a spring boot application using spring-cloud and want to use RestTemplate within my client application (which is also a microservice) so that I can continue using mockMvc for integration testing. I am a using the default ribbon/eureka/hystrix client setup with my client microservice and eureka client within the service I'm calling. This is working (once I figured out that serviceIds are what identifies a service endpoint within restTemplate). My problem is that I cant seem to change the restTemplate read nor connection timeout from what seems like a default of 300ms.

通话详情:

`@Configuration
 @EnableAutoConfiguration
 @ComponentScan
 @EnableConfigurationProperties
 @EnableHystrix
 @EnableEurekaClient
public class Application { ... public static void main(String[] args) {} ... }

@Component
class EricComponentToDoHystrix {   // apparently this has to be a component for hystrix to work btw
    @Autowired
    RestTemplate restTemplate;
    ..
    @HystrixCommand(fallbackMethod="defaultRestTemplateCall")
    public void doRestTemplateCall()
        ResponseEntity<String> result = restTemplate.getForEntity("http://someservice/doSomething", String.class);  // actually make a call
    ..
    }
}`

一个 application.properties 包含:

with an application.properties containing:

spring:
  cloud:
    client:
      serviceIds:
        - someservice

someservice:
  ribbon:
    #listOfServers: localhost:7080
    #NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList

    # the eureka vipAddress of the target service (Disabled)
    DeploymentContextBasedVipAddresses: someservice

    # Interval to refresh the server list from the source
    ServerListRefreshInterval: 1000

    # Connect timeout used by Apache HttpClient.. apparently not by restTemplate
    ConnectTimeout: 30000

    # Read timeout used by Apache HttpClient.. apparently not by restTemplate
    ReadTimeout: 30000

eureka:
  client:
    #Region where eureka is deployed -For AWS specify one of the AWS regions, for other datacenters specify a arbitrary string
    #indicating the region.This is normally specified as a -D option (eg) -Deureka.region=us-east-1
    region: default

    #For eureka clients running in eureka server, it needs to connect to servers in other zones
    preferSameZone: false

    us-east-1:
      availabilityZones: default

  instance:
    #Virtual host name by which the clients identifies this service
    virtualHostName: ${spring.application.name}
    appGroupName: ericGroup


# disable Ribbon's cicruit breaker and rely soley on Hystrix.
# this helps to avoid confusion.
# see https://github.com/Netflix/ribbon/issues/15
niws:
  loadbalancer:
    availabilityFilteringRule:
      filterCircuitTripped: false

有人知道我需要什么属性来修改restTemplate的默认超时吗?关于这个主题的文档非常简单,似乎最近的代码甚至允许针对ribbon/eureka spring boot默认值使用restTemplate.也许这还没有建成.

Anybody know what properties I need so set to modify restTemplate's default timeouts? Documentation is very light on this subject and it seems the code just recently even allowed restTemplate to be used against ribbon/eureka spring boot defaults. Perhaps this has not been built yet.

推荐答案

你正在注入的 RestTemplate 完全是普通的,除了一个 RibbonInterceptor 选择物理主机您的 URI(请参阅 https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.java).超时和其他属性通过 ClientHttpRequestRestTemplate 中控制.您可能应该将 RibbonInterceptor 注入您自己的 RestTemplate 并设置一个 ClientHttpRequestFactory 来执行超时,例如

The RestTemplate you are injecting is completely vanilla except for a RibbonInterceptor that chooses the physical host in the URI for you (see https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.java). The timeouts and other properties are controlled in the RestTemplate through the ClientHttpRequest. You probably should just inject the RibbonInterceptor into your own RestTemplate and set up a ClientHttpRequestFactory to do the timeouts, e.g.

@Component
class EricComponentToDoHystrix {
    private RestTemplate restTemplate;
    @Autowired
    public EricComponentToDoHystrix(RibbonInterceptor interceptor) {
         restTemplate = new RestTemplate();
         restTemplate.setInterceptors(Arrays.asList(interceptor));
         restTemplate.setRequestFactory(...);
    }
}

这篇关于使用restTemplate的带有ribbon/eureka/hystrix的spring-cloud无法设置连接/读取超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 10:33