当前位置:网站首页>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 an API interface SQL injection practice
- Attack and defense world misc practice area (GIF lift table ext3)
- 【VMware异常问题】问题分析&解决办法
- 7-3 construction hash table (PTA program design)
- sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
- msf生成payload大全
- Wei Shen of Peking University revealed the current situation: his class is not very good, and there are only 5 or 6 middle-term students left after leaving class
- 7-9 make house number 3.0 (PTA program design)
- Experiment 6 inheritance and polymorphism
- MSF generate payload Encyclopedia
猜你喜欢

Mixlab unbounded community white paper officially released

HackMyvm靶机系列(6)-videoclub

Xray and Burp linked Mining
![[VMware abnormal problems] problem analysis & Solutions](/img/64/f44864da600b61a1a646a5865a2083.jpg)
[VMware abnormal problems] problem analysis & Solutions

Middleware vulnerability recurrence Apache

Intranet information collection of Intranet penetration (5)

Hackmyvm target series (5) -warez

HackMyvm靶机系列(2)-warrior

Experiment 6 inheritance and polymorphism

Safe driving skills on ice and snow roads
随机推荐
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
Record once, modify password logic vulnerability actual combat
Hackmyvm target series (1) -webmaster
Experiment 6 inheritance and polymorphism
A complete collection of papers on text recognition
【MySQL数据库的学习】
Hackmyvm target series (2) -warrior
Hackmyvm target series (7) -tron
Nuxtjs quick start (nuxt2)
Matlab opens M file garbled solution
Strengthen basic learning records
. Net6: develop modern 3D industrial software based on WPF (2)
Hackmyvm Target Series (3) - vues
7-1 输出2到n之间的全部素数(PTA程序设计)
DVWA (5th week)
实验五 类和对象
【Numpy和Pytorch的数据处理】
攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)
Renforcer les dossiers de base de l'apprentissage
[MySQL table structure and integrity constraint modification (Alter)]