Resttemplate can be automatically configured to use the Ribbon. To create a load balancing resttemplate create a resttemplate @Bean and use the @loadbalanced qualifier.
Warning: resttemplate beans are no longer created with automatic configuration. It must be created by a single application. Br/> @Configuration
< p="">
@LoadBalanced@BeanRestTemplate restTemplate() { return new RestTemplate();}
}
public class MyClass {br/> @Autowired
< p="">
public String doOtherStuff() { String results = restTemplate.getForObject("http://stores/stores", String.class); return results;}
}
The URI needs to use the virtual hostname (that is, the service name, not the host name). The Ribbon client is used to create a complete physical address. For more information about how to set up Resttemplate, see ribbonautoconfiguration.
Retry a failed request
Load Balancing Resttemplate can configure requests for retry failures. By default, this logic is disabled, and you can enable it by adding spring Retry to the application's classpath. Load balancing resttemplate will conform to some of the Ribbon configuration values related to retry failed requests. If you want to disable retry logic by using Spring retry in the classpath, you can set Spring.cloud.loadbalancer.retry.enabled=false. The properties you can use are Client.ribbon.maxautoretries,client.ribbon.maxautoretriesnextserver and client.ribbon.OkToRetryOnAllOperations. See the Ribbon documentation for details about the properties.
Spring resttemplate as a load balancer client