项目地址:

github地址

添加自定义算法的步骤

1.主启动类添加注解: @RibbonClient(name="DEBUGGGCLOUD-DEPT",configuration=MySelfRule.class)

此处的MySelfRule是自定义的算法规则的配置类,示例代码如下:

package com.cyjz.irule;import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 作者 ZYL* 功能描述 : 自定义配置类Ribbon* 日期 2019/12/13 10:32  * 参数 null* 返回值 */
@Configuration
public class ZYLRule {@Beanpublic IRule myRule(){return new RandomRule_DIY();}
}

上面的 RandomRule_DIY()的示例代码如下:实现的功能是

问题:依旧轮询策略,但是加上新需求,每个服务器要求被调用5次。也即
以前是每台机器一次,现在是每台机器5次

package com.cyjz.irule;import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;import java.util.List;
import java.util.Random;public class RandomRule_DIY extends AbstractLoadBalancerRule{//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
////total = 0 //当total = 5以后,我们的指针才能往下走//index = 0 //当前对外提供服务的服务器地址//total需要重新置为零,但是已经达到过一个5次,我们的Index = 1//分析:我们5次,但是微服务只有8001,8002,8003三台//index到达3就要拉回来private int total = 0;private int currentIndex = 0;public Server choose(ILoadBalancer lb, Object key) {if (lb == null) {return null;} else {Server server = null;while(server == null) {if (Thread.interrupted()) {return null;}List<Server> upList = lb.getReachableServers();List<Server> allList = lb.getAllServers();int serverCount = allList.size();if (serverCount == 0) {return null;}if(total < 5){server = upList.get(currentIndex);total ++;}else{total = 0;currentIndex ++;if(currentIndex >= upList.size()){currentIndex = 0;}}if (server == null) {Thread.yield();} else {if (server.isAlive()) {return server;}server = null;Thread.yield();}}return server;}}public Server choose(Object key) {return this.choose(this.getLoadBalancer(), key);}public void initWithNiwsConfig(IClientConfig clientConfig) {}
}

2.注意:

官方文档明确给出了警告:
这个自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,
否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,也就是说
我们达不到特殊化定制的目的了。

3.自定义算法的步骤:

1.从github上面获取随机算法的源代码:https://github.com/Netflix/ribbon/blob/master/ribbon-loadbalancer/src/main/java/com/netflix/loadbalancer/RandomRule.java

随机算法的源代码如下:

/*** Copyright 2013 Netflix, Inc.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.**/
package com.netflix.loadbalancer;import com.netflix.client.config.IClientConfig;import java.util.List;
import java.util.concurrent.ThreadLocalRandom;/*** A loadbalacing strategy that randomly distributes traffic amongst existing* servers.* * @author stonse* */
public class RandomRule extends AbstractLoadBalancerRule {/*** Randomly choose from all living servers*/@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")public Server choose(ILoadBalancer lb, Object key) {if (lb == null) {return null;}Server server = null;while (server == null) {if (Thread.interrupted()) {return null;}List<Server> upList = lb.getReachableServers();List<Server> allList = lb.getAllServers();int serverCount = allList.size();if (serverCount == 0) {/** No servers. End regardless of pass, because subsequent passes* only get more restrictive.*/return null;}int index = chooseRandomInt(serverCount);server = upList.get(index);if (server == null) {/** The only time this should happen is if the server list were* somehow trimmed. This is a transient condition. Retry after* yielding.*/Thread.yield();continue;}if (server.isAlive()) {return (server);}// Shouldn't actually happen.. but must be transient or a bug.server = null;Thread.yield();}return server;}protected int chooseRandomInt(int serverCount) {return ThreadLocalRandom.current().nextInt(serverCount);}@Overridepublic Server choose(Object key) {return choose(getLoadBalancer(), key);}
}

2.参考源码修改为我们需求要求的RandomRule_DIY.java,示例代码上面有显是

配置完成,可以测试

7.Springcloud的Ribbon的自定义算法实现相关推荐

  1. SpringCloud学习 Ribbon实现自定义负载均衡

    ribbon实现负载均衡 ribbon中所有的loadbalancer都是通过继承AbstractLoalBalancerRule来实现负载均衡规则的实现 公司项目中使用的zookeeper注册节点地 ...

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

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

  3. SpringCloud的Ribbon负载均衡

    Spring Cloud Ribbon相关学习: 简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具. 简单的说,Ribbon是Netflix ...

  4. SpringCloud[04]Ribbon负载均衡服务调用

    文章目录 Ribbon负载均衡服务调用 1. 概述 1. Ribbon是什么 2. Ribbon能做什么 2. Ribbon负载均衡演示 1. 架构说明 2. Ribbon的POM依赖 3. Rest ...

  5. SpringCloud之Ribbon简单入门

    提出疑问 ① 如何在配置Eureka Client注册中心时不去硬编码Eureka Server的地址? ② 在微服务不同模块间进行通信时,如何不去硬编码服务提供者的地址? ③ 当部署多个相同微服务时 ...

  6. SpringCloud(7)Ribbon 与负载均衡

    Ribbon SpringCloud Ribbon 是基于NetFlix Ribbon实现的一套客户端负载均衡的工具 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负 ...

  7. Ribbon负载均衡 算法

    1.Ribbon 简介 Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP客户端的行为.为Ribbon配置服务提供者地址列表后,Ribbon就可基于某种负载均衡算法,自动地帮助 ...

  8. SpringCloud系列-Ribbon的基本应用

    一 Ribbon简介 Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为.为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务 ...

  9. 算法高级(17)-SpringCloud中的负载均衡算法

    前面讲的负载均衡技术实际上都是服务端负载均衡,一个请求需要被发送到哪台服务器做出响应,是由我们的服务器决定的.而在SpringCloud中,采用的缺是客户端负载均衡技术.那么客户端负载均衡的优势到底在 ...

最新文章

  1. ON REG EXPRESSION.SYNTAX
  2. CSP:CSP认证考试:202109-1(数组推导)满分答案,Java版
  3. list筛选数据_Power Query如何对于各类混合格式的数据展开?
  4. Collections.sort()泛型集合排序的使用,和自定义类实现Comparable<T>接口重写compareTo(T o)方法完成Collections.sort()排序,以及自定义排序规则
  5. NSUserDefaults
  6. 互联网 | 逻辑上的黑话才是真正的花里胡哨
  7. java中自定义泛型类_java 自定义一个泛型类
  8. SQL Server中的MTVF和CE模型变化
  9. JavaScript学习笔记 -- ES6学习(三) 变量的解构赋值
  10. 深度剖析Java数据结构之表(三)——ArrayList泛型类的实现
  11. JDK8新特性-Lambda表达式查找
  12. 快码!数据可视化大屏设计必备步骤
  13. [网络安全自学篇] 九十二.《Windows黑客编程技术详解》之病毒启动技术创建进程API、突破SESSION0隔离、内存加载详解(3)
  14. python 线性插值处理_详解Python实现线性插值法
  15. 【转载】应聘总经理的答卷,供大家打分
  16. 【1076】正常血压
  17. COSO企业风险管理综合框架主要关注8大层面的企业风险
  18. 服务器自带的防篡改,防篡改系统
  19. 从《我不是潘金莲》谈程序员的核心竞争力 1
  20. 通过浏览器UA获取设备信息

热门文章

  1. “创业吃过饼,国企养过老” 架构师的头发不是一天掉完的!
  2. Mysql学习总结(84)—— Mysql的主从复制延迟问题总结
  3. Java基础学习总结(178)——时候替换你的logback/log4j1了,使用性能更强大配置更简单的log4j2
  4. Mysql学习总结(80)——统计数据库的总记录数和库中各个表的数据量
  5. Mysql学习总结(59)——数据库分库分表策略总结
  6. 系统架构师成长之路(三)
  7. 哪些是计算机应用基金,计算机应用的基金有哪些
  8. Elasticsearch 数据搜索篇
  9. 搭建直播源码与软件开发的注意事项
  10. 约束布局(ConstraintLayout)1.1.2 版本的新特性