Ribbon
Introduction
Ribbon
IsNetflix
released open source project, the main function is to provide the client software load balancing algorithm is basedHTTP
andTCP
client-side load balancing toolspringCloud
ToRibbon
do a second package, it allows us to use theRestTemplate
service request, automatically converted to the client load balancing service callsRibbon
Support a variety of load balancing algorithms, and also support custom load balancing algorithmsRibbon
It is just a tool framework, relatively small,springCloud
and very convenient to use after it is packaged. It doesAPI
not need to be deployed independently like the service registry, configuration center, and gateway. ItRibbon
only 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
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: springCloud
inRibbon
Ribbon
It is based HTTP
and TCP
client load balancer, when we will Ribbon
, and Eureka
when used together, Ribbon
will go to the Eureka
registry 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 springCloud
middle, Ribbon
mainly related to RestTemplate
the object and together use, Ribbon
will automate the configuration RestTemplate
object, @LoadBalanced
open RestTemplate
load balancing when the calling object
Server load balancing VS
client 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 Controller
layer
@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 RestTemplate
andRibbon
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
Use Ribbon
client load balancing
method one
Class to start spring
injecting a vessel with @LoadBalanced
annotations RestTemplate
ofbean
@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 LoadBalancerClient
load 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
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
ribbon
Achieving load balancing, if access to a service A
after the node timeout is triggered ribbon
retry 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 # 切换实例的最大重试次数(如果还失败就切换下个实例访问,切换一次)
Ribbon
work process
First: service discovery
Used to obtain the address list: When we Ribbon
and Eureka
when used together, Ribbon
will go to the Eureka
registry 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 Ribbon
provides 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, Feign
complete the service call by
Third: service monitoring
That is to remove invalid service instances at any time