Ribbon 负载规则替换
2024-09-01 12:51:23
1 添加规则类:
注意: 官方文档明确给出了警告:
这个自定义配置类不能放在 @ComponentScan 所扫描的当前包下以及子包下,否则自定义的配置类就会被所有的 Ribbon 客户端所共享,达不到特殊化定制的目的了。
package com.atguigu.myrule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* 自定义负载均衡规则类
*/
@Configuration
public class MySelfRule {
@Bean
public IRule myRule(){
return new RandomRule();
}
}
2 主启动类添加 @RibbonClient
在启动该微服务的时候就能去加载我们的自定义 Ribbon 配置类,从而使配置生效
package com.atguigu.springcloud;
import com.atguigu.myrule.MySelfRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient; @SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "CLOUD-PROVIDER-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
3 测试
多次刷新,是随机出现 serverPort ,负载规则就更改为随机了。
最新文章
- gulp错误GulpUglifyError: unable to minify JavaScript解决
- IIS 输入地址 目录浏览
- C++对于大型图片的加载缩放尝试
- input 获取当前id,name
- Entity Framework 学习总结之一:ADO.NET 实体框架概述
- web.config的数据库连接字符串进行加密
- Java之--Java基础知识
- 实战项目:通过当当API将订单抓取到SAP(一)
- Asp.net 主题
- PHP配置xdebug
- 与我一起extjs5(04--MVVM简要说明财产)
- sql语句,实践证明了某种情况下not in的效率高于not exists
- 【POJ 2176】Folding
- Spring Security(二十四):6.6 The Authentication Manager and the Namespace
- POJ 1860 Currency Exchange(如何Bellman-Ford算法判断图中是否存在正环)
- JS实现下拉单的二级联动
- TFS2018环境搭建一单实例安装(适用于小型团队)
- 如何用Python为你的邮箱加油?还有这种操作!
- gVim 中文内容显示为乱码的解决办法
- Windows与VMware中的CentOS系统互通访问