当前位置:网站首页>Ribbon负载均衡
Ribbon负载均衡
2022-07-26 22:08:00 【兮动人】
- 添加了@LoadBalanced注解,即可实现负载均衡功能,这是什么原理呢?
1. 负载均衡流程
SpringCloud底层其实是利用了一个名为Ribbon的组件,来实现负载均衡功能的。

发出的请求明明是http://userservice/user/1,怎么变成了http://localhost:8081的呢?

Ribbon的负载均衡规则是一个叫做
IRule的接口来定义的,每一个子接口都是一种规则:
2. 源码跟踪
为什么只输入了service名称就可以访问了呢?之前还要获取ip和端口。
显然有帮我们根据service名称,获取到了服务实例的ip和端口。它就是LoadBalancerInterceptor,这个类会在对RestTemplate的请求进行拦截,然后从Eureka根据服务id获取服务列表,随后利用负载均衡算法得到真实的服务地址信息,替换服务id。
- LoadBalancerIntercepor查看源码,

可以看到这里的intercept方法,拦截了用户的HttpRequest请求,然后做了几件事:
request.getURI():获取请求uri,本例中就是 http://user-service/user/8originalUri.getHost():获取uri路径的主机名,其实就是服务id,user-servicethis.loadBalancer.execute():处理服务id,和用户请求。
这里的this.loadBalancer是LoadBalancerClient类型
2、LoadBalancerClient
- 继续跟入execute方法:

代码是这样的:
- getLoadBalancer(serviceId):根据服务id获取ILoadBalancer,而ILoadBalancer会拿着服务id去eureka中获取服务列表并保存起来。
- getServer(loadBalancer):利用内置的负载均衡算法,从服务列表中选择一个。本例中,可以看到获取了8082端口的服务
放行后,再次访问并跟踪,发现获取的是8081:
实现了负载均衡
3、负载均衡策略IRule
在刚才的代码中,可以看到获取服务使通过一个getServer方法来做负载均衡:
继续跟入:
继续跟踪源码chooseServer方法,发现这么一段代码:
看看这个rule是谁:
这里的rule默认值是一个RoundRobinRule,看类的介绍:
这就是轮询
到这里,整个负载均衡的流程就清楚了。
3. 小结
SpringCloudRibbon的底层采用了一个拦截器,拦截了RestTemplate发出的请求,对地址做了修改。用一幅图来总结一下:
基本流程如下:
- 拦截的RestTemplate请求http://userservice/user/1
- RibbonLoadBalancerClient会从请求url中获取服务名称,也就是user-service
- DynamicServerListLoadBalancer根据user-service到eureka拉取服务列表
- eureka返回列表,localhost:8081、localhost:8082
- IRule利用内置负载均衡规则,从列表中选择一个,例如localhost:8081
- RibbonLoadBalancerClient修改请求地址,用localhost:8081替代userservice,得到http://localhost:8081/user/1,发起真实请求
4. 负载均衡策略
负载均衡的规则都定义在IRule接口中,而IRule有很多不同的实现类:
不同规则的含义如下:
| 内置负载均衡规则类 | 规则描述 |
|---|---|
| RoundRobinRule | 简单轮询服务列表来选择服务器。它是Ribbon默认的负载均衡规则。 |
| AvailabilityFilteringRule | 对以下两种服务器进行忽略: (1)在默认情况下,这台服务器如果3次连接失败,这台服务器就会被设置为“短路”状态。短路状态将持续30秒,如果再次连接失败,短路的持续时间就会几何级地增加。 (2)并发数过高的服务器。如果一个服务器的并发连接数过高,配置了AvailabilityFilteringRule规则的客户端也会将其忽略。并发连接数的上限,可以由客户端的..ActiveConnectionsLimit属性进行配置。 |
| WeightedResponseTimeRule | 为每一个服务器赋予一个权重值。服务器响应时间越长,这个服务器的权重就越小。这个规则会随机选择服务器,这个权重值会影响服务器的选择。 |
| ZoneAvoidanceRule | 以区域可用的服务器为基础进行服务器的选择。使用Zone对服务器进行分类,这个Zone可以理解为一个机房、一个机架等。而后再对Zone内的多个服务做轮询。 |
| BestAvailableRule | 忽略那些短路的服务器,并选择并发数较低的服务器。 |
| RandomRule | 随机选择一个可用的服务器。 |
| RetryRule | 重试机制的选择逻辑 |
- 默认的实现就是ZoneAvoidanceRule,是一种轮询方案
5. 自定义负载均衡策略
通过定义IRule实现可以修改负载均衡规则,有两种方式:
- 代码方式:在order-service中的OrderApplication类中,定义一个新的IRule:
@Bean
public IRule randomRule(){
return new RandomRule();
}
- 配置文件方式:在order-service的application.yml文件中,添加新的配置也可以修改规则:
userservice: # 给某个微服务配置负载均衡规则,这里是userservice服务
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # 负载均衡规则
注意,一般用默认的负载均衡规则,不做修改。
6. 饥饿加载
Ribbon默认是采用懒加载,即第一次访问时才会去创建LoadBalanceClient,请求时间会很长。
而饥饿加载则会在项目启动时创建,降低第一次访问的耗时,通过下面配置开启饥饿加载:
ribbon:
eager-load:
enabled: true
clients: userservice
边栏推荐
- Xu Li, CEO of Shangtang Technology: the company's valuation has exceeded $7billion, so we are not in a hurry to go public
- 证监会:同意传音控股科创板IPO注册
- Module 8 (message queue MySQL table storing message data)
- Interview Essentials
- 参数解析与跳石板
- SQL二次注入详解
- 纷享销客罗旭对话西门子王海滨:先进制造,要向数字化要效益
- Introduction to MySQL database
- 测试开发是开发吗?
- Summary of shell related commands
猜你喜欢

Module 8 (message queue MySQL table storing message data)

Embedded SIG | 分布式软总线

【HCIP】OSPF 关系建立

Spend 120 billion to build a "subway" to connect 4 trillion cities. What is Guangdong thinking?
![[hcip] OSPF route calculation](/img/1c/ee9eee2e723b850c401f7cddda1b27.png)
[hcip] OSPF route calculation

Making wechat robot with go (I) sending messages
![[IO Development Notes] smart cloud intelligent watering device practice (1) - Basic demo implementation](/img/db/122cc54ad8b5b06c885d7455ecb034.png)
[IO Development Notes] smart cloud intelligent watering device practice (1) - Basic demo implementation

数据库全栈工程师(DevDBOps)低首付、高回报,先就业后付款

Interview: your most impressive bug, for example

A chip company fell in round B
随机推荐
Mate30 series release: how far can Huawei go in reconstructing images?
Implementation of V-model syntax sugar
CSRC: agree to the IPO registration of Chuanyin holding science and Technology Innovation Board
芯鼎收购紫光控股!万业企业:全面转型集成电路!
Reinforcement learning weekly 55: lb-sgd, msp-drl & robust reinforcement learning against
"Mongodb" here are all the basic mongodb operations you want
Huawei atlas900 reveals the secret: it integrates thousands of shengteng 910 chips, and its computing power is comparable to 500000 PCs!
A chip company fell in round B
Embedded sig | distributed soft bus
Apifox--比 Postman 还好用的 API 测试工具
Mate30系列发布:华为的重构影像之路还能走多远?
各有一项指纹专利被宣告无效!汇顶与思立微均正式回应!
Implementation principle of semaphore in golang
Arduino实验一:双色灯实验
动态规划之线性DP
Incremental secure file system SFS based on C language design
[MySQL] - index principle and use
[hcip] OSPF special area, summary, certification
蔚来杯2022牛客暑期多校训练营2
kalibr标定realsenseD435i --多相机标定