Search in sources :

Example 1 with RibbonServer

use of org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancedRetryFactoryTests method testGetRetryPolicyRetryOnNonGet.

@Test
public void testGetRetryPolicyRetryOnNonGet() throws Exception {
    int sameServer = 3;
    int nextServer = 3;
    boolean retryOnAllOps = true;
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(sameServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(nextServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(retryOnAllOps).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
    doReturn(retryOnAllOps).when(config).getPropertyAsBoolean(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
    doReturn("").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
    doReturn(server.getServiceId()).when(config).getClientName();
    doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
    clientFactory.getLoadBalancerContext(server.getServiceId()).initWithNiwsConfig(config);
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
    LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
    HttpRequest request = mock(HttpRequest.class);
    doReturn(HttpMethod.POST).when(request).getMethod();
    LoadBalancedRetryContext context = new LoadBalancedRetryContext(null, request);
    assertThat(policy.canRetryNextServer(context), is(true));
    assertThat(policy.canRetrySameServer(context), is(true));
    assertThat(policy.retryableStatusCode(400), is(false));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Example 2 with RibbonServer

use of org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancedRetryFactoryTests method testGetRetryPolicyNotGet.

@Test
public void testGetRetryPolicyNotGet() throws Exception {
    int sameServer = 3;
    int nextServer = 3;
    boolean retryOnAllOps = false;
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(sameServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(nextServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(retryOnAllOps).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
    doReturn(retryOnAllOps).when(config).getPropertyAsBoolean(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
    doReturn("").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
    doReturn(server.getServiceId()).when(config).getClientName();
    doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
    clientFactory.getLoadBalancerContext(server.getServiceId()).setRetryHandler(new DefaultLoadBalancerRetryHandler(config));
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
    LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
    HttpRequest request = mock(HttpRequest.class);
    doReturn(HttpMethod.POST).when(request).getMethod();
    LoadBalancedRetryContext context = new LoadBalancedRetryContext(null, request);
    assertThat(policy.canRetryNextServer(context), is(false));
    assertThat(policy.canRetrySameServer(context), is(false));
    assertThat(policy.retryableStatusCode(400), is(false));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Example 3 with RibbonServer

use of org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancedRetryFactoryTests method testRetryableStatusCodes.

@Test
public void testRetryableStatusCodes() throws Exception {
    int sameServer = 3;
    int nextServer = 3;
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(false).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), eq(false));
    doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
    doReturn("404, 418,502,foo, ,").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
    clientFactory.getLoadBalancerContext(server.getServiceId()).setRetryHandler(new DefaultLoadBalancerRetryHandler(config));
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
    LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
    HttpRequest request = mock(HttpRequest.class);
    doReturn(HttpMethod.GET).when(request).getMethod();
    assertThat(policy.retryableStatusCode(400), is(false));
    assertThat(policy.retryableStatusCode(404), is(true));
    assertThat(policy.retryableStatusCode(418), is(true));
    assertThat(policy.retryableStatusCode(502), is(true));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) Test(org.junit.Test)

Example 4 with RibbonServer

use of org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancerClientTests method testReconstructURI.

private void testReconstructURI(String scheme) throws Exception {
    RibbonServer server = getRibbonServer();
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    ServiceInstance serviceInstance = client.choose(server.getServiceId());
    URI uri = client.reconstructURI(serviceInstance, new URL(scheme + "://" + server.getServiceId()).toURI());
    assertThat(uri).hasScheme(scheme).hasHost(serviceInstance.getHost()).hasPort(serviceInstance.getPort());
}
Also used : RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI) URL(java.net.URL)

Example 5 with RibbonServer

use of org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancerClientTests method testReconstructHonorsRibbonServerScheme.

@Test
public void testReconstructHonorsRibbonServerScheme() {
    RibbonServer server = new RibbonServer("testService", new Server("ws", "myhost", 9080), false, Collections.singletonMap("mykey", "myvalue"));
    IClientConfig config = mock(IClientConfig.class);
    when(config.get(CommonClientConfigKey.IsSecure)).thenReturn(false);
    when(clientFactory.getClientConfig(server.getServiceId())).thenReturn(config);
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    ServiceInstance serviceInstance = client.choose(server.getServiceId());
    URI uri = client.reconstructURI(serviceInstance, URI.create("http://testService"));
    assertThat(uri).hasScheme("ws").hasHost("myhost").hasPort(9080);
}
Also used : RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) Server(com.netflix.loadbalancer.Server) IClientConfig(com.netflix.client.config.IClientConfig) ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI) Test(org.junit.Test)

Aggregations

RibbonServer (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer)16 Test (org.junit.Test)13 IClientConfig (com.netflix.client.config.IClientConfig)10 ServiceInstance (org.springframework.cloud.client.ServiceInstance)8 HttpRequest (org.springframework.http.HttpRequest)7 LoadBalancedRetryPolicy (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy)6 DefaultLoadBalancerRetryHandler (com.netflix.client.DefaultLoadBalancerRetryHandler)5 IOException (java.io.IOException)5 URI (java.net.URI)5 LoadBalancedRetryContext (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext)5 Server (com.netflix.loadbalancer.Server)4 URL (java.net.URL)4 ServerStats (com.netflix.loadbalancer.ServerStats)2 DefaultUriBuilderFactory (org.springframework.web.util.DefaultUriBuilderFactory)2 CommonClientConfigKey (com.netflix.client.config.CommonClientConfigKey)1 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)1 LoadBalancerStats (com.netflix.loadbalancer.LoadBalancerStats)1 SocketException (java.net.SocketException)1 Collections (java.util.Collections)1 Map (java.util.Map)1