自定义FeignLoadBalancer

自定义FeignLoadBalancer

此功能主要用于本地服务路由规则改造
需要根据实际请求觉得客户端可用节点

配置类

package com.wmang.sevice.client.seviceclient.config;

import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: wmang
 * @Date: 2020/12/2 00:13
 * @Description:
 */
@Configuration
public class FeignAutoConfig {
    @Bean
	@Scope("prototype")
    public IRule getRule(){
        return new CustomRule();
    }
}

重写选定规则

package com.wmang.sevice.client.seviceclient.config;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;
import lombok.extern.slf4j.Slf4j;
import org.omg.PortableServer.THREAD_POLICY_ID;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Author: wmang
 * @Date: 2020/12/2 00:15
 * @Description:
 */
@Slf4j
public class CustomRule extends AbstractLoadBalancerRule {


    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {

    }

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

    public Server choose(ILoadBalancer loadBalancer, Object key) {
        if (null == loadBalancer) {
            log.error("loadBalancer is null");
            return null;
        }


        List<Server> upServer = loadBalancer.getReachableServers();
        List<Server> allServer = loadBalancer.getAllServers();
        int upServerCount = upServer.size();
        int allServerCount = allServer.size();
        if (upServerCount == 0 || allServerCount == 0) {
            log.error("loadBalancer not has available server");
            return null;
        }

        Server server = null;

        int total = 0;
        int currentIndex = 0;

        while (server == null) {
            if (Thread.interrupted()) {
                return null;
            }
            //通过负载均衡来计算获取server
            if (total < 5) {
                server = upServer.get(currentIndex);
                total++;
            } else {
                total = 0;
                currentIndex++;
                if (currentIndex >= upServerCount) {
                    currentIndex = 0;
                }
            }
            if (null == server) {
                Thread.yield();
                continue;
            }

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

            server = null;
            Thread.yield();
        }
        //正常情况下,每次应该都能选择一个up实例
        return server;
    }


    /**
     * 获取一个随机数
     *
     * @param serverCount
     * @return
     */
    protected int chooseRandomInt(int serverCount) {
        return ThreadLocalRandom.current().nextInt(serverCount);
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值