IRule 默认提供有7种方式,使用轮询方式

如何自定义

1:主启动类加@RibbonClient

@RibbonClient(name="微服务名", configuration=MySelfRule.class)

也就是说MySelfRule.java不能和@SpringBootApplication注释的类在同一包下.

MySelfRule.java

  1. package com.atguigu.myrule;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. import com.netflix.loadbalancer.IRule;
  7. import com.netflix.loadbalancer.RoundRobinRule;
  8.  
  9. @Configuration
  10. public class MySelfRule
  11. {
  12. @Bean
  13. public IRule myRule()
  14. {
  15. //return new RandomRule();// Ribbon默认是轮询,我自定义为随机
  16. //return new RoundRobinRule();// Ribbon默认是轮询,我自定义为随机
  17.  
  18. return new RandomRule_ZY();// 我自定义为每台机器5次
  19. }
  20. }
  1. package com.atguigu.myrule;
  2.  
  3. import java.util.List;
  4.  
  5. import com.netflix.client.config.IClientConfig;
  6. import com.netflix.loadbalancer.AbstractLoadBalancerRule;
  7. import com.netflix.loadbalancer.ILoadBalancer;
  8. import com.netflix.loadbalancer.Server;
  9.  
  10. public class RandomRule_ZY extends AbstractLoadBalancerRule
  11. {
  12.  
  13. // total = 0 // 当total==5以后,我们指针才能往下走,
  14. // index = 0 // 当前对外提供服务的服务器地址,
  15. // total需要重新置为零,但是已经达到过一个5次,我们的index = 1
  16. // 分析:我们5次,但是微服务只有8001 8002 8003 三台,OK?
  17. //
  18.  
  19. private int total = ; // 总共被调用的次数,目前要求每台被调用5次
  20. private int currentIndex = ; // 当前提供服务的机器号
  21.  
  22. public Server choose(ILoadBalancer lb, Object key)
  23. {
  24. if (lb == null) {
  25. return null;
  26. }
  27. Server server = null;
  28.  
  29. while (server == null) {
  30. if (Thread.interrupted()) {
  31. return null;
  32. }
  33. List<Server> upList = lb.getReachableServers();
  34. List<Server> allList = lb.getAllServers();
  35.  
  36. int serverCount = allList.size();
  37. if (serverCount == ) {
  38. /*
  39. * No servers. End regardless of pass, because subsequent passes only get more
  40. * restrictive.
  41. */
  42. return null;
  43. }
  44.  
  45. // int index = rand.nextInt(serverCount);// java.util.Random().nextInt(3);
  46. // server = upList.get(index);
  47.  
  48. // private int total = 0; // 总共被调用的次数,目前要求每台被调用5次
  49. // private int currentIndex = 0; // 当前提供服务的机器号
  50. if(total < )
  51. {
  52. server = upList.get(currentIndex);
  53. total++;
  54. }else {
  55. total = ;
  56. currentIndex++;
  57. if(currentIndex >= upList.size())
  58. {
  59. currentIndex = ;
  60. }
  61. }
  62.  
  63. if (server == null) {
  64. /*
  65. * The only time this should happen is if the server list were somehow trimmed.
  66. * This is a transient condition. Retry after yielding.
  67. */
  68. Thread.yield();
  69. continue;
  70. }
  71.  
  72. if (server.isAlive()) {
  73. return (server);
  74. }
  75.  
  76. // Shouldn't actually happen.. but must be transient or a bug.
  77. server = null;
  78. Thread.yield();
  79. }
  80.  
  81. return server;
  82.  
  83. }
  84.  
  85. @Override
  86. public Server choose(Object key)
  87. {
  88. return choose(getLoadBalancer(), key);
  89. }
  90.  
  91. @Override
  92. public void initWithNiwsConfig(IClientConfig clientConfig)
  93. {
  94. // TODO Auto-generated method stub
  95.  
  96. }
  97.  
  98. }

Srping cloud Ribbon 自定义负载均衡的更多相关文章

  1. spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 当超大并发量并发访问一个服务接口时,服务器会崩溃 ,不仅导致这个接口无法 ...

  2. Spring Cloud Ribbon——客户端负载均衡

    一.负载均衡负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意思 ...

  3. Spring Cloud Ribbon客户端负载均衡(四)

    序言 Ribbon 是一个客户端负载均衡器(Nginx 为服务端负载均衡),它赋予了应用一些支配 HTTP 与 TCP 行为的能力,可以得知,这里的客户端负载均衡也是进程内负载均衡的一种.它在 Spr ...

  4. spring cloud --- Ribbon 客户端负载均衡 + RestTemplate ---心得【无熔断器】

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 了解了 eureka 服务注册与发现 的3大角色 ,会使用RestTem ...

  5. Spring Cloud Gateway Ribbon 自定义负载均衡

    在微服务开发中,使用Spring Cloud Gateway做为服务的网关,网关后面启动N个业务服务.但是有这样一个需求,同一个用户的操作,有时候需要保证顺序性,如果使用默认负载均衡策略,同一个用户的 ...

  6. SpringCloud的Ribbon自定义负载均衡算法

    1.Ribbon默认使用RoundRobinRule策略轮询选择server 策略名 策略声明 策略描述 实现说明 BestAvailableRule public class BestAvailab ...

  7. 笔记:Spring Cloud Ribbon 客户端负载均衡

    Spring Cloud Ribbon 是一个基于 HTTP 和 TCP 的客户端负载均衡工具,基于 Netflix Ribbon 实现,通过Spring Cloud 的封装,可以让我们轻松的将面向服 ...

  8. Spring Cloud Ribbon 客户端负载均衡 4.3

      在分布式架构中,服务器端负载均衡通常是由Nginx实现分发请求的,而客户端的同一个实例部署在多个应用上时,也需要实现负载均衡.那么Spring Cloud中是否提供了这种负载均衡的功能呢?答案是肯 ...

  9. Ribbon自定义负载均衡策略,在网关实现类似Ip_hash的负载均衡,ribbon给单个服务配置属性

    背景: 我需要在网关实现一种功能,某个用户的请求永远打在后台指定的服务,也就是根据ip地址进行负载均衡 原理: 在ribbon的配置类下: 那我们自己创建一个IRule的实现类,模仿ZoneAvoid ...

随机推荐

  1. 廖雪峰Java2面向对象编程-1面向对象-1面向对象基础

    1.对象的概念 面向对象编程:Object-Oriented Programming 对现实世界建立计算机模型的一种编程方法. 现实世界 计算机模型 Java代码 人 类/class class Pe ...

  2. 阿里云线上ROS静态路由转发,有大坑。

    原因见上去,阿里云不支持VPC中转流量,VPC1和VPC2都在国内,VPC3在香港,如果按阿里云的做法,必须付费2次国际隧道的钱,才可以实现三个VPC互通.明显很浪费钱. 所以我们只能在三个VPC,各 ...

  3. [UE4]使用材质将图片变成黑白

    拖动到材质界面不放,会自动切换到材质界面: 拖放到视图窗口,放开鼠标,就会自动生成一个“Texture Sample”节点 图片材质使用方法跟直接使用图片素材一样:

  4. 0001 - Spring MVC中的注解

    1.概述 Spring MVC框架提供了功能强大的注解,大大简化了代码开发的同时也提升了程序的可扩展性 2.注解 2.1.@RequestMapping Spring MVC通过@RequestMap ...

  5. mybatis的插件,挺好支持下

    利用 Mybatis-generator自动生成代码http://www.cnblogs.com/yjmyzz/p/4210554.html Mybatis 通用 Mapper3 https://gi ...

  6. 新型DenseBody框架:一张照片获得3D人体信息

    来自云从科技和上海交通大学的研究者近期提出一种新型框架 DenseBody,可直接从一张彩色照片中获取 3D 人体姿势和形状.该研究设计了一种高效的 3D 人体姿势和形状表示,无需中间表示和任务,端到 ...

  7. RxJava响应式编程,入门的HelloWorld;

    RxJava核心就是异步,它也被称之为响应式编程:最大的优势就是随着程序逻辑变得越来越复杂,它依然能够保持简洁. Rxjava真的是让人又爱又恨,因为它的线程切换和链式调用真的很好用,但是入门却有点难 ...

  8. fastle

    昨晚梦见日本和中国打仗, 发过来了很多导弹, 但是飞行速度很慢, 我还能看到上面的辐射三角号 之后我就趴在地上躲导弹 然后感觉身体被蒸发, 意识逐渐模糊, 就醒了 attack大爷的休闲(修仙)题 感 ...

  9. jsfiddle将demo设置为public公开的

    jsfiddle的demo虽然可以通过链接分享给所有人,但是进入个人主页是没有的,需要将项目设置为公开public的 根据提示,打开demo项目页==>左侧菜单==>填写标题和描述==&g ...

  10. git push declined due to email privacy restrictions 解决方法

    push declined due to email privacy restrictions 今天push的时候发现了这个问题无法push 解决: 进入github主页==>setting = ...