1. What can you do?

Spring Cloud Ribbon is a client load balancing tool based on Netflix Ribbon. The Ribbon client component provides a series of complete configurations, such as timeout and retry. Load Balancer(LB) with all machine instances provided by the service, and the Ribbon automatically invokes these services based on certain rules (polling, random). The Ribbon can also implement our own load balancing algorithms;

Personal description: a user’s one request, multiple services can handle, the following problems solved;

Select which service to process;

If the efficient use of service resources;

How to ensure the response speed;

2. How to play it?

2.1 Project Construction

The spring-boot project is initialized

2.2 introduced ribbon

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> < version > 2.1.3. RELEASE < / version > < / dependency >Copy the code

2.3 configuration

For simplicity, the registry is not used here

server.port=18090
eat.ribbon.eureka.enabled=false
eat.ribbon.listOfServers=http://localhost:18091,http://localhost:18092
Copy the code

Note: the http://localhost:18091, http://localhost:18092 services can be created in-house or directly use the source code projects in see

Load balancing code for the EAT service

Core configuration: @loadBalanced

@Configuration public class RibbonConfig { @LoadBalanced @Bean public RestTemplate restTemplate(){ return new RestTemplate(); }}Copy the code

The controller USES

@RestController @RequestMapping("/eat") public class EatController { @Autowired private RestTemplate restTemplate; @PostMapping("/fruit1") public String eatFruit(){ return restTemplate.postForObject("http://eat/eat/fruit",null,String.class); }}Copy the code

For the restTemplate template, use http://service name /{service name}/ service name /{request URI}.

2.4 the use of

Access: localhost: 18090 / eat/fruit1

I eat fruit on 18091

I eat fruit on 18092

The default region load balancing policy takes effect.

2.5 Modifying the Default Load Balancing Policy

Custom policies: After one machine performs five polling operations, the next machine performs five polling operations in sequence.

Implement custom rules by inherits AbstractLoadBalancerRule

Public class YiRule extends AbstractLoadBalancerRule {public class YiRule extends AbstractLoadBalancerRule {private int total = 0; Private int currentIndex = 0; public Server choose(ILoadBalancer lb, Object key) { if (lb == null) { return null; } Server server = null; while (server == null) { if (Thread.interrupted()) { return null; } // Activate available services List<Server> upList = lb.getreachableservers (); if (total < 5) { server = upList.get(currentIndex); total++; } else { total = 0; currentIndex++; if (currentIndex >= upList.size()) { currentIndex = 0; } } if (server == null) { Thread.yield(); continue; } if (server.isAlive()) { return (server); } // Shouldn't actually happen.. but must be transient or a bug. server = null; Thread.yield(); } return server; } @Override public void initWithNiwsConfig(IClientConfig clientConfig) { } @Override public Server choose(Object key) { return choose(getLoadBalancer(), key); }}Copy the code

Configure this rule only for the EAT service

@RibbonClient(name = "eat",configuration = YiRule.class) @SpringBootApplication public class YiyiRibbonConsumerApplication { public static void main(String[] args) { SpringApplication.run(YiyiRibbonConsumerApplication.class, args); }}Copy the code

You can debug whether a custom policy is adopted.

3. Analyze the principle

To analyze the ribbon principle, we must look at the source code. Here we look at it from two perspectives.

Request link flow

From this point of view, we debug;

The debug purpose:

1) Find out when the two services were loaded; Localhost :1808x;

Ribbon Configuration Loading

LoadBalancerAutoConfiguration

Load balancing extended configuration in Spring-Cloud;

RibbonAutoConfiguration

Ribbon core configuration loading;

Configuration + sequence diagram

The following figure shows the request sequence diagram, combined with the configuration we can see:

The Ribbon is an implementation of the LoadbalancerClient provided by Spring-Cloud-common. LoadbalancerClient is the entrance to the Ribbon, and subsequent processing is the personalized implementation of the Ribbon.

see

Ribbon Source Code Analysis series

The use of the Ribbon and its core principles

Example code for this article – Github link