Spring Cloud uses Ribbon components to balance loads. Ribbon provides consumer-side software load balancing algorithms. In Spring Cloud, Ribbon implements load balancing at the underlying layer for RestTemplate and Feign clients. This topic describes how to use Ribbon to implement load balancing for Spring Cloud applications.

Background information

Nacos integrates the features of Ribbon. NacosServerList implements the com.netflix.loadbalancer.ServerList interface provided by Ribbon. This interface is for generic use. Other similar service discovery components, such as Eureka, Consul, and ZooKeeper, also implement the corresponding ServerList interfaces, such as DomainExtractingServerList, ConsulServerList, and ZookeeperServerList.

If the interface is implemented, your service supports the generic specifications of Spring Cloud load balancing. When you change the tool used for your service discovery solution from Eureka, Consul, or ZooKeeper to Spring Cloud Alibaba, you can implement load balancing without the need to modify your code. This means that you do not need to modify your RestTemplate code, FeignClient code, or your code of the deprecated AsyncRestTemplate for your service discovery solution.

Note To ensure the compatibility between Enterprise Distributed Application Service (EDAS) and Hystrix, add the dependency of Hystrix to your client. It supports the fallback attribute. This can be implemented by Sentinel.

You can implement load balancing on your applications by using the instructions in this topic. You can also download the application demos: service-provider and service-consumer.

The method to implement load balancing for RestTemplate is different from that for Feign. The following sections in the topic describe how to implement application load balancing for RestTemplate and Feign.

RestTemplate

RestTemplate is a client that is provided by Spring Cloud to access RESTful services. It provides multiple convenient methods to access remote HTTP services. This helps you write clients in a more efficient manner.

To implement load balancing for RestTemplate, modify the code in your application based on the following example:

public class MyApp {
    // Inject the RestTemplate client that is built by using the @LoadBalanced annotation.
    // This annotation adds the following interceptor to the RestTemplate client: LoadBalancerInterceptor.
    // LoadBalancerInterceptor internally uses the implementation class RibbonLoadBalancerClient of the LoadBalancerClient interface to balance loads.
    @Autowired
    private RestTemplate restTemplate;

    @LoadBalanced // Modify the RestTemplate client that is built by using the @LoadBalanced annotation to enable the load balancing feature.
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    // The RestTemplate client calls services. Load balancing is internally implemented.
    public void doSomething() {
        Foo foo = restTemplate.getForObject("http://service-provider/query", Foo.class);
        doWithFoo(foo);
    }

    ...
}            

Feign

Feign is a Java HTTP client that is implemented in Java to simplify RESTful API calls.

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

  1. Use @EnableFeignClients to enable the features of Feign.
    @SpringBootApplication
    @EnableFeignClients // Enable the features of Feign.
    public class MyApplication {
      ...
    }                        
  2. Use @FeignClient to build a Feign client.
    @FeignClient(name = "service-provider")
    public interface EchoService {
            @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
            String echo(@PathVariable("str") String str);
    }                        
  3. Inject the EchoService and call the echo method.
    When you call the echo method, an HTTP request is initiated.
    public class MyService {
    @Autowired // Inject the EchoService that is built by using the @FeignClient annotation.
        private EchoService echoService;
    
        public void doSomething() {
        // This is equivalent to the operation of initiating an http://service-provider/echo/test request.
        echoService.echo("test");
            }
        ...
    }                        

Verify the result

After the service-consumer service and multiple service-provider services are started, access the URL that is provided by service-consumer to check whether load balancing is implemented.

  • RestTemplate

    Access /echo-rest/rest-test multiple times and check whether the requests are forwarded to different instances.

  • Feign

    Access /echo-feign/feign-test multiple times and check whether the requests are forwarded to different instances.