Ribbon负载均衡
![image-20240119103956254](https://img-blog.csdnimg.cn/img_convert/5dae826f0355798332ca74cb8dea1314.png)
01 负载均衡原理
负载均衡流程
![image-20240119104420499](https://img-blog.csdnimg.cn/img_convert/eb8dfd2737801b0e196541f6683a1a25.png)
详细图
![image-20240119111754585](https://img-blog.csdnimg.cn/img_convert/96a5e9bb1667d08a1c9c60d223b06a89.png)
源代码示例
![image-20240119104758678](https://img-blog.csdnimg.cn/img_convert/d9d28f29df130c34622d8d829033900b.png)
拦截由LoadBalancerInterceptor来完成
![image-20240119105124200](https://img-blog.csdnimg.cn/img_convert/da4e061cdff1fe0706245b4518c2d828.png)
![image-20240119110427381](https://img-blog.csdnimg.cn/img_convert/c185476c7448373116601448bb806578.png)
![image-20240119110510448](https://img-blog.csdnimg.cn/img_convert/42dcf7c4a8eac9ea2cb1f7796c659d6d.png)
![image-20240119110332680](https://img-blog.csdnimg.cn/img_convert/6f6921c9becbbff297ad9b45ef57f969.png)
02 负载均衡策略
![image-20240119113042567](https://img-blog.csdnimg.cn/img_convert/f69532ff280a2cda1bc77830ff742f1c.png)
IRule常见实现类
![image-20240119135109837](https://img-blog.csdnimg.cn/img_convert/702a017ea29257be9a0b5c6387966390.png)
通过定义IRule实现可以修改负载均衡规则,有以下两种方式
![image-20240119140537616](https://img-blog.csdnimg.cn/img_convert/8cd9862c2c20bc617d9145911dd8056e.png)
结果如下(两种方法结果都类似)
![image-20240119140158281](https://img-blog.csdnimg.cn/img_convert/896b7addac966f522d976b8f2e741247.png)
![image-20240119140237139](https://img-blog.csdnimg.cn/img_convert/a80f1db5966714e7505c80b8d56b7775.png)
03 懒加载改成饥饿加载
第一次请求的时间远远长于后续请求的时间,why??
答:因为Ribbon默认采用懒加载,即第一次访问服务时才会去创建LoadBalanceClient,故第一次请求时间长
![image-20240119143124736](https://img-blog.csdnimg.cn/img_convert/a7325dc9ece043d7771364a46ffd00db.png)
![image-20240119143205909](https://img-blog.csdnimg.cn/img_convert/66d734af76458a217ec57af2e936a308.png)
如何解决?答:饥饿加载。在项目启动时创建,降低第一次访问的耗时
![image-20240119144428730](https://img-blog.csdnimg.cn/img_convert/57def412ae090e8b5033c0e38d827486.png)
Ribbon小结
![image-20240119145644754](https://img-blog.csdnimg.cn/img_convert/a437a3f8529a4abee850993ef1d2cde3.png)