Spring Cloud uses the Ribbon component to implement load balancing. Ribbon mainly provides consumer-side software load balancing algorithms. In Spring Cloud, the underlying load balancing for RestTemplate and FeignClient is implemented by Ribbon.

Background

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

This interface is generic. Other similar service discovery components, such as Nacos, Eureka, Consul, and ZooKeeper, also implement ServerList interfaces such as NacosServerList, DomainExtractingServerList, ConsulServerList, and ZookeeperServerList.

Implementing the com.netflix.loadbalancer.ServerList interface is equivalent to complying with the load balancing specifications of Spring Cloud. These specifications are generic. This also means that you can change the service discovery solution from Eureka, Consul, or ZooKeeper to Spring Cloud Alibaba, including RestTemplate, FeignClient, and the outdated AsyncRestTemplate, without modifying any code concerning load balancing.

Note EDAS is compatible with Hystrix, but the user needs to introduce the dependency of Hystrix on the client. It supports the fallback attribute, which can also be implemented by Sentinel.

The following describes how to implement load balancing for RestTemplate and FeignClient in your application.

This section describes key information for developing applications locally. For more information about Spring Cloud, download service-provider and service-consumer.

The methods to implement load balancing for RestTemplate and FeignClient are different and therefore described separately.

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.

public class MyApp {
    // Inject the RestTemplate you built with the @LoadBalanced annotation.
    // This annotation adds the LoadBalancerInterceptor to RestTemplate.
    // Internally, LoadBalancerInterceptor uses the implementation class RibbonLoadBalancerClient of the LoadBalancerClient interface for load balancing.
    @Autowired
    private RestTemplate restTemplate;

    @LoadBalanced // Modify the built RestTemplate with this annotation to enable its load balancing function.
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    // RestTemplate internally calls services in load balancing mode.
    public void doSomething() {
        Foo foo = restTemplate.getForObject("http://service-provider/query", Foo.class);
        doWithFoo(foo);
    }

    ...
}            

Feign

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

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

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

Verify the result

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

  • RestTemplate

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

  • Feign

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