当前位置:网站首页>Load balancing ribbon of microservices
Load balancing ribbon of microservices
2022-07-06 14:14:00 【Bulst】
List of articles
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
边栏推荐
- Record once, modify password logic vulnerability actual combat
- HackMyvm靶机系列(7)-Tron
- Hackmyvm target series (7) -tron
- 实验七 常用类的使用
- C language file operation
- HackMyvm靶机系列(3)-visions
- [paper reproduction] cyclegan (based on pytorch framework) {unfinished}
- Record an API interface SQL injection practice
- WEB漏洞-文件操作之文件包含漏洞
- 链队实现(C语言)
猜你喜欢

7-5 staircase upgrade (PTA program design)

Hackmyvm target series (6) -videoclub

HackMyvm靶机系列(4)-vulny

On the idea of vulnerability discovery

SRC mining ideas and methods

captcha-killer验证码识别插件

Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)

xray与burp联动 挖掘

搭建域环境(win)

强化学习基础记录
随机推荐
Force deduction 152 question multiplier maximum subarray
Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning
内网渗透之内网信息收集(四)
Experiment 4 array
7-3 构造散列表(PTA程序设计)
实验五 类和对象
Which is more advantageous in short-term or long-term spot gold investment?
搭建域环境(win)
内网渗透之内网信息收集(一)
链队实现(C语言)
sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
Spot gold prices rose amid volatility, and the rise in U.S. prices is likely to become the key to the future
力扣152题乘数最大子数组
msf生成payload大全
Data mining - a discussion on sample imbalance in classification problems
Renforcer les dossiers de base de l'apprentissage
Attach the simplified sample database to the SQLSERVER database instance
7-11 mechanic mustadio (PTA program design)
Harmonyos JS demo application development
记一次edu,SQL注入实战