Java Code Examples for com.netflix.loadbalancer.BaseLoadBalancer#setServersList()

The following examples show how to use com.netflix.loadbalancer.BaseLoadBalancer#setServersList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RetryTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Before
public void beforeTest() {
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.NFLoadBalancerClassName", BaseLoadBalancer.class.getName());
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.client.NFLoadBalancerPingClassName", DummyPing.class.getName());

    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.ReadTimeout", "1000");
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon." + CommonClientConfigKey.ConnectTimeout, "500");
    ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon." + CommonClientConfigKey.OkToRetryOnAllOperations, "true");

    client = (RestClient) ClientFactory.getNamedClient("RetryTest");

    lb = (BaseLoadBalancer) client.getLoadBalancer();
    lb.setServersList(Lists.newArrayList(localServer));
    
    httpClient = NFHttpClientFactory.getNamedNFHttpClient("RetryTest");
    connectionPoolManager = (MonitoredConnectionManager) httpClient.getConnectionManager(); 
    
    client.setMaxAutoRetries(0);
    client.setMaxAutoRetriesNextServer(0);
    client.setOkToRetryOnAllOperations(false);
    lb.setServersList(Lists.newArrayList(localServer));
    // reset the server index
    lb.setRule(new AvailabilityFilteringRule());
    lb.getLoadBalancerStats().getSingleServerStat(localServer).clearSuccessiveConnectionFailureCount();
}
 
Example 2
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamWithLoadBalancer() throws Exception {
    // NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    // IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    IClientConfig config = IClientConfig.Builder.newBuilder().withRetryOnAllOperations(true)
            .withMaxAutoRetries(1)
            .withMaxAutoRetriesNextServer(3)
            .build();
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ServerSentEvent> lbObservables = (LoadBalancingHttpClient<ByteBuf, ServerSentEvent>) RibbonTransport.newSSEClient(lb, config);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/personStream");
    List<Person> result = Lists.newArrayList();
    Server goodServer = new Server("localhost:" + port);
    Server badServer = new Server("localhost:12245");
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    lb.setServersList(servers);
    result = getPersonListFromResponse(lbObservables.submit(request, null, null));
    assertEquals(EmbeddedResources.entityStream, result);
}
 
Example 3
Source File: HelloClientApplication.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
@Bean
public ILoadBalancer ribbonLoadBalancer() {
	// because of this, it doesn't use eureka to lookup the server,
	// but the classpath is tested
	BaseLoadBalancer balancer = new BaseLoadBalancer();
	balancer.setServersList(Arrays.asList(new Server("example.com", 80)));
	return balancer;
}
 
Example 4
Source File: UdpClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testUdpClientWithoutTimeout() throws Exception {
    int port = choosePort();
    UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 0).createServer();
    server.start();
    BaseLoadBalancer lb = new BaseLoadBalancer();
    lb.setServersList(Lists.newArrayList(new Server("localhost", port)));
    RxClient<DatagramPacket, DatagramPacket> client = RibbonTransport.newUdpClient(lb,
            DefaultClientConfigImpl.getClientConfigWithDefaultValues());
    try {
        String response = client.connect().flatMap(new Func1<ObservableConnection<DatagramPacket, DatagramPacket>,
                Observable<DatagramPacket>>() {
            @Override
            public Observable<DatagramPacket> call(ObservableConnection<DatagramPacket, DatagramPacket> connection) {
                connection.writeStringAndFlush("Is there anybody out there?");
                return connection.getInput();
            }
        }).take(1)
                .map(new Func1<DatagramPacket, String>() {
                    @Override
                    public String call(DatagramPacket datagramPacket) {
                        return datagramPacket.content().toString(Charset.defaultCharset());
                    }
                })
                .toBlocking()
                .first();
        assertEquals(HelloUdpServer.WELCOME_MSG, response);
    } finally {
        server.shutdown();
    }
}
 
Example 5
Source File: UdpClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testUdpClientTimeout() throws Exception {
    int port = choosePort();
    UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 5000).createServer();
    server.start();
    BaseLoadBalancer lb = new BaseLoadBalancer();
    Server myServer = new Server("localhost", port);
    lb.setServersList(Lists.newArrayList(myServer));
    MyUDPClient client = new MyUDPClient(lb, DefaultClientConfigImpl.getClientConfigWithDefaultValues());
    try {
        String response = client.submit("Is there anybody out there?")
                .map(new Func1<DatagramPacket, String>() {
                    @Override
                    public String call(DatagramPacket datagramPacket) {
                        return datagramPacket.content().toString(Charset.defaultCharset());
                    }
                })
                .toBlocking()
                .first();
        fail("Exception expected");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof TimeoutException);
        assertEquals(1, client.getLoadBalancerContext().getServerStats(myServer).getSuccessiveConnectionFailureCount());
    }
    finally {
        server.shutdown();
    }
}
 
Example 6
Source File: ServerListRefreshTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/**
 * This test ensures that when server list is refreshed in the load balancer, the set of servers
 * which equals to (oldList - newList) should be removed from the map of cached RxClient. Any server
 * that is not part of oldList should stay in the map.
 *
 * @throws IOException
 */
@Test
public void testServerListRefresh() throws IOException {
    String content = "Hello world";
    MockWebServer server1 = new MockWebServer();
    MockWebServer server2 = new MockWebServer();
    MockWebServer server3 = new MockWebServer();
    MockResponse mockResponse = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain")
            .setBody(content);
    server1.enqueue(mockResponse);
    server2.enqueue(mockResponse);
    server3.enqueue(mockResponse);
    server1.play();
    server2.play();
    server3.play();
    try {
        BaseLoadBalancer lb = new BaseLoadBalancer();
        List<Server> initialList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()));
        lb.setServersList(initialList);
        LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb);
        HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/");
        client.submit(request).toBlocking().last();
        client.submit(request).toBlocking().last();
        HttpClientRequest<ByteBuf> request2 = HttpClientRequest.createGet("http://localhost:" + server3.getPort());
        client.submit(request2).toBlocking().last();
        Set<Server> cachedServers = client.getRxClients().keySet();
        assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()), new Server("localhost", server3.getPort())), cachedServers);
        List<Server> newList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", 99999));
        lb.setServersList(newList);
        cachedServers = client.getRxClients().keySet();
        assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server3.getPort())), cachedServers);
    } finally {
        server1.shutdown();
        server2.shutdown();
        server3.shutdown();
    }
}
 
Example 7
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableWithMultipleServersFailed() throws Exception {        
    IClientConfig config = IClientConfig.Builder.newBuilder()
            .withDefaultValues()
            .withRetryOnAllOperations(true)
            .withMaxAutoRetries(1)
            .withMaxAutoRetriesNextServer(3)
            .withConnectTimeout(100)
            .build();
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());        
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config);
    Server badServer = new Server("localhost:12345");
    Server badServer1 = new Server("localhost:12346");
    Server badServer2 = new Server("localhost:12347");

    List<Server> servers = Lists.newArrayList(badServer, badServer1, badServer2);
    lb.setServersList(servers);
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    assertNull(observer.obj);
    observer.error.printStackTrace();
    assertTrue(observer.error instanceof ClientException);
    
    ServerStats stats = lbObservables.getServerStats(badServer);
    // two requests to bad server because retry same server is set to 1
    assertEquals(2, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(2, stats.getSuccessiveConnectionFailureCount());
}
 
Example 8
Source File: Demo.java    From request-correlation-spring-cloud-starter with Apache License 2.0 4 votes vote down vote up
@Bean
public ILoadBalancer ribbonLoadBalancer() {
    BaseLoadBalancer balancer = new BaseLoadBalancer();
    balancer.setServersList(Collections.singletonList(new Server("localhost", this.port)));
    return balancer;
}
 
Example 9
Source File: OkHttpClientTest.java    From feign-okhttp-spring-cloud-starter with Apache License 2.0 4 votes vote down vote up
@Bean
public ILoadBalancer ribbonLoadBalancer() {
    BaseLoadBalancer balancer = new BaseLoadBalancer();
    balancer.setServersList(Collections.singletonList(new Server("localhost", this.port)));
    return balancer;
}
 
Example 10
Source File: OAuth2HttpClientTest.java    From feign-oauth2-spring-cloud-starter with Apache License 2.0 4 votes vote down vote up
@Bean
public ILoadBalancer ribbonLoadBalancer() {
    BaseLoadBalancer balancer = new BaseLoadBalancer();
    balancer.setServersList(Collections.singletonList(new Server("localhost", this.port)));
    return balancer;
}
 
Example 11
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadBalancingObservablesWithReadTimeout() throws Exception {
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json")
            .setBody(content));       
    server.play();

    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues()
            .set(CommonClientConfigKey.ReadTimeout, 100);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/readTimeout");
    
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    
    Server goodServer = new Server("localhost:" + server.getPort());
    Server badServer  = new Server("localhost:" + port);
    lb.setServersList(Lists.newArrayList(goodServer, badServer, badServer, goodServer));
    
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    ServerStats stats = lbObservables.getServerStats(badServer);
    server.shutdown();
    
    final HttpClientListener listener = lbObservables.getListener();
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 5;
        }
    });
    assertEquals(0, listener.getPoolReuse());
    
    // two requests to bad server because retry same server is set to 1
    assertEquals(4, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(4, stats.getSuccessiveConnectionFailureCount());
    
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
}
 
Example 12
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadBalancingWithTwoServers() throws Exception {
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json")
            .setBody(content));       
    server.play();

    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues();
            
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/person")
            .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null))
            .withHeader("Content-type", "application/json");
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    HttpClientListener externalListener = HttpClientListener.newHttpListener("external");
    lbObservables.subscribe(externalListener);
    Server server1 = new Server("localhost:" + server.getPort());
    Server server2 = new Server("localhost:" + port);
    
    lb.setServersList(Lists.newArrayList(server1, server2));
    RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {
        @Override
        public boolean isRetriableException(Throwable e, boolean sameServer) {
            return true;
        }
    };
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(EmbeddedResources.defaultPerson.age, observer.obj.age);
    
    observer = new ObserverWithLatch<Person>();
    observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    
    ServerStats stats = lbObservables.getServerStats(server1);
    server.shutdown();
    // assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    
    stats = lbObservables.getServerStats(server2);
    // two requests to bad server because retry same server is set to 1
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
    final HttpClientListener listener = lbObservables.getListener();
    assertEquals(2, listener.getPoolAcquires());
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 2;
        }
    });
    assertEquals(2, listener.getConnectionCount());
    assertEquals(0, listener.getPoolReuse());
    assertEquals(2, externalListener.getPoolAcquires());
}
 
Example 13
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadBalancingPostWithReadTimeout() throws Exception {
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse()
            .setResponseCode(200)
            .setHeader("Content-type", "application/json")
            .setBody(content));       
    server.play();

    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues()
            .set(CommonClientConfigKey.ReadTimeout, 100);
    
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/postTimeout")
            .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null))
            .withHeader("Content-type", "application/json");
    
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    Server goodServer = new Server("localhost:" + server.getPort());
    Server badServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    lb.setServersList(servers);
    RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {
        @Override
        public boolean isRetriableException(Throwable e, boolean sameServer) {
            return true;
        }
    };
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    ServerStats stats = lbObservables.getServerStats(badServer);
    server.shutdown();
    assertEquals(4, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(4, stats.getSuccessiveConnectionFailureCount());
    
    stats = lbObservables.getServerStats(goodServer);
    // two requests to bad server because retry same server is set to 1
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
}
 
Example 14
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadBalancingPostWithNoRetrySameServer() throws Exception {
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json")
            .setBody(content));       
    server.play();

    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues()
            .set(CommonClientConfigKey.ReadTimeout, 100);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/postTimeout")
            .withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null))
            .withHeader("Content-type", "application/json");
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(0, 3, true);
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    Server goodServer = new Server("localhost:" + server.getPort());
    Server badServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    lb.setServersList(servers);
    RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {
        @Override
        public boolean isRetriableException(Throwable e, boolean sameServer) {
            return true;
        }
    };
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    server.shutdown();
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    ServerStats stats = lbObservables.getServerStats(badServer);
    assertEquals(2, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(2, stats.getSuccessiveConnectionFailureCount());
    
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
}