当前位置:网站首页>Ribbon修改默认负载均衡策略
Ribbon修改默认负载均衡策略
2022-07-18 02:43:00 【胡尚】
第一种方式 使用配置类
如果这个配置类使用@Configuration注解并写在主启动类能够进行包扫描的范围中,那么调用其他微服务,一律使用这一种负载均衡算法。当然也可以写在主启动所在包的外面
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2kLJy6r5-1658024849726)(E:\Java笔记\image\springcloud_alibaba\image-20220609112753434.png)]](/img/c5/49be06bc3ef24e5383fabf424684c8.png)
配置类的内容如下:
package com.hs.rule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** * @Description: 将ribbon默认的负载均衡方式改为随机的 * @Author 胡尚 * @Date: 2022/6/9 11:10 * @Version 1.0 */
@Configuration
public class MyRuleConfig {
@Bean
public IRule iRule(){
return new RandomRule();
}
}
然后在主启动类中指定调用某些服务的时候使用我们配置的负载均衡算法
@SpringBootApplication
// 如果只有一个服务就可以直接使用@RibbonClient注解,而不需要@RibbonClients。
// name是写服务提供方的服务名 configuration是指定我们上面创建的配置类
@RibbonClients(value = {
@RibbonClient(name = "stock-service", configuration = MyRuleConfig.class)
})
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
第二种方式 使用yaml配置
修改的这种负载均衡算法是使用nacos设置的权重方式
# 使用配置文件的方式修改ribbon默认的负载均衡算法
stock-service:
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IduBkqFI-1658024849728)(E:\Java笔记\image\springcloud_alibaba\image-20220609114317412.png)]](/img/0b/b969a0a9837e65eb7ce6df13717b2d.png)
接下来进行测试,首先在nacos管理平台上面修改订单服务stock的权重
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wXBBSlIi-1658024849731)(E:\Java笔记\image\springcloud_alibaba\image-20220609114435159.png)]](/img/a1/b13c3f68dd9f753981ed256daf1397.png)
然后再进行调用,就可以发现测试效果了。
如果要使用自定义的负载均算法,一般就自己创建一个类,然后继承AbstractLoadBalancerRule抽象类,然后写相应的负载均衡逻辑。最后再使用上面的两种方法,要么在配置类中创建该对象并注册进容器,要么在yml文件中指定全路径
边栏推荐
- groovy学习
- 线段树练题.1
- JMeter 21 day clock in day13
- 劲旅环境深交所上市:市值50亿 于晓霞家族色彩浓厚
- [foundation of deep learning] how to understand the channel in convolutional neural network
- WCF——实现带认证的服务及常见报错
- Clouds want clothes, flowers want looks, spring breeze blows the sill, Revlon (romantic code implementation)
- JMeter 21 day clock in Day12
- 【VScode输出为乱码】解决方法
- Date and time function of MySQL function summary
猜你喜欢

项目报错“BeanInitializationException: com.xxxxx.xx.dao.data.Dao can‘t get a sessionFactory“

阿里云(OSS)文件上传和删除

Jinlv environment listed on Shenzhen Stock Exchange: market value of 5billion Yu Xiaoxia family has a strong color

使用深度學習制作機器人大腦圖紙

Word文档中封面的下划线如何精确对齐

个人 IP 实验室周复盘 · 第 19 期

Gesture recognition dataset: Jester dataset decompression

Flask模板

QA机器人第二节——召回

CANoe:. What is a vmodule file
随机推荐
使用go json path的demo
07.二叉树的高级应用
[foundation of deep learning] how to understand the channel in convolutional neural network
RT-thread培训学习和心得(二)
劲旅环境深交所上市:市值50亿 于晓霞家族色彩浓厚
The fourth day of MATLAB learning (decision statement)
torch dist分布式数据汇总
在go中使用yaml
groovy学习
Entropy technology passed the registration: the annual revenue was 1.955 billion, and the book balance of accounts receivable was 290million
Vs2019 list control control self drawing routine inherits CListCtrl class onnmcustomdraw redrawing
UVA10341 Solve It
揪心!56岁老父亲为了给孩子筹学费,高温工作9小时去世
云服务ECS/RDS:搭建云服务器ECS管理linux,搭建云数据库管理创建RDS MySQL;
Aspose.OCR 22.6 for .NET//Aspose.OCR
After the media forwarding function is enabled in Vos, calculate the bandwidth occupied by several common codes
个人 IP 实验室周复盘 · 第 19 期
【VScode输出为乱码】解决方法
向数据库表中插入中文数据报错“1366 (HY000): Incorrect string value: ‘\xE5\x90\x95\xE5‘ for column ‘name‘ at row 1“
Expressions régulières MySQL