springcloud feign定制负载均衡

背景:开发,测试环境都使用了一个注册中心,所以在开发,测试环境不想进行负载均衡

原因:自己本地调试方便, 可能会负载到同事的机器上

 

注册中心使用nacos

实现原理:获取本机ip,从服务列表中获取相同ip的服务实例进行调用

 

配置步骤:

//1: 自定义负载策略

@Slf4j
public class MyRibbonRule extends AbstractLoadBalancerRule implements InitializingBean {

    @Autowired
    InetUtils inetUtils;

    private String localIp = "";

    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {
    }

    @Override
    public Server choose(Object key) {
        Server server = null;
        try {

            ILoadBalancer lb = getLoadBalancer();

            int count = 0;
            while (server == null && count++ < 10) {
                List 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;
                }

                for (Server server1 : reachableServers) {
                    NacosServer nacosServer = (NacosServer) server1;
                    Instance instance = nacosServer.getInstance();
                    String ip = instance.getIp();
                    if (ip.equals(localIp)) {

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

                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);
            }
        } catch (Exception e) {
            log.error("rule error", e);
            throw new CommonException("rule error");
        }

        return server;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        localIp = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
        log.info("本地ip地址:{}", localIp);
    }
}

//:2:Ribbon配置文件,配置我们自己的负载策略
@Configuration
public class RibbonConfig {

    @Profile({"dev", "test"})
    @Bean
    public IRule ribbonRule() {
        return new MyRibbonRule();
    }
}
 

 


// 3:RibbonClients配置,xxxx为你自己的服务
@RibbonClients({
        @RibbonClient(value = "xxxx", configuration = RibbonConfig.class),
        @RibbonClient(value = "xxxx", configuration = RibbonConfig.class)
})
public class GateWayServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(GateWayServerApplication.class, args);
    }
}
 

你可能感兴趣的:(问题记录)