码迷,mamicode.com
首页 > 编程语言 > 详细

Ribbon 负载均衡自定义算法

时间:2021-01-12 11:20:12      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:abstract   for   随机   override   rri   ble   ace   run   netflix   

自定义负载均衡

IRule接口介绍

  • com.netflix.loadbalancer.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:会先轮询获取服务,如果获取失败,则会在一定的时间内进行重试

开始

    1. 在主启动类上级,创建配置文件,不能在同级,同级会被扫描到
      技术图片
    1. 在启动类上使用注解,指向自定义的算法配置类
@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);
    }
}
  • 3.代码
  • 和Ribbon同包下的配置类
@Configuration
public class ZZNumberConfig {
    @Bean
    public NumberRule numberRule(){
        return new NumberRule();
    }
}
  • 自定义算法的负载均衡实现类,参考自RoundRobinRule
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++ < 10) {
            List<Server> reachableServers = lb.getReachableServers(); //还活着的服务
            List<Server> allServers = lb.getAllServers(); //所有得服务
            int upCount = reachableServers.size(); //活着的数量
            int serverCount = allServers.size(); //所有服务的数量

            if ((upCount == 0) || (serverCount == 0)) {
                log.warn("No up servers available from load balancer: " + lb);
                return null;
            }

            //总数小于还活着的数量
           if(total < 3){
               server = reachableServers.get(cunnentIndex);
               total++;
           }else {
               total =0;
               cunnentIndex++;
               if(cunnentIndex >=  reachableServers.size()){
                   cunnentIndex = 0;
               }
               server = reachableServers.get(cunnentIndex);
           }

            if (server == null) {
                /* Transient. */
                Thread.yield();
                continue;
            }

            if (server.isAlive() && (server.isReadyToServe())) {
                return (server);
            }

            // Next.
            server = null;
        }

        if (count >= 10) {
            log.warn("No available alive servers after 10 tries from load balancer: "
                    + lb);
        }
        return server;
    }



    @Override
    public Server choose(Object key) {
        return choose(getLoadBalancer(), key);
    }

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {
    }
}

Ribbon 负载均衡自定义算法

标签:abstract   for   随机   override   rri   ble   ace   run   netflix   

原文地址:https://www.cnblogs.com/xiaominaaaa/p/14259080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!