SpringCloud: ribbon自定义负载均衡策略

package cn.edu.tju.config;

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 MyRule extends AbstractLoadBalancerRule {
    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {

    }

    @Override
    public Server choose(Object key) {
        List reachableServers = this.getLoadBalancer().getReachableServers();
        int selected = ThreadLocalRandom.current().nextInt(reachableServers.size());


        return reachableServers.get(selected);
    }
}

你可能感兴趣的:(SpringCloud,spring,cloud,ribbon,负载均衡)