How to set the value of loadBalancerKey in ribbon or Feign

363 views Asked by At

In Spring Cloud, I'm using Ribbon or Feign as the client API, and we have a server cluster registered in Eureka. In my case, I want to use the specific key to determine which server I want to call, eg: server-A has two instances, localhost:8001 and localhost:8002, and I want to call 8001 by setting the loadBalancerKey to 8001, after that I can write my customized balance rule class to choose which server I can call like below:

public class MyConfigBalanceRule extends ClientConfigEnabledRoundRobinRule {
    @Override
    public Server choose(Object key) {
        List<Server> serverList = getLoadBalancer().getAllServers();
        for (Server srv : serverList) {
            if (srv.getPort == key) {
                return srv;
            }
        }
    }
}
0

There are 0 answers