Ribbon负载均衡独立使用,不依赖springCloud

1.依赖环境:

  • springboot
  • pom文件依赖
        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
            2.2.9.RELEASE
        

java 测试类



import com.netflix.client.DefaultLoadBalancerRetryHandler;
import com.netflix.client.RetryHandler;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.config.ConfigurationManager;
import com.netflix.loadbalancer.*;
import com.netflix.loadbalancer.reactive.LoadBalancerCommand;
import com.netflix.loadbalancer.reactive.ServerOperation;
import rx.Observable;

import java.net.HttpURLConnection;
import java.net.URL;

public class URLConnectionLoadBalancer2 {

    private final ILoadBalancer loadBalancer;
    private final RetryHandler retryHandler = new DefaultLoadBalancerRetryHandler(0, 1, true);

    public URLConnectionLoadBalancer2(String serviceId, String namespace) {

        // 设置服务列表
        ConfigurationManager.getConfigInstance().setProperty(serviceId + "." + namespace + ".listOfServers", "taobao.com:80,baidu99:8081");
        // 打印服务列表
        System.out.println("服务列表:" + ConfigurationManager.getConfigInstance().getProperty(serviceId + "." + namespace + ".listOfServers"));

        //本示例略,先默认使用其默认负载均衡策略规则
        // ConfigurationManager.getConfigInstance().setProperty("default" + ".ribbon.NFLoadBalancerRuleClassName", RetryRule.class.getName());

        DefaultClientConfigImpl config = new DefaultClientConfigImpl(namespace);
        config.loadProperties(serviceId);

        loadBalancer = LoadBalancerBuilder.newBuilder()
                .withClientConfig(config)
                .withRule(new RoundRobinRule())
                .withPing(new PingUrl())
                .buildDynamicServerListLoadBalancer();
//                .buildFixedServerListLoadBalancer(serverList);
    }

    public String call(final String path) throws Exception {
        return LoadBalancerCommand.builder()
                .withLoadBalancer(loadBalancer)
                .withRetryHandler(retryHandler)
                .build()
                .submit(new ServerOperation() {
                    @Override
                    public Observable call(Server server) {
                        URL url;
                        try {
                            url = new URL("http://" + server.getHost() + ":" + server.getPort() + path);
                            System.out.println("http://" + server.getHost() + ":" + server.getPort() + path);
                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                            return Observable.just(conn.getResponseMessage());
                        } catch (Exception e) {
                            return Observable.error(e);
                        }
                    }
                }).toBlocking().first();
    }

    public LoadBalancerStats getLoadBalancerStats() {
        return ((BaseLoadBalancer) loadBalancer).getLoadBalancerStats();
    }

    public static void main(String[] args) throws Exception {
        String serviceId = "web";
        URLConnectionLoadBalancer2 urlLoadBalancer = new URLConnectionLoadBalancer2(serviceId,"ribbon");
        for (int i = 0; i < 6; i++) {
            System.out.println("第" + (i + 1) + "次成功:" + urlLoadBalancer.call("/"));
        }
        
        // 动态更新serverList      
        ConfigurationManager.getConfigInstance().setProperty(serviceId + "." + namespace + ".listOfServers", "www.baidu.com:80");
        loadBalancer.updateListOfServers();// 动态更新serverList

        for (int i = 0; i < 6; i++) {
            System.out.println("第" + (i + 1) + "次成功:" + urlLoadBalancer.call("/"));
        }
        System.out.println("=== Load balancer stats ===");
        System.out.println(urlLoadBalancer.getLoadBalancerStats());
    }
}

你可能感兴趣的:(Ribbon负载均衡独立使用,不依赖springCloud)