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

Spring cloud 使用restTemplate的ribbon/eureka/hystrix的spring cloud无法设置连接/读取超时,spring-cloud,Spring Cloud,我已经使用SpringCloud构建了一个SpringBoot应用程序,并希望在我的客户机应用程序(也是一个微服务)中使用RestTemplate,以便继续使用mockMvc进行集成测试。我使用默认的ribbon/eureka/hystrix客户端设置,在我调用的服务中使用我的客户端microservice和eureka客户端。这是可行的(一旦我发现ServiceID是restTemplate中标识服务端点的东西)。我的问题是,我似乎无法更改restTemplate读取或连接超时(默认值为300

我已经使用SpringCloud构建了一个SpringBoot应用程序,并希望在我的客户机应用程序(也是一个微服务)中使用RestTemplate,以便继续使用mockMvc进行集成测试。我使用默认的ribbon/eureka/hystrix客户端设置,在我调用的服务中使用我的客户端microservice和eureka客户端。这是可行的(一旦我发现ServiceID是restTemplate中标识服务端点的东西)。我的问题是,我似乎无法更改restTemplate读取或连接超时(默认值为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
    ..
    }
}`

有人知道我需要设置哪些属性来修改restTemplate的默认超时吗?关于这个主题的文档非常简单,最近的代码似乎甚至允许对ribbon/eureka spring引导默认值使用restTemplate。可能这还没有建立。

您正在注入的
restemplate
完全是普通的,除了一个
RibbonInterceptor
为您选择URI中的物理主机(请参阅)。超时和其他属性通过
ClientHttpRequest
restemplate
中控制。您可能只需要将
RibbonInterceptor
注入您自己的
RestTemplate
中,并设置一个
ClientHttpRequestFactory
来执行超时,例如

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

既然我不能评论,我就回答。RestTemplate集成仅使用功能区LoadBalancer,而不是RestClient或NFHttpClient


顺便说一句,您不再需要spring.cloud.client.serviceIds。如果它在文档中,我将删除它。

感谢你们两位这么快的回答!这和@Spencergib的回答对我都有帮助。知道ribbon的配置与RestTemplate无关,并且知道除了RibbonInterceptor之外,ribbon的配置对测试也有帮助。我将按照上面列出的方式设置自己。顺便说一句,这里还有一个超时,那就是hystrix的execution.isolation.thread.timeoutinems。因此现在有3个超时,Ribbon的、RestTemplate的和Hystrix的。@DaveSyer看起来好像
RibbonInterceptor
在发布版本中消失了。你能告诉我现在如何配置RestTemplate的正确方向吗?试试
LoadBalancerInterceptor
@DirkLachowski:你能提供你需要工作的配置示例吗?@头脑风暴当然,看一下,但也要注意,如果你把rest模板的超时推得太远,你必须调整hystrix的超时(要点中第二节课的样本)。看看为什么不再需要serviceIds?建议是什么?我发现的示例使用serviceIds。你能指出相关文档吗?还有,请在可能的时候给出你对这篇文章的见解。
serviceIds
是一个早期的实现,不再需要了。哪些示例仍然有它?请参阅。到目前为止正如我所知,这个SO问题是唯一引用它的东西。你想做什么?我想这些都使用ServiceID。Josh Long的教程。我只是浏览了这些项目,没有看到它被使用。如果你看
https://github.com/joshlong/service-registration-and-discovery/blob/master/passport-service/src/ma在/java/passport/Application.java
中,您会注意到
书签服务
照片服务
是serviceID。要从微服务中调用其他微服务,需要serviceID,对吗?
@Component
class EricComponentToDoHystrix {
    private RestTemplate restTemplate;
    @Autowired
    public EricComponentToDoHystrix(RibbonInterceptor interceptor) {
         restTemplate = new RestTemplate();
         restTemplate.setInterceptors(Arrays.asList(interceptor));
         restTemplate.setRequestFactory(...);
    }
}