Search in sources :

Example 21 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancerClientTests method testReconstructUriWithSecureClientConfig.

@Test
public void testReconstructUriWithSecureClientConfig() throws Exception {
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    when(config.get(CommonClientConfigKey.IsSecure)).thenReturn(true);
    when(clientFactory.getClientConfig(server.getServiceId())).thenReturn(config);
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    ServiceInstance serviceInstance = client.choose(server.getServiceId());
    URI uri = client.reconstructURI(serviceInstance, new URL("http://" + server.getServiceId()).toURI());
    assertEquals(server.getHost(), uri.getHost());
    assertEquals(server.getPort(), uri.getPort());
    assertEquals("https", uri.getScheme());
}
Also used : RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) IClientConfig(com.netflix.client.config.IClientConfig) ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI) URL(java.net.URL) Test(org.junit.Test)

Example 22 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancerClientTests method testReconstructUriWithPath.

private void testReconstructUriWithPath(String scheme, String path) {
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    when(config.get(CommonClientConfigKey.IsSecure)).thenReturn(true);
    when(clientFactory.getClientConfig(server.getServiceId())).thenReturn(config);
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    ServiceInstance serviceInstance = client.choose(server.getServiceId());
    URI expanded = new DefaultUriBuilderFactory().expand(scheme + "://" + server.getServiceId() + path);
    URI reconstructed = client.reconstructURI(serviceInstance, expanded);
    assertThat(reconstructed).hasPath(path);
}
Also used : RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) IClientConfig(com.netflix.client.config.IClientConfig) ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory)

Example 23 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancerClientTests method testChoose.

@Test
public void testChoose() {
    RibbonServer server = getRibbonServer();
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    ServiceInstance serviceInstance = client.choose(server.getServiceId());
    assertServiceInstance(server, serviceInstance);
}
Also used : RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Test(org.junit.Test)

Example 24 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancerClientTests method testReconstructSchemelessUriWithoutClientConfig.

public void testReconstructSchemelessUriWithoutClientConfig(RibbonServer server, String expectedScheme) throws Exception {
    IClientConfig config = mock(IClientConfig.class);
    when(config.get(CommonClientConfigKey.IsSecure)).thenReturn(null);
    when(clientFactory.getClientConfig(server.getServiceId())).thenReturn(config);
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    ServiceInstance serviceInstance = client.choose(server.getServiceId());
    URI uri = client.reconstructURI(serviceInstance, new URI("//" + server.getServiceId()));
    assertEquals(server.getHost(), uri.getHost());
    assertEquals(server.getPort(), uri.getPort());
    assertEquals(expectedScheme, uri.getScheme());
}
Also used : IClientConfig(com.netflix.client.config.IClientConfig) ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI)

Example 25 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-netflix by spring-cloud.

the class RetryableRibbonLoadBalancingHttpClient method execute.

@Override
public RibbonApacheHttpResponse execute(final RibbonApacheHttpRequest request, final IClientConfig configOverride) throws Exception {
    final RequestConfig.Builder builder = RequestConfig.custom();
    IClientConfig config = configOverride != null ? configOverride : this.config;
    RibbonProperties ribbon = RibbonProperties.from(config);
    builder.setConnectTimeout(ribbon.connectTimeout(this.connectTimeout));
    builder.setSocketTimeout(ribbon.readTimeout(this.readTimeout));
    builder.setRedirectsEnabled(ribbon.isFollowRedirects(this.followRedirects));
    final RequestConfig requestConfig = builder.build();
    final LoadBalancedRetryPolicy retryPolicy = loadBalancedRetryFactory.createRetryPolicy(this.getClientName(), this);
    RetryCallback<RibbonApacheHttpResponse, Exception> retryCallback = context -> {
        // on retries the policy will choose the server and set it in the context
        // extract the server and update the request being made
        RibbonApacheHttpRequest newRequest = request;
        if (context instanceof LoadBalancedRetryContext) {
            ServiceInstance service = ((LoadBalancedRetryContext) context).getServiceInstance();
            validateServiceInstance(service);
            if (service != null) {
                // Reconstruct the request URI using the host and port set in the retry context
                newRequest = newRequest.withNewUri(UriComponentsBuilder.newInstance().host(service.getHost()).scheme(service.getUri().getScheme()).userInfo(newRequest.getURI().getUserInfo()).port(service.getPort()).path(newRequest.getURI().getPath()).query(newRequest.getURI().getQuery()).fragment(newRequest.getURI().getFragment()).build().encode().toUri());
            }
        }
        newRequest = getSecureRequest(newRequest, configOverride);
        HttpUriRequest httpUriRequest = newRequest.toRequest(requestConfig);
        final HttpResponse httpResponse = RetryableRibbonLoadBalancingHttpClient.this.delegate.execute(httpUriRequest);
        if (retryPolicy.retryableStatusCode(httpResponse.getStatusLine().getStatusCode())) {
            throw new HttpClientStatusCodeException(RetryableRibbonLoadBalancingHttpClient.this.clientName, httpResponse, HttpClientUtils.createEntity(httpResponse), httpUriRequest.getURI());
        }
        return new RibbonApacheHttpResponse(httpResponse, httpUriRequest.getURI());
    };
    LoadBalancedRecoveryCallback<RibbonApacheHttpResponse, HttpResponse> recoveryCallback = new LoadBalancedRecoveryCallback<RibbonApacheHttpResponse, HttpResponse>() {

        @Override
        protected RibbonApacheHttpResponse createResponse(HttpResponse response, URI uri) {
            return new RibbonApacheHttpResponse(response, uri);
        }
    };
    return this.executeWithRetry(request, retryPolicy, retryCallback, recoveryCallback);
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) InterceptorRetryPolicy(org.springframework.cloud.client.loadbalancer.InterceptorRetryPolicy) HttpRequest(org.springframework.http.HttpRequest) RequestConfig(org.apache.http.client.config.RequestConfig) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) BooleanUtils(org.apache.commons.lang.BooleanUtils) BackOffPolicy(org.springframework.retry.backoff.BackOffPolicy) LoadBalancedRetryFactory(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory) RecoveryCallback(org.springframework.retry.RecoveryCallback) RetryListener(org.springframework.retry.RetryListener) URI(java.net.URI) ServiceInstanceChooser(org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) NoBackOffPolicy(org.springframework.retry.backoff.NoBackOffPolicy) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestSpecificRetryHandler(com.netflix.client.RequestSpecificRetryHandler) IClientConfig(com.netflix.client.config.IClientConfig) ContextAwareRequest(org.springframework.cloud.netflix.ribbon.support.ContextAwareRequest) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) NeverRetryPolicy(org.springframework.retry.policy.NeverRetryPolicy) LoadBalancedRecoveryCallback(org.springframework.cloud.client.loadbalancer.LoadBalancedRecoveryCallback) RetryHandler(com.netflix.client.RetryHandler) RibbonProperties(org.springframework.cloud.netflix.ribbon.RibbonProperties) RetryCallback(org.springframework.retry.RetryCallback) HttpResponse(org.apache.http.HttpResponse) ServiceInstance(org.springframework.cloud.client.ServiceInstance) RetryTemplate(org.springframework.retry.support.RetryTemplate) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RequestConfig(org.apache.http.client.config.RequestConfig) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) ServiceInstance(org.springframework.cloud.client.ServiceInstance) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) IClientConfig(com.netflix.client.config.IClientConfig) LoadBalancedRecoveryCallback(org.springframework.cloud.client.loadbalancer.LoadBalancedRecoveryCallback) RibbonProperties(org.springframework.cloud.netflix.ribbon.RibbonProperties) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext)

Aggregations

ServiceInstance (org.springframework.cloud.client.ServiceInstance)73 DefaultServiceInstance (org.springframework.cloud.client.DefaultServiceInstance)25 Test (org.junit.Test)24 HashMap (java.util.HashMap)12 URI (java.net.URI)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Registration (de.codecentric.boot.admin.server.domain.values.Registration)10 ArrayList (java.util.ArrayList)10 Test (org.junit.jupiter.api.Test)10 Application (de.codecentric.boot.admin.model.Application)7 Map (java.util.Map)7 List (java.util.List)6 RibbonServer (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer)6 IClientConfig (com.netflix.client.config.IClientConfig)5 Random (java.util.Random)4 Collections (java.util.Collections)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 MonitorProperties (com.alibaba.druid.admin.config.MonitorProperties)2 ServiceNode (com.alibaba.druid.admin.model.ServiceNode)2 ConnectionResult (com.alibaba.druid.admin.model.dto.ConnectionResult)2