Search in sources :

Example 1 with HttpResponse

use of com.netflix.client.http.HttpResponse in project ribbon by Netflix.

the class SampleApp method main.

public static void main(String[] args) throws Exception {
    // 1
    ConfigurationManager.loadPropertiesFromResources("sample-client.properties");
    System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.listOfServers"));
    // 2
    RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");
    // 3
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
    for (int i = 0; i < 20; i++) {
        // 4
        HttpResponse response = client.executeWithLoadBalancer(request);
        System.out.println("Status code for " + response.getRequestedURI() + "  :" + response.getStatus());
    }
    ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();
    System.out.println(lb.getLoadBalancerStats());
    ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", // 5
    "www.linkedin.com:80,www.google.com:80");
    System.out.println("changing servers ...");
    // 6
    Thread.sleep(3000);
    for (int i = 0; i < 20; i++) {
        HttpResponse response = client.executeWithLoadBalancer(request);
        System.out.println("Status code for " + response.getRequestedURI() + "  : " + response.getStatus());
    }
    // 7
    System.out.println(lb.getLoadBalancerStats());
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) RestClient(com.netflix.niws.client.http.RestClient) HttpResponse(com.netflix.client.http.HttpResponse) ZoneAwareLoadBalancer(com.netflix.loadbalancer.ZoneAwareLoadBalancer) URI(java.net.URI)

Example 2 with HttpResponse

use of com.netflix.client.http.HttpResponse in project ribbon by Netflix.

the class SampleApp method main.

public static void main(String[] args) throws Exception {
    // 1
    ConfigurationManager.loadPropertiesFromResources("sample-client.properties");
    System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.listOfServers"));
    // 2
    RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");
    // 3
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
    for (int i = 0; i < 20; i++) {
        // 4
        HttpResponse response = client.executeWithLoadBalancer(request);
        System.out.println("Status code for " + response.getRequestedURI() + "  :" + response.getStatus());
    }
    @SuppressWarnings("rawtypes") ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();
    System.out.println(lb.getLoadBalancerStats());
    ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", // 5
    "www.linkedin.com:80,www.google.com:80");
    System.out.println("changing servers ...");
    // 6
    Thread.sleep(3000);
    for (int i = 0; i < 20; i++) {
        HttpResponse response = null;
        try {
            response = client.executeWithLoadBalancer(request);
            System.out.println("Status code for " + response.getRequestedURI() + "  : " + response.getStatus());
        } finally {
            if (response != null) {
                response.close();
            }
        }
    }
    // 7
    System.out.println(lb.getLoadBalancerStats());
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) RestClient(com.netflix.niws.client.http.RestClient) HttpResponse(com.netflix.client.http.HttpResponse) ZoneAwareLoadBalancer(com.netflix.loadbalancer.ZoneAwareLoadBalancer) URI(java.net.URI)

Example 3 with HttpResponse

use of com.netflix.client.http.HttpResponse in project ribbon by Netflix.

the class RetryTest method testRetriesOnPostWithConnectException.

@Test
public void testRetriesOnPostWithConnectException() throws Exception {
    URI localUrl = new URI("/status?code=503");
    lb.setServersList(Lists.newArrayList(localServer));
    HttpRequest request = HttpRequest.newBuilder().uri(localUrl).verb(Verb.POST).setRetriable(true).build();
    try {
        HttpResponse response = client.executeWithLoadBalancer(request, DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.MaxAutoRetriesNextServer, 2));
        fail("Exception expected");
    } catch (ClientException e) {
    // NOPMD
    }
    ServerStats stats = lb.getLoadBalancerStats().getSingleServerStat(localServer);
    assertEquals(3, stats.getSuccessiveConnectionFailureCount());
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) ServerStats(com.netflix.loadbalancer.ServerStats) HttpResponse(com.netflix.client.http.HttpResponse) ClientException(com.netflix.client.ClientException) URI(java.net.URI) Test(org.junit.Test)

Example 4 with HttpResponse

use of com.netflix.client.http.HttpResponse in project ribbon by Netflix.

the class RetryTest method testSuccessfulRetries.

@Test
public void testSuccessfulRetries() throws Exception {
    lb.setServersList(Lists.newArrayList(new Server("localhost:12987"), new Server("localhost:12987"), localServer));
    URI localUrl = new URI("/ok");
    HttpRequest request = HttpRequest.newBuilder().uri(localUrl).queryParams("name", "ribbon").build();
    try {
        HttpResponse response = client.executeWithLoadBalancer(request, DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.MaxAutoRetriesNextServer, 2));
        assertEquals(200, response.getStatus());
    } catch (ClientException e) {
        fail("Unexpected exception");
    }
    ServerStats stats = lb.getLoadBalancerStats().getSingleServerStat(new Server("localhost:12987"));
    assertEquals(1, stats.getSuccessiveConnectionFailureCount());
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) MockHttpServer(com.netflix.client.testutil.MockHttpServer) Server(com.netflix.loadbalancer.Server) ServerStats(com.netflix.loadbalancer.ServerStats) HttpResponse(com.netflix.client.http.HttpResponse) ClientException(com.netflix.client.ClientException) URI(java.net.URI) Test(org.junit.Test)

Example 5 with HttpResponse

use of com.netflix.client.http.HttpResponse in project ribbon by Netflix.

the class SecureAcceptAllGetTest method testPositiveAcceptAllSSLSocketFactory.

@Test
public void testPositiveAcceptAllSSLSocketFactory() throws Exception {
    // test connection succeeds connecting to a random SSL endpoint with allow all SSL factory
    AbstractConfiguration cm = ConfigurationManager.getConfigInstance();
    String name = "GetPostSecureTest" + ".testPositiveAcceptAllSSLSocketFactory";
    String configPrefix = name + "." + "ribbon";
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.CustomSSLSocketFactoryClassName, "com.netflix.http4.ssl.AcceptAllSocketFactory");
    RestClient rc = (RestClient) ClientFactory.getNamedClient(name);
    TEST_SERVER.accept();
    URI getUri = new URI(TEST_SERVICE_URI + "test/");
    HttpRequest request = HttpRequest.newBuilder().uri(getUri).queryParams("name", "test").build();
    HttpResponse response = rc.execute(request);
    assertEquals(200, response.getStatus());
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) HttpRequest(com.netflix.client.http.HttpRequest) HttpResponse(com.netflix.client.http.HttpResponse) URI(java.net.URI)

Aggregations

HttpRequest (com.netflix.client.http.HttpRequest)20 HttpResponse (com.netflix.client.http.HttpResponse)20 URI (java.net.URI)16 Test (org.junit.Test)16 MockHttpServer (com.netflix.client.testutil.MockHttpServer)3 Server (com.netflix.loadbalancer.Server)3 RestClient (com.netflix.niws.client.http.RestClient)3 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)3 ClientException (com.netflix.client.ClientException)2 IClientConfig (com.netflix.client.config.IClientConfig)2 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)2 ServerStats (com.netflix.loadbalancer.ServerStats)2 ZoneAwareLoadBalancer (com.netflix.loadbalancer.ZoneAwareLoadBalancer)2 MockResponse (com.google.mockwebserver.MockResponse)1 MockWebServer (com.google.mockwebserver.MockWebServer)1 NFRequestContext (com.netflix.zuul.context.NFRequestContext)1 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1