Java Code Examples for com.netflix.ribbon.transport.netty.RibbonTransport#newHttpClient()

The following examples show how to use com.netflix.ribbon.transport.netty.RibbonTransport#newHttpClient() . 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: ProxyHandler.java    From Prana with Apache License 2.0 6 votes vote down vote up
private LoadBalancingHttpClient<ByteBuf, ByteBuf> getClient(String vip) {
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = httpClients.get(vip);
    if (client == null) {
        IClientConfig config = IClientConfig.Builder.newBuilder("prana_backend").
                withDefaultValues().
                withDeploymentContextBasedVipAddresses(vip).
                build().
                set(IClientConfigKey.Keys.MaxTotalConnections, 2000).
                set(IClientConfigKey.Keys.MaxConnectionsPerHost, 2000).
                set(IClientConfigKey.Keys.OkToRetryOnAllOperations, false).
                set(IClientConfigKey.Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());

        client = RibbonTransport.newHttpClient(new HttpClientPipelineConfigurator<ByteBuf, ByteBuf>(), config);
        httpClients.putIfAbsent(vip, client);

    }
    return client;
}
 
Example 2
Source File: DiscoveryLoadBalancerTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadBalancer() {
    IClientConfig config = IClientConfig.Builder.newBuilder().withDefaultValues()
            .withDeploymentContextBasedVipAddresses(getVipAddress()).build()
            .set(IClientConfigKey.Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(config);
    LoadBalancerContext lbContext = client.getLoadBalancerContext();
    List<Server> serverList = lbContext.getLoadBalancer().getAllServers();
    assertEquals(getMockServerList(), serverList);
}
 
Example 3
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testObservable() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person");
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient();
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
    Person person = getPersonObservable(response).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    final HttpClientListener listener = observableClient.getListener();
    assertEquals(1, listener.getPoolAcquires());
    assertEquals(1, listener.getConnectionCount());
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 1;
        }
    });
}
 
Example 4
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubmitToAbsoluteURI() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person");
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient();
    // final List<Person> result = Lists.newArrayList();
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
    Person person = getPersonObservable(response).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    // need to sleep to wait until connection is released
    final HttpClientListener listener = observableClient.getListener();
    assertEquals(1, listener.getConnectionCount());
    assertEquals(1, listener.getPoolAcquires());
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 1;
        }
    });
}
 
Example 5
Source File: SimpleGet.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@edu.umd.cs.findbugs.annotations.SuppressWarnings
public static void main(String[] args) throws Exception {
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient();
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://www.google.com/");
    final CountDownLatch latch = new CountDownLatch(1);
    client.submit(request)
        .toBlocking()
        .forEach(new Action1<HttpClientResponse<ByteBuf>>() {
            @Override
            public void call(HttpClientResponse<ByteBuf> t1) {
                System.out.println("Status code: " + t1.getStatus());
                t1.getContent().subscribe(new Action1<ByteBuf>() {

                    @Override
                    public void call(ByteBuf content) {
                        System.out.println("Response content: " + content.toString(Charset.defaultCharset()));
                        latch.countDown();
                    }
                    
                });
            }
        });
    latch.await(2, TimeUnit.SECONDS);
}
 
Example 6
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testPostWithByteBuf() throws Exception {
    Person myPerson = new Person("netty", 5);
    ObjectMapper mapper = new ObjectMapper();
    byte[] raw = mapper.writeValueAsBytes(myPerson);
    ByteBuf buffer = Unpooled.copiedBuffer(raw);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost(SERVICE_URI + "testAsync/person")
            .withHeader("Content-type", "application/json")
            .withHeader("Content-length", String.valueOf(raw.length))
            .withContent(buffer);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(
            DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(CommonClientConfigKey.ReadTimeout, 10000));
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
    Person person = getPersonObservable(response).toBlocking().single();
    assertEquals(myPerson, person);
}
 
Example 7
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 8
Source File: ListenerTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testSuccessExecutionOnAbosoluteURI() throws IOException {
    MockWebServer server = new MockWebServer();
    String content = "OK";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json")
            .setBody(content));
    server.play();

    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "2000")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://localhost:" + server.getPort() + "/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + server.getPort());
    List<Server> servers = Lists.newArrayList(goodServer, badServer);

    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.ConnectTimeout, 500);
    TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    HttpClientResponse<ByteBuf> response = client.submit(request, null, overrideConfig).toBlocking().last();
    assertEquals(200, response.getStatus().code());
    assertEquals(1, listener.executionStartCounter.get());
    assertEquals(1, listener.startWithServerCounter.get());
    assertEquals(0, listener.exceptionWithServerCounter.get());
    assertEquals(0, listener.executionFailedCounter.get());
    assertEquals(1, listener.executionSuccessCounter.get());
    assertEquals(500, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue());
    assertTrue(listener.isContextChecked());
    assertTrue(listener.isCheckExecutionInfo());
    assertSame(response, listener.getResponse());
}
 
Example 9
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectTimeout() throws Exception {
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(
            DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1"));
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://www.google.com:81/");
    Observable<HttpClientResponse<ByteBuf>> observable = observableClient.submit(new Server("www.google.com", 81), request);
    
    ObserverWithLatch<HttpClientResponse<ByteBuf>> observer = new ObserverWithLatch<HttpClientResponse<ByteBuf>>();
    observable.subscribe(observer);
    observer.await();
    assertNotNull(observer.error);
    assertTrue(observer.error instanceof io.netty.channel.ConnectTimeoutException);
}
 
Example 10
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadTimeout() throws Exception {
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(
            DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ReadTimeout, "100"));
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/readTimeout");
    Observable<HttpClientResponse<ByteBuf>> observable = observableClient.submit(request);
    ObserverWithLatch<HttpClientResponse<ByteBuf>> observer = new ObserverWithLatch<HttpClientResponse<ByteBuf>>();
    observable.subscribe(observer);
    observer.await();
    assertTrue(observer.error instanceof io.netty.handler.timeout.ReadTimeoutException);      
}
 
Example 11
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableWithMultipleServers() throws Exception {
    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues()
            .withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config,
            new NettyHttpLoadBalancerErrorHandler(1, 3, true));
    Person person = getPersonObservable(lbObservables.submit(request)).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    ServerStats stats = lbObservables.getServerStats(badServer);
    // 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());

    person = getPersonObservable(lbObservables.submit(request)).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    HttpClientListener listener = lbObservables.getListener();
    assertEquals(1, listener.getPoolReuse());
}
 
Example 12
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableWithMultipleServersWithOverrideRxConfig() throws Exception {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config,
            new NettyHttpLoadBalancerErrorHandler(1, 3, true));
    HttpClientConfig rxconfig = HttpClientConfig.Builder.newDefaultConfig();
    Person person = getPersonObservable(lbObservables.submit(request, rxconfig)).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    ServerStats stats = lbObservables.getServerStats(badServer);
    // 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());
    
    final HttpClientListener listener = lbObservables.getListener();
    assertEquals(1, listener.getConnectionCount());
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 1;
        }
    });
}
 
Example 13
Source File: NettyClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testObservableWithRetrySameServer() throws Exception {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, goodServer);
    
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config,
            new NettyHttpLoadBalancerErrorHandler(1, 0, true));

    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    assertNull(observer.obj);
    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());
    
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(0, stats.getTotalRequestsCount());
}
 
Example 14
Source File: ListenerTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailedExecutionForAbsoluteURI() {
    IClientConfig config = DefaultClientConfigImpl
            .getClientConfigWithDefaultValues()
            .withProperty(CommonClientConfigKey.ConnectTimeout, "100")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://xyz.unknowhost.xyz/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);

    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    try {
        client.submit(request, null, overrideConfig).toBlocking().last();
        fail("Exception expected");
    } catch(Exception e) {
        assertNotNull(e);
    }
    assertEquals(1, listener.executionStartCounter.get());
    assertEquals(2, listener.startWithServerCounter.get());
    assertEquals(2, listener.exceptionWithServerCounter.get());
    assertEquals(1, listener.executionFailedCounter.get());
    assertTrue(listener.isContextChecked());
    assertTrue(listener.isCheckExecutionInfo());
    assertTrue(listener.getFinalThrowable() instanceof ClientException);
}
 
Example 15
Source File: ListenerTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testAbortedExecutionOnServer() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100")
            .withProperty(CommonClientConfigKey.MaxAutoRetries, 1)
            .withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
            .withRule(new AvailabilityFilteringRule())
            .withPing(new DummyPing())
            .buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) {
        @Override
        public void onStartWithServer(ExecutionContext context, ExecutionInfo info) {
            throw new AbortExecutionException("exit now");
        }
    };
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> ref = new AtomicReference<Throwable>();
    client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() {
        @Override
        public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) {
        }
    }, new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            ref.set(throwable);
            latch.countDown();
        }
    });
    try {
        latch.await(500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(ref.get() instanceof AbortExecutionException);
}
 
Example 16
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());
}
 
Example 17
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 18
Source File: RxMovieTransportExample.java    From ribbon with Apache License 2.0 4 votes vote down vote up
public RxMovieTransportExample(int port) {
    IClientConfig clientConfig = IClientConfig.Builder.newBuilder("movieServiceClient").build();
    clientConfig.set(CommonClientConfigKey.ListOfServers, "localhost:" + port);
    client = RibbonTransport.newHttpClient(clientConfig);
}
 
Example 19
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 20
Source File: RibbonTransportFactory.java    From ribbon with Apache License 2.0 4 votes vote down vote up
public HttpClient<ByteBuf, ByteBuf> newHttpClient(IClientConfig config) {
    return RibbonTransport.newHttpClient(config);
}