2024-01-13 10:19:30
浏览数 (3)
增加自定义负载均衡实现类
代码语言:javascript
复制package com.learning.springcloud.ribbon.rule;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class CustomRule extends AbstractLoadBalancerRule {
@Override
public void initWithNiwsConfig(IClientConfig iClientConfig) {
}
@Override
public Server choose(Object key) {
ILoadBalancer loadBalancer = this.getLoadBalancer();
// 获取当前请求的服务的实例
List<Server> reachableServers = loadBalancer.getReachableServers();
int random = ThreadLocalRandom.current().nextInt(reachableServers.size());
Server server = reachableServers.get(random);
return server;
}
}