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;
}
}
}
}