Spring Cloud Ribbon
Spring Cloud Ribbon is a client load balancing tool based on Netflix Ribbon. It is a client load balancer based on HTTP and TCP. RibbonServerList can be configured on the client to configure the server list for polling access to balance load.
When used in combination with Ribbon and Eureka ribbonServerList will be DiscoveryEnabledNIWSServerList rewritten, extended to get the service instance list from Eureka registration center. It also replaces IPing with NIWSDiscoveryPing, which delegates responsibility to Eureka to determine if the server is started.
When Ribbon is used with Consul, ribbonServerList is extended by ConsulServerList to obtain a list of service instances from Consul. ConsulPing is also an implementation of the IPing interface.
When we use Spring Cloud Ribbon, whether it is in conjunction with Eureka Consul or Consul, we will load the configuration mentioned above through automatic configuration when we introduce Spring Cloud Eureka or Spring Cloud Consul dependencies. So we can quickly load balance calls between services in Spring Cloud.
Let’s take a look at some examples of how Spring Cloud Ribbon can be used to invoke services and load balance clients.
We will use the previously built Eureka-Server as the service registry and Eureka-Client as the service provider as the base. As for consumers based on Spring Cloud Ribbon, we can make simple changes according to eureka-Consumer. The specific steps are as follows:
Copy a service consumer project based on Eureka-Consumer, named eureka-consumer-ribbon. Add the following dependencies to pom.xml:
. Org. Springframework. Cloud spring – the cloud – starter – ribbon modify the application of the main class. Add @loadBalanced annotation to RestTemplate: @EnableDiscoveryClient @SpringBootApplication public class Application { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }} Modify Controller. Instead of using LoadBalancerClient to select instances and concatenate urls, use the RestTemplate to initiate requests directly. @RestController public class DcController { @Autowired RestTemplate restTemplate; @GetMapping(“/consumer”) public String dc() { return restTemplate.getForObject( String.class); }} As you can see here, in addition to removing the logic originally associated with LoadBalancerClient, our first URL parameter has something special for use with RestTemplate. The requested host location is not in the form of a specific IP address and port, but in the form of a service name. So why can such a request be called successfully? Because Spring Cloud Ribbon has an interceptor, it can automatically select the service instance and replace the service name with the IP address and port to complete the invocation of the service interface.
Minglisoft. Cn/honghu/tech…