Spring Cloud uses Ribbon for consumer-side software load balancing. Ribbon distributes requests across multiple service provider instances at the client layer, covering both RestTemplate and Feign clients.
How it works
Nacos integrates Ribbon through NacosServerList, which implements the com.netflix.loadbalancer.ServerList interface. This interface is generic — Eureka, Consul, and ZooKeeper implement equivalent interfaces (DomainExtractingServerList, ConsulServerList, and ZookeeperServerList). Because of this, migrating your service discovery from any of those tools to Spring Cloud Alibaba requires no changes to your RestTemplate, FeignClient, or (deprecated) AsyncRestTemplate code.
To ensure compatibility between SAE and Hystrix, add the Hystrix dependency to your client. Hystrix supports the fallback attribute, which can also be implemented by Sentinel.
You can use Alibaba Cloud Toolkit to interconnect on-premises applications and cloud applications that are deployed in SAE by using mutual calls without the need to establish VPN connections. This helps you improve development efficiency.
Prerequisites
Before you begin, ensure that you have:
A Spring Cloud application deployed on SAE (Serverless App Engine)
The Nacos dependency in your project, which integrates Ribbon automatically
Multiple instances of your service provider running, required to observe load balancing behavior
You can download the demo applications to follow along: service-provider and service-consumer.
Enable load balancing with RestTemplate
RestTemplate is a Spring Cloud HTTP client for accessing RESTful services. To enable load balancing, annotate your RestTemplate bean with @LoadBalanced.
Step 1: Declare the load-balanced bean in a configuration class.
@Configuration
public class MyConfiguration {
@LoadBalanced // Adds LoadBalancerInterceptor, which uses RibbonLoadBalancerClient internally.
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}Step 2: Inject the bean and make service calls using the service name as the host.
public class MyService {
@Autowired
private RestTemplate restTemplate;
public void doSomething() {
// Ribbon resolves "service-provider" to an actual instance address.
Foo foo = restTemplate.getForObject("http://service-provider/query", Foo.class);
doWithFoo(foo);
}
}Enable load balancing with Feign
Feign is a declarative Java HTTP client that simplifies RESTful API calls. Use @EnableFeignClients and @FeignClient together to set up load-balanced calls.
Step 1: Enable Feign in your application class.
@SpringBootApplication
@EnableFeignClients
public class MyApplication {
// ...
}Step 2: Declare a Feign client interface.
The name attribute in @FeignClient specifies the service name registered with Nacos. Ribbon uses this name to look up available instances and distribute requests across them.
@FeignClient(name = "service-provider")
public interface EchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
String echo(@PathVariable("str") String str);
}Step 3: Inject the client and call its methods.
Calling echoService.echo("test") sends an HTTP GET request to http://service-provider/echo/test. Ribbon selects an instance automatically.
public class MyService {
@Autowired
private EchoService echoService;
public void doSomething() {
echoService.echo("test");
}
}Verify load balancing
Start service-consumer and at least two service-provider instances, then access the URL exposed by service-consumer repeatedly and check whether the requests are forwarded to different instances.
RestTemplate: Access
/echo-rest/rest-testmultiple times and confirm that successive requests are handled by differentservice-providerinstances.Feign: Access
/echo-feign/feign-testmultiple times and confirm the same distribution across instances.