Spring -cloud with / eureka / hystrix ribbon using restTemplate unable to set connection / read timeouts

I created a spring boot application using spring -cloud and want to use RestTemplate in my client application (which is also a microservice) so that I can continue to use mockMvc to test integration. I use the default ribbon / eureka / hystrix client setting with my microservice client and eureka client in the service I'm calling. This works (as soon as I realized that serviceIds is what identifies the service endpoint in restTemplate). My problem is that I seem to be unable to change the readTemplate read or timeout due to what seems to be a default of 300 ms.

Call Details:

`@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 .. } }` 

with application.properties application 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 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 

Does anyone know what properties I need to configure to change restTemplate default timeouts? The documentation is very light on this point, and it seems like the code most recently even allowed restTemplate to be used with / eureka spring feed loading settings. It may not have been built yet.

+7
spring cloud
source share
2 answers

RestTemplate that you enter is completely vanilla, with the exception of the RibbonInterceptor , which selects 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 ). Timeouts and other properties are managed in RestTemplate through ClientHttpRequest . You should probably just enter the RibbonInterceptor in your own RestTemplate and configure ClientHttpRequestFactory to do timeouts, for example.

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

Since I can not comment, I will answer. Integration RestTemplate uses only LoadBalancer tapes, not RestClient or NFHttpClient.

You will no longer need spring.cloud.client.serviceIds, by the way. If it is in the documents, I will delete it.

+1
source share

All Articles