当前位置:网站首页>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
边栏推荐
- 内网渗透之内网信息收集(一)
- 网络层—简单的arp断网
- 强化学习基础记录
- Network layer - simple ARP disconnection
- 记一次,修改密码逻辑漏洞实战
- SRC mining ideas and methods
- Interpretation of iterator related "itertools" module usage
- List and data frame of R language experiment III
- How to understand the difference between technical thinking and business thinking in Bi?
- Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning
猜你喜欢
随机推荐
xray與burp聯動 挖掘
"Gold, silver and four" job hopping needs to be cautious. Can an article solve the interview?
An unhandled exception occurred when C connected to SQL Server: system Argumentexception: "keyword not supported:" integrated
搭建域环境(win)
. Net6: develop modern 3D industrial software based on WPF (2)
On the idea of vulnerability discovery
HackMyvm靶机系列(4)-vulny
Only 40% of the articles are original? Here comes the modification method
HackMyvm靶机系列(7)-Tron
What language should I learn from zero foundation. Suggestions
[insert, modify and delete data in the headsong educator data table]
7-3 构造散列表(PTA程序设计)
sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
HackMyvm靶机系列(1)-webmaster
7-4 hash table search (PTA program design)
Record an edu, SQL injection practice
攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)
【educoder数据库实验 索引】
7-7 7003 combination lock (PTA program design)
XSS unexpected event