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 ,负载规则就更改为随机了。

最新文章

  1. gulp错误GulpUglifyError: unable to minify JavaScript解决
  2. IIS 输入地址 目录浏览
  3. C++对于大型图片的加载缩放尝试
  4. input 获取当前id,name
  5. Entity Framework 学习总结之一:ADO.NET 实体框架概述
  6. web.config的数据库连接字符串进行加密
  7. Java之--Java基础知识
  8. 实战项目:通过当当API将订单抓取到SAP(一)
  9. Asp.net 主题
  10. PHP配置xdebug
  11. 与我一起extjs5(04--MVVM简要说明财产)
  12. sql语句,实践证明了某种情况下not in的效率高于not exists
  13. 【POJ 2176】Folding
  14. Spring Security(二十四):6.6 The Authentication Manager and the Namespace
  15. POJ 1860 Currency Exchange(如何Bellman-Ford算法判断图中是否存在正环)
  16. JS实现下拉单的二级联动
  17. TFS2018环境搭建一单实例安装(适用于小型团队)
  18. 如何用Python为你的邮箱加油?还有这种操作!
  19. gVim 中文内容显示为乱码的解决办法
  20. Windows与VMware中的CentOS系统互通访问

热门文章

  1. 网络基础和 TCP、IP 协议
  2. nginx配置奇怪问题记录
  3. 简述vue的双向绑定原理
  4. P1004 方格取数——奇怪的dp
  5. 区间dp(低价回文)
  6. 【线型DP】【LCS】UVA_10635 Prince and Princess
  7. Mister B and PR Shifts,题解
  8. 记录一下安装hexo的过程
  9. response对象乱码--解决
  10. celery 基础教程(二):简单实例