zoukankan      html  css  js  c++  java
  • Srping cloud Ribbon 自定义负载均衡

    IRule 默认提供有7种方式,使用轮询方式

    如何自定义

    1:主启动类加@RibbonClient

    @RibbonClient(name="微服务名", configuration=MySelfRule.class)

    也就是说MySelfRule.java不能和@SpringBootApplication注释的类在同一包下.

    MySelfRule.java

    package com.atguigu.myrule;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import com.netflix.loadbalancer.IRule;
    import com.netflix.loadbalancer.RoundRobinRule;
    
    @Configuration
    public class MySelfRule
    {
        @Bean
        public IRule myRule()
        {
            //return new RandomRule();// Ribbon默认是轮询,我自定义为随机
            //return new RoundRobinRule();// Ribbon默认是轮询,我自定义为随机
            
            return new RandomRule_ZY();// 我自定义为每台机器5次
        }
    }
    package com.atguigu.myrule;
    
    import java.util.List;
    
    import com.netflix.client.config.IClientConfig;
    import com.netflix.loadbalancer.AbstractLoadBalancerRule;
    import com.netflix.loadbalancer.ILoadBalancer;
    import com.netflix.loadbalancer.Server;
    
    public class RandomRule_ZY extends AbstractLoadBalancerRule
    {
    
        // total = 0 // 当total==5以后,我们指针才能往下走,
        // index = 0 // 当前对外提供服务的服务器地址,
        // total需要重新置为零,但是已经达到过一个5次,我们的index = 1
        // 分析:我们5次,但是微服务只有8001 8002 8003 三台,OK?
        // 
        
        
        private int total = 0;             // 总共被调用的次数,目前要求每台被调用5次
        private int currentIndex = 0;    // 当前提供服务的机器号
    
        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 = rand.nextInt(serverCount);// java.util.Random().nextInt(3);
    //            server = upList.get(index);
    
                
    //            private int total = 0;             // 总共被调用的次数,目前要求每台被调用5次
    //            private int currentIndex = 0;    // 当前提供服务的机器号
                if(total < 5)
                {
                    server = upList.get(currentIndex);
                    total++;
                }else {
                    total = 0;
                    currentIndex++;
                    if(currentIndex >= upList.size())
                    {
                      currentIndex = 0;
                    }
                }            
                
                
                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;
    
        }
    
        @Override
        public Server choose(Object key)
        {
            return choose(getLoadBalancer(), key);
        }
    
        @Override
        public void initWithNiwsConfig(IClientConfig clientConfig)
        {
            // TODO Auto-generated method stub
    
        }
    
    }
  • 相关阅读:
    opensuse tumbleweed中安装code
    树莓派中将caplock映射为esc键
    记录一次奇怪但是很有意义的程序编译警告
    新树莓派系统安装ROS记录
    程序的深挖
    intle官方手册下载
    slax linux的定制
    angular4 *ngFor获取index
    axios post传参后台无法接收问题
    AMD、CMD、CommonJs和 ES6对比
  • 原文地址:https://www.cnblogs.com/eason-d/p/9484603.html
Copyright © 2011-2022 走看看