Spring Cloud Commons Tutorial (ii) Spring resttemplate as a load balancer client

Source: Internet
Author: User

RestTemplate can be automatically configured to use the Ribbon. Create RestTemplate RestTemplate @Bean and use @LoadBalanced qualifiers to create load balancing.

Warning The bean is no longer created by automatic configuration RestTemplate . It must be created by a single application.
@Configurationpublic class MyConfiguration {    @LoadBalanced    @Bean    RestTemplate restTemplate() {        return new RestTemplate();    }}public class MyClass {    @Autowired    private RestTemplate restTemplate;    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 RestTemplate More information about how to set up, 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 the spring.cloud.loadbalancer.retry.enabled=false . The properties that you can use are client.ribbon.MaxAutoRetries , client.ribbon.MaxAutoRetriesNextServer and client.ribbon.OkToRetryOnAllOperations . See the Ribbon documentation for details about the properties.

Attention The above example client should be replaced with your ribbon client name.

Source Source

Spring Cloud Commons Tutorial (ii) 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.