Search in sources :

Example 1 with RestClient

use of com.netflix.niws.client.http.RestClient in project spring-cloud-netflix by spring-cloud.

the class SpringClientFactoryTests method testCookiePolicy.

@SuppressWarnings("deprecation")
@Test
public void testCookiePolicy() {
    SpringClientFactory factory = new SpringClientFactory();
    AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
    addEnvironment(parent, "ribbon.restclient.enabled=true");
    parent.register(RibbonAutoConfiguration.class, ArchaiusAutoConfiguration.class);
    parent.refresh();
    factory.setApplicationContext(parent);
    RestClient client = factory.getClient("foo", RestClient.class);
    ApacheHttpClient4 jerseyClient = (ApacheHttpClient4) client.getJerseyClient();
    assertEquals(CookiePolicy.IGNORE_COOKIES, jerseyClient.getClientHandler().getHttpClient().getParams().getParameter(ClientPNames.COOKIE_POLICY));
    parent.close();
    factory.destroy();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) RestClient(com.netflix.niws.client.http.RestClient) ApacheHttpClient4(com.sun.jersey.client.apache4.ApacheHttpClient4) Test(org.junit.Test)

Example 2 with RestClient

use of com.netflix.niws.client.http.RestClient 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 3 with RestClient

use of com.netflix.niws.client.http.RestClient 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 4 with RestClient

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

the class ManyShortLivedRequestsSurvivorTest method survive.

@Test
public void survive() throws IOException, ClientException, URISyntaxException, InterruptedException {
    String clientName = "RibbonClientTest-loadBalancingDefaultPolicyRoundRobin";
    String serverListKey = clientName + ".ribbon.listOfServers";
    int nbHitsPerServer = 60;
    MockWebServer server1 = new MockWebServer();
    MockWebServer server2 = new MockWebServer();
    for (int i = 0; i < nbHitsPerServer; i++) {
        server1.enqueue(new MockResponse().setResponseCode(200).setBody("server1 success <" + i + ">!"));
        server2.enqueue(new MockResponse().setResponseCode(200).setBody("server2 success <" + i + ">!"));
    }
    server1.play();
    server2.play();
    getConfigInstance().setProperty(serverListKey, hostAndPort(server1.getUrl("")) + "," + hostAndPort(server2.getUrl("")));
    RestClient client = (RestClient) ClientFactory.getNamedClient(clientName);
    HttpRequest request;
    for (int i = 0; i < nbHitsPerServer * 2; i++) {
        request = HttpRequest.newBuilder().uri(new URI("/")).build();
        HttpResponse response = client.executeWithLoadBalancer(request);
        response.close();
    }
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) RestClient(com.netflix.niws.client.http.RestClient) HttpResponse(com.netflix.client.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 5 with RestClient

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

the class NamedConnectionPoolTest method testConnectionPoolCleaner.

@Test
public void testConnectionPoolCleaner() throws Exception {
    // LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
    ConfigurationManager.getConfigInstance().setProperty("ConnectionPoolCleanerTest.ribbon." + CommonClientConfigKey.ConnIdleEvictTimeMilliSeconds, "100");
    ConfigurationManager.getConfigInstance().setProperty("ConnectionPoolCleanerTest.ribbon." + CommonClientConfigKey.ConnectionCleanerRepeatInterval, "500");
    RestClient client = (RestClient) ClientFactory.getNamedClient("ConnectionPoolCleanerTest");
    NFHttpClient httpclient = NFHttpClientFactory.getNamedNFHttpClient("ConnectionPoolCleanerTest");
    assertNotNull(httpclient);
    com.netflix.client.http.HttpResponse response = null;
    try {
        response = client.execute(HttpRequest.newBuilder().uri(server.getServerPath("/")).build());
    } finally {
        if (response != null) {
            response.close();
        }
    }
    MonitoredConnectionManager connectionPoolManager = (MonitoredConnectionManager) httpclient.getConnectionManager();
    Thread.sleep(2000);
    assertEquals(0, connectionPoolManager.getConnectionsInPool());
    client.shutdown();
}
Also used : RestClient(com.netflix.niws.client.http.RestClient) Test(org.junit.Test)

Aggregations

RestClient (com.netflix.niws.client.http.RestClient)8 HttpRequest (com.netflix.client.http.HttpRequest)4 HttpResponse (com.netflix.client.http.HttpResponse)3 URI (java.net.URI)3 Test (org.junit.Test)3 ZoneAwareLoadBalancer (com.netflix.loadbalancer.ZoneAwareLoadBalancer)2 MockResponse (com.google.mockwebserver.MockResponse)1 MockWebServer (com.google.mockwebserver.MockWebServer)1 IClientConfig (com.netflix.client.config.IClientConfig)1 ApacheHttpClient4 (com.sun.jersey.client.apache4.ApacheHttpClient4)1 IOException (java.io.IOException)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 Bean (org.springframework.context.annotation.Bean)1 Lazy (org.springframework.context.annotation.Lazy)1 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)1