/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.mycompany.ribbon.test; import com.google.common.collect.Lists; import com.netflix.client.DefaultLoadBalancerRetryHandler; import com.netflix.client.RetryHandler; 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; import java.util.List; public class URLConnectionLoadBalancer { private final ILoadBalancer loadBalancer; // retry handler that does not retry on same server, but on a different server private final RetryHandler retryHandler = new DefaultLoadBalancerRetryHandler(0, 1, true); public URLConnectionLoadBalancer(List<Server> serverList) { loadBalancer = LoadBalancerBuilder.newBuilder() .withRule(new RoundRobinRule()) .withPing(new PingUrl()) .buildFixedServerListLoadBalancer(serverList); } public String call(final String path) throws Exception { return LoadBalancerCommand.<String>builder() .withLoadBalancer(loadBalancer) .withRetryHandler(retryHandler) .build() .submit(new ServerOperation<String>() { @Override public Observable<String> 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 { URLConnectionLoadBalancer urlLoadBalancer = new URLConnectionLoadBalancer(Lists.newArrayList( new Server("www.baidu.com", 80), new Server("www.taobao.com", 80), new Server("www.processon.com", 80))); for (int i = 0; i < 6; i++) { System.out.println(urlLoadBalancer.call("/")); } System.out.println("=== Load balancer stats ==="); System.out.println(urlLoadBalancer.getLoadBalancerStats()); } }
来源:oschina
链接:https://my.oschina.net/u/3847203/blog/4304762