spring-cloud with ribbon/eureka/hystrix using restTemplate unable to set connect/read timeouts

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 19:54:14

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(...);
    }
}

Since I can't comment, I'll answer. The RestTemplate integration only uses the Ribbon LoadBalancer, not RestClient or NFHttpClient.

You no longer need spring.cloud.client.serviceIds, btw. If it is in docs, I'll remove it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!