当前位置:网站首页>Load balancing ribbon of microservices

Load balancing ribbon of microservices

2022-07-06 14:14:00 Bulst

One 、Ribbon summary

1、 What is? Ribbon

Ribbon yes Netflixfa Released a load balancer , It helps to control HTTP and TCP Client behavior . stay SpringCloud in ,Eureka General coordination Ribbon To use ,Ribbon It provides the function of client load balancing ,Ribbon Use from Eureka Service information read in , When calling the service provided by the service node , The load will be reasonable .
stay SpringCloud The registry and Ribbon In combination with ,Ribbon Automatically get the list information of service providers from the registry , And based on the built-in load balancing algorithm , Request service .

2、 effect

(1) The service call
be based on Ribbon Implement service calls , It is composed of all service lists pulled ( service name - Request path ) The mapping relationship . With the help of RestTemplate Finally call
(2) Load balancing
When there are multiple service providers ,Ribbon The service address to be called can be automatically selected according to the load balancing algorithm

3、 be based on Ribbon Realize order call commodity service

Whether it's based on Eureka Our registry is still based on Consul Registration Center for ,SpringCloudRibbon Unified packaging , the
For service calls , The way of both is the same .

Coordinate dependence

stay springcloud The services provided are found jar It contains Ribbon Dependence . So there is no need to import any additional coordinates

engineered

(1) Service providers

modify shop_service_product Module ProductController#findById() The method is as follows

@Value("${server.port}")
private String port;
@Value("${spring.cloud.client.ip-address}")
private String ip;
@GetMapping("/{id}")
public Product findById(@PathVariable Long id) {
    
    Product product = productService.findById(id);
    // Set port 
    product.setProductDesc(" call shop-service-product service ,ip:"+ip+", Service provider side 
 mouth :"+port);
    return product; } 

(2) Serving consumers

Modify service consumer shop_service_order Startup class in module OrderApplication , Creating RestTemplate Method @LoadBalanced annotation

/** *  be based on Ribbon Service invocation and load balancing  */
@LoadBalanced
@Bean
public RestTemplate getRestTemplate() {
    
    return new RestTemplate();
}

stay shop_service_order Of OrderController Add ordering method in , And use RestTemplate Complete the service call

@Autowired
private RestTemplate restTemplate;
@GetMapping("/buy/{id}")
public Product order() {
    
// adopt restTemplate Call commodity micro service 
//Product product = 
restTemplate.getForObject("http://127.0.0.1:9002/product/1", Product.class);
Product product = restTemplate.getForObject("http://shop-serviceproduct/product/1", Product.class);
return product;
 }

(3) test

Request in browser http://localhost:9001/order/buy/1 The display effect is as follows , Can already be served in the order micro service
Call the commodity micro service in the form of name to obtain data

Two 、Ribbon Advanced

1、 Overview of load balancing

When building a website , If a single node web The service performance and reliability cannot meet the requirements ; Or when using Internet services , Often worried about being broken , If you are not careful, you will open the external network port , Usually, adding load balancing at this time can effectively solve the service problem .

Load balancing is a basic network service , The principle is through the load balancing service running in front , Calculate according to the specified load balancing
Law , Allocate traffic to the back-end service cluster , So as to provide the system with the ability of parallel expansion .

The application scenarios of load balancing include traffic packets 、 Forwarding rules and back-end services , Because the service has intranet and Intranet cases 、 Health examination and other work
can , It can effectively provide the security and availability of the system .

Server side load balancing
First send the request to the load balancing server or software , Then through the load balancing algorithm , Select one of multiple servers for access
ask ; That is, load balancing algorithm is allocated on the server side

Client load balancing
The client will have a list of server addresses , Select a server by load balancing algorithm before sending the request , Then visit , This is client load balancing ; That is, load balancing algorithm is allocated on the client side

2、 be based on Ribbon Load balancing

Build multiple service instances

modify shop_service_product Of application.yml The configuration file , has profiles Configure multiple instances in the form of

spring:
 profiles: product1
 application:
   name: shop-service-product
 datasource:
   driver-class-name: com.mysql.jdbc.Driver
   url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
   username: root
   password: 111111
 jpa:
   database: MySQL
   show-sql: true
   open-in-view: true
 cloud:
   consul: #consul Related configuration 
     host: localhost #ConsulServer Request address 
     port: 8500 #ConsulServer port 
     discovery:
        # example ID
       instance-id: ${
    spring.application.name}-1
        # Turn on ip Address registration 
       prefer-ip-address: true
        # Instance request ip
       ip-address: ${
    spring.cloud.client.ip-address}
server:
 port: 9002
---
spring:
 profiles: product2
 application:
   name: shop-service-product
 datasource:
   driver-class-name: com.mysql.jdbc.Driver
   url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
   username: root
   password: 111111
 jpa:
   database: MySQL
   show-sql: true
   open-in-view: true
 cloud:
   consul: #consul Related configuration 
     host: localhost #ConsulServer Request address 
     port: 8500 #ConsulServer port 
     discovery:
        # example ID
       instance-id: ${
    spring.application.name}-2
        # Turn on ip Address registration 
       prefer-ip-address: true
        # Instance request ip
       ip-address: ${
    spring.cloud.client.ip-address}
server:
 port: 9004

Start the server twice to verify the effect , Check the two consoles and find that the goods and services have been called in the polling mode

3、 Load balancing strategy

Ribbon Built in a variety of load balancing strategies , The internal top-level interface responsible for complex balance is com.netflix.loadbalancer.IRule , The implementation is as follows :

  • com.netflix.loadbalancer.RoundRobinRule : Load balancing by polling .
  • com.netflix.loadbalancer.RandomRule : Random strategy
  • com.netflix.loadbalancer.RetryRule : Retrying strategy .
  • com.netflix.loadbalancer.WeightedResponseTimeRule : Weight strategy . Accounting calculates the weight of each service , The higher the probability of being called .
  • com.netflix.loadbalancer.BestAvailableRule : The best strategy . Traverse all service instances , Filter out fault instances , And return the instance with the smallest number of requests .
  • com.netflix.loadbalancer.AvailabilityFilteringRule : Filter strategy available . Filter out service instances with failures and requests exceeding the threshold , Then call from the remaining strength .

In serving consumers application.yml Modify the load balancing policy in the configuration file

## The name of the micro service to be called 
shop-service-product:
 ribbon:
   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

Strategy choice :
1、 If each machine has the same configuration , It is recommended not to modify the policy ( recommend )
2、 If the configuration of some machines is strong , It can be changed to WeightedResponseTimeRule

原网站

版权声明
本文为[Bulst]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131410386692.html