Spring resttemplate as a load balancer client

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.