Ribbon 负载均衡自定义算法

2021-03-07 17:31

阅读:448

标签: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++  reachableServers = lb.getReachableServers(); //还活着的服务
            List 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 =  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


评论


亲,登录后才可以留言!