Ribbon interpretation

Ribbon Introduction

  • RibbonIs Netflixreleased open source project, the main function is to provide the client software load balancing algorithm is based HTTPand TCPclient-side load balancing tool
  • springCloudTo Ribbondo a second package, it allows us to use the RestTemplateservice request, automatically converted to the client load balancing service calls
  • Ribbon Support a variety of load balancing algorithms, and also support custom load balancing algorithms
  • RibbonIt is just a tool framework, relatively small, springCloudand very convenient to use after it is packaged. It does APInot need to be deployed independently like the service registry, configuration center, and gateway. It Ribbononly needs to be used directly in the code.

Server-side load balancing

Load balancing is one of our important methods for dealing with high concurrency, alleviating network pressure and server expansion, but in general, what we call load balancing usually refers to server-side load balancing, and server-side load balancing is divided into two types. One is hardware load balancing, and the other is software load balancing

  • Hardware load balancing is mainly through the installation of dedicated load balancing equipment in front of the server nodes, such as:F5
  • Software load balancing is mainly to install some software with load balancing function on the server to complete request distribution and achieve load balancing, such as:LVS 、 Nginx 、Haproxy

Whether it is hardware load balancing or software load balancing, its working principle is nothing more than the following picture

Insert picture description here

Client load balancing

The emergence of microservices provides another idea for the realization of load balancing: the function of load balancing is integrated into the service consumer in a library, instead of being provided by a designated load balancing device. This solution is called soft load balancing or client load balancing. Common such as: springCloudinRibbon

RibbonIt is based HTTPand TCPclient load balancer, when we will Ribbon, and Eurekawhen used together, Ribbonwill go to the Eurekaregistry to get the server list, and then reach the polling access to the role of load balancing, client load balancing heartbeat mechanism also needs to go Maintain the validity of the server list. Of course, this process needs to be completed with the service registry

In the springCloudmiddle, Ribbonmainly related to RestTemplatethe object and together use, Ribbonwill automate the configuration RestTemplateobject, @LoadBalancedopen RestTemplateload balancing when the calling object

Server load balancing VSclient load balancing

  • Server-side load balancing: The client first sends the request to the load balancing server, and then the load balancing server uses the load balancing algorithm to select one of the many available servers to process the request
  • Client load balancing: The client itself maintains a list of available server addresses, selects a server that will be used to process the request through the load balancing algorithm before sending the request, and then directly sends the request to the server

Ribbon Example of implementing client load balancing

Service provider Controllerlayer

@RestController
@RequestMapping("/api/v1/msg")
public class MessageController {
    
    
 
	@Value("${server.port}")
	private String port;
 
	@GetMapping("/get")
	public String getMsg() {
    
    
		return "This message is sent from port: " + port;
	}
}

Service consumers introduce dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>  

Consumer service integration RestTemplateandRibbon

@Bean
@LoadBalanced
public RestTemplate restTemplate(){
    
    
    return new RestTemplate();
}

Use Ribbonclient load balancing

method one

Class to start springinjecting a vessel with @LoadBalancedannotations RestTemplateofbean

@SpringBootApplication
@EnableEurekaClient
public class MessageCenterApplication {
    
    
 
	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
    
     
		return new RestTemplate();
	}
 
	public static void main(String[] args) {
    
    
		SpringApplication.run(MessageCenterApplication.class, args);
	}
}
@RestController
@RequestMapping("/api/v1/center")
public class MessageCenterController {
    
    
 
	@Autowired
	private RestTemplate restTemplate;
 
	@GetMapping("/msg/get")
	public Object getMsg() {
    
    
		String msg = restTemplate.getForObject("http://message-service/api/v1/msg/get", String.class);
		return msg;
	}
}

Way two

Direct use of LoadBalancerClientload balancing policy to obtain service address available, and then to request

@RestController
@RequestMapping("/api/v1/center")
public class MessageCenterController {
    
    
 
	@Autowired
	private LoadBalancerClient loadBalancer;
 
	@GetMapping("/msg/get")
	public Object getMsg() {
    
    
		ServiceInstance instance = loadBalancer.choose("message-service");
		URI url = URI.create(String.format("http://%s:%s/api/v1/msg/get", instance.getHost(), instance.getPort()));
		RestTemplate restTemplate = new RestTemplate();
		String msg = restTemplate.getForObject(url, String.class);
		return msg;
	}
}

After the application is started, the address is requested three times in a row http://localhost:8781/api/v1/center/msg/get, and the returned result is shown in the figure

Insert picture description here

Ribbon Load balancing strategy

Strategy name description
RandomRule Random strategy Randomly select server
RoundRobinRule Polling strategy Select the server in order (the default strategy of ribbon)
RetryRule Retry strategy In a configuration period, when the server selection is unsuccessful, try to select an available server all the time
BestAvailableRule Minimum concurrency strategy Check the servers one by one, if the server circuit breaker is open, ignore it, and then select the server with the lowest concurrent link
AvailabilityFilteringRule Available filtering strategies Filter out the servers that have been failing and are marked as circuit tripped, and filter out those servers with high concurrent connections (active connections exceed the configured threshold)
ResponseTimeWeightedRule Response time weighted strategy The weight is assigned according to the response time of the server. The longer the response time, the lower the weight and the lower the probability of being selected. The shorter the response time, the higher the weight, and the higher the probability of being selected. This strategy is very appropriate. It combines various factors, such as: network, disk, io, etc., which directly affect the response time
ZoneAvoidanceRule Regional weighting strategy Comprehensively judge the performance of the area where the server is located, and the availability of the server, poll to select the server and determine whether the operating performance of an AWS Zone is available, and remove all servers in the unavailable zone

Ribbon Retry mechanism settings

ribbonAchieving load balancing, if access to a service Aafter the node timeout is triggered ribbonretry mechanism

全局设置
ribbon:
    ReadTimeout: 6000
    ConnectTimeout: 6000
    OkToRetryOnAllOperations: true
    MaxAutoRetries: 1
    MaxAutoRetriesNextServer: 2
    
局部服务设置
service-id:                             # 服务ID
    ribbon:
        ConnectTimeout: 6000            # 毫秒  连接超时时间
        ReadTimeout: 6000               # 毫秒  逻辑处理超时时间
        OkToRetryOnAllOperations: true  # 是否对所有操作都进行重试
        MaxAutoRetries: 1               # 对当前实例的最大重试次数(请求服务超时6s则会再请求一次)    
        MaxAutoRetriesNextServer: 1     # 切换实例的最大重试次数(如果还失败就切换下个实例访问,切换一次)

Ribbonwork process

First: service discovery

Used to obtain the address list: When we Ribbonand Eurekawhen used together, Ribbonwill go to the Eurekaregistry to get the server service instance list . It can be static (provide a set of fixed addresses) or dynamic (check the address list regularly from the registry)

Second: Service selection

Service selection rule: According to the policy specified by the user, select a list address from its instance list. Which Ribbonprovides a number of strategies, such as polling, random, weighted in accordance with the response time and the like. After getting a specific service list address, Feigncomplete the service call by

Third: service monitoring

That is to remove invalid service instances at any time

Guess you like

Origin blog.csdn.net/weixin_38192427/article/details/115191963