首页 > 编程语言 >Ribbon:自定义负载均衡算法

Ribbon:自定义负载均衡算法

时间:2022-08-24 21:11:18浏览次数:49  
标签:负载 return 自定义 int server serverCount null Ribbon currentIndex

在springcloud同级,与启动类所在包不同级创建myrule包,写MyWTYRule配置

 

 

Spring Cloud还允许您通过使用@RibbonClient声明其他配置(位于RibbonClientConfiguration之上)来完全控制客户端。例:

@Configuration
@RibbonClient(name = "foo", configuration = FooConfiguration.class)
public class TestConfiguration {
}

在这种情况下,客户端由RibbonClientConfiguration中已经存在的组件与FooConfiguration中的任何组件组成(后者通常会覆盖前者)。

注意:
警告 FooConfiguration必须是@Configuration,但请注意,它不在主应用程序上下文的@ComponentScan中,否则将由所有@RibbonClients共享。如果您使用@ComponentScan(或@SpringBootApplication),则需要采取措施避免包含(例如将其放在一个单独的,不重叠的包中,或者指定要在@ComponentScan)。

 

再写自定义负载均衡配置RandRule.java

 1 package com.company.myrule;
 2 
 3 
 4 import com.netflix.client.config.IClientConfig;
 5 import com.netflix.loadbalancer.AbstractLoadBalancerRule;
 6 import com.netflix.loadbalancer.ILoadBalancer;
 7 import com.netflix.loadbalancer.Server;
 8 
 9 import java.util.List;
10 
11 
12 public class RandRule extends AbstractLoadBalancerRule {
13 
14 
15     //每个服务,访问5次后换下一个服务(3个)
16 
17     private int total = 0; //被调用的次数
18     private int currentIndex = 0; //当前是谁在提供服务
19     public Server choose(ILoadBalancer lb, Object key) {
20         System.out.println("=========================================");
21         if (lb == null) {
22             return null;
23         }
24         Server server = null;
25 
26         while (server == null) {
27             if (Thread.interrupted()) {
28                 return null;
29             }
30             List<Server> upList = lb.getReachableServers(); //获得还活着的服务
31             List<Server> allList = lb.getAllServers(); //获取全部服务
32 
33             int serverCount = allList.size();
34             if (serverCount == 0) {
35 
36                 return null;
37             }
38 
39             //int index = chooseRandomInt(serverCount);
40             //server = upList.get(index);
41 
42             if (total<5){
43                 server = upList.get(currentIndex);
44                 total++;
45                 System.out.println("<5"+total+":"+currentIndex);
46             }else {
47                 total=0;
48                 currentIndex++;
49                 if (currentIndex>upList.size()-1){
50                     currentIndex = 0;
51                     System.out.println(">size"+total+":"+currentIndex);
52                 }
53                 server = upList.get(currentIndex);
54                 System.out.println(">5"+total+":"+currentIndex);
55             }
56 
57             if (server == null) {
58 
59                 Thread.yield();
60                 continue;
61             }
62 
63             if (server.isAlive()) {
64                 return (server);
65             }
66 
67 
68             server = null;
69             Thread.yield();
70         }
71 
72         return server;
73 
74     }
75 
76     //protected int chooseRandomInt(int serverCount) {
77     //    return ThreadLocalRandom.current().nextInt(serverCount);
78     //}
79 
80     @Override
81     public Server choose(Object key) {
82         return choose(getLoadBalancer(), key);
83     }
84 
85     @Override
86     public void initWithNiwsConfig(IClientConfig clientConfig) {
87         // TODO Auto-generated method stub
88 
89     }
90 }

主启动类添加注解@RibbonClient(name="XXXX",configuration=XXXRule.class)

 

 

标签:负载,return,自定义,int,server,serverCount,null,Ribbon,currentIndex
From: https://www.cnblogs.com/doremi429/p/16622032.html

相关文章

  • zabbix自定义监控项
    zabbix自定义监控项zabbix报错排查#1.检查端口[root@zabbix~]#telnet172.16.1.510050#2.服务端是否能获取到客户端的监控数据[root@zabbix~]#yuminstall-......
  • Ribbon作用
    1.作用用来解析域名,当你的同一种服务有多个微服务时,这时不能通过ip进行访问所有的微服务了,这时需要将域名修改为在Nacos中注册的名称,这样就能使用轮询的方法来进行调用微......
  • MySQL自定义监控
    zabbix自定义监控项zabbix拍错排查#检查端口[root@zabbix6~]#telnet172.16.1.7210050#服务端是否获取到客户端的监控数据[root@zabbix~]#yuminstall-yzab......
  • Ribbon:使用Ribbon实现负载均衡
      1.新建两个服务提供者Moudle:springcloud-provider-dept-8003、springcloud-provider-dept-80022.参照springcloud-provider-dept-8001依次为另外两个Moudle添加po......
  • tomcat自定义错误页面
    tomcat自定义错误页面 当我们访问tomcat的一个不存在的页面,返回错误信息如下: 这样的界面直接暴露给用户并不友好,有时候还不安全,因此一般需要修改默认的错误......
  • 【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
    问题描述在AppServiceforWindows的环境中,当前只提供了PHP7.4版本的选择情况下,如何实现自定义PHPRuntime的版本呢?如 PHPVersion8.1.9?当AppService创建号值后......
  • 自定义音量键
    Windows10上,音量加减只能通过相应的音量键,如果不习惯原生的按键想要修改成其他按键呢?解决办法就是手动添加注册表项,步骤只有简单的2步:打开注册表,定位到:HKEY_LOCAL_MACH......
  • 三种分页方式-JWT原理-Django中快速使用JWT-修改返回格式-JWT的验证-自定义用户签发To
    三种分页方式#什么样接口要分页----》获取所有#三种分页方式---》继承GenericAPIView,ListModelMixin-list方法---》分页的使用 page.pyfromrest_framework.p......
  • 自定义频率-权限 频率执行源码分析-全局异常处理-自动生成接口文档-RBAC
    自定义频率类 #自定义的逻辑1)取出访问者ip{192.168.1.12:[访问时间3,访问时间2,访问时间1],192.168.1.12:[],192.168.1.14:[]}2)判断当前ip不在访问字典里,......
  • Banner自定义
    Banner1.简介SpringBootLogo以及版本信息:${spring-boot.version}、${spring-boot.formatted-version},参考banner.txt文件;Banner大全地址:https://www.bootschool......