All Products
Search
Document Center

Implement load balancing

Last Updated: Oct 16, 2019

Spring Cloud uses the Ribbon component for load balancing. Ribbon mainly provides client-side software load balancing algorithms.In Spring Cloud, load balancing is achieved for the underlying RestTemplate and Feign clients through Ribbon.

Spring Cloud Alibaba ANS integrates the functions of Ribbon and AnsServerList implements the com.netflix.loadbalancer.ServerList API provided by Ribbon.

This API is generic and other similar service discovery components, such as Nacos, Eureka, Consul, and ZooKeeper, implement ServerList APIs such as NacosServerList, DomainExtractingServerList, ConsulServerList, and ZookeeperServerList.

Implementing the com.netflix.loadbalancer.ServerList API is equivalent to accessing the load balancing specifications of Spring Cloud. These specifications are generic.This means that no code modification is required to change the service discovery solution from Eureka, Consul, or ZooKeeper to Spring Cloud Alibaba, including RestTemplate, FeignClient, and the outdated AsyncRestTemplate.

The following describes how to implement load balancing of RestTemplate and Feign in your application.

You can download service-provider and service-consumer for complete demos.

RestTemplate

RestTemplate is a client provided by Spring Cloud to access RESTful services. It provides multiple ways to conveniently access remote HTTP services, greatly improving the writing efficiency of client-side code.

To use the load balancing feature of RestTemplate, you need to modify the code in your application based on the following example.

  1. public class MyApp {
  2. // Inject the RestTemplate you built with the @LoadBalanced annotation
  3. // This annotation adds the LoadBalancerInterceptor to RestTemplate
  4. // Internally, LoadBalancerInterceptor uses the implementation class RibbonLoadBalancerClient of the LoadBalancerClient API for load balancing
  5. @Autowired
  6. private RestTemplate restTemplate;
  7. @LoadBalanced //Modify the built RestTemplate with this annotation to enable its load balancing function.
  8. @Bean
  9. public RestTemplate restTemplate() {
  10. return new RestTemplate();
  11. }
  12. // RestTemplate calls services in load balancing mode internally
  13. public void doSomething() {
  14. Foo foo = restTemplate.getForObject("http://service-provider/query", Foo.class);
  15. doWithFoo(foo);
  16. }
  17. ...
  18. }

Feign

Feign is an HTTP client written in Java to simplify RESTful calls.

Use @EnableFeignClients and @FeignClient to initiate a load balancing request.

  1. Enable the functions of Feign with the @EnableFeignClients annotation.

    1. @SpringBootApplication
    2. @EnableFeignClients // Enable the functions of Feign
    3. public class MyApplication {
    4. ...
    5. }
  2. Build a FeignClient with the @FeignClient annotation.

    1. @FeignClient(name = "service-provider")
    2. public interface EchoService {
    3. @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    4. String echo(@PathVariable("str") String str);
    5. }
  3. Inject EchoService and call the echo method.

    Calling the echo method is equivalent to initiating an HTTP request.

    1. public class MyService {
    2. @Autowired // Inject the EchoService you built with the @FeignClient annotation
    3. private EchoService echoService;
    4. public void doSomething() {
    5. // This is equivalent to initiating an "http://service-provider/echo/test" request
    6. echoService.echo("test");
    7. }
    8. ...
    9. }