spring-cloud with ribbon/eureka/hystrix使用restTemplate无法设置连接/读取超时

Rub*_*sMN 7 spring-cloud

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

电话详情:

`@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
    ..
    }
}`
Run Code Online (Sandbox Code Playgroud)

使用包含以下内容的application.properties:

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
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*yer 6

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).超时和其它性质被控制在RestTemplate通过ClientHttpRequest.你可能应该注入RibbonInterceptor你自己的RestTemplate并设置一个ClientHttpRequestFactory来做超时,例如

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