Ribbon 负载均衡自定义算法
2021-03-07 17:31
标签:abstract for 随机 override rri ble ace run netflix Ribbon 负载均衡自定义算法 标签:abstract for 随机 override rri ble ace run netflix 原文地址:https://www.cnblogs.com/xiaominaaaa/p/14259080.html自定义负载均衡
IRule接口介绍
/**
* Interface that defines a "Rule" for a LoadBalancer. A Rule can be thought of
* as a Strategy for loadbalacing. Well known loadbalancing strategies include
* Round Robin, Response Time based etc.
*
* @author stonse
*
*/
public interface IRule{
/*
* choose one alive server from lb.allServers or
* lb.upServers according to key
*
* @return choosen Server object. NULL is returned if none
* server is available
*/
public Server choose(Object key);
public void setLoadBalancer(ILoadBalancer lb);
public ILoadBalancer getLoadBalancer();
}
AvailabilityFilteringRule: 先过滤掉跳闸,访问故障的服务,再对剩下的服务进行轮询
RoundRobinRule:轮询调度,默认的
RandomRule:随机调度
WeightedResponseTimeRule:根据权重调度
RetryRule:会先轮询获取服务,如果获取失败,则会在一定的时间内进行重试
开始
@SpringBootApplication
@EnableEurekaClient //为Eureka客户端
//微服务启动时就会去加载我们的Ribbon配置类
@RibbonClient(name = "provider-person-8001",configuration = NumberRule.class)
public class PersonConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(PersonConsumerApplication.class,args);
}
}
@Configuration
public class ZZNumberConfig {
@Bean
public NumberRule numberRule(){
return new NumberRule();
}
}
public class NumberRule extends AbstractLoadBalancerRule {
private static Logger log = LoggerFactory.getLogger(RoundRobinRule.class);
private int total = 0;//总数
private int cunnentIndex = 0;//当前索引,指向那个服务
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
log.warn("no load balancer");
return null;
}
Server server = null;
int count = 0;
while (server == null && count++ reachableServers = lb.getReachableServers(); //还活着的服务
List
上一篇:“菜鸟”的python之旅
下一篇:http与https协议整理