Search in sources :

Example 31 with ServiceInstance

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

the class LoadBalancerClientFilter method filter.

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    URI url = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
    String schemePrefix = exchange.getAttribute(GATEWAY_SCHEME_PREFIX_ATTR);
    if (url == null || (!"lb".equals(url.getScheme()) && !"lb".equals(schemePrefix))) {
        return chain.filter(exchange);
    }
    // preserve the original url
    addOriginalRequestUrl(exchange, url);
    log.trace("LoadBalancerClientFilter url before: " + url);
    final ServiceInstance instance = loadBalancer.choose(url.getHost());
    if (instance == null) {
        throw new NotFoundException("Unable to find instance for " + url.getHost());
    }
    URI uri = exchange.getRequest().getURI();
    // if the `lb:<scheme>` mechanism was used, use `<scheme>` as the default,
    // if the loadbalancer doesn't provide one.
    String overrideScheme = null;
    if (schemePrefix != null) {
        overrideScheme = url.getScheme();
    }
    URI requestUrl = loadBalancer.reconstructURI(new DelegatingServiceInstance(instance, overrideScheme), uri);
    log.trace("LoadBalancerClientFilter url chosen: " + requestUrl);
    exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl);
    return chain.filter(exchange);
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) NotFoundException(org.springframework.cloud.gateway.support.NotFoundException) URI(java.net.URI)

Example 32 with ServiceInstance

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

the class ConsulServiceRegistryTests method assertHasInstance.

private void assertHasInstance(String serviceId) {
    List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
    assertThat(instances).hasSize(1);
    ServiceInstance instance = instances.get(0);
    assertThat(instance.getServiceId()).isEqualTo(serviceId);
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 33 with ServiceInstance

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

the class ConsulDiscoveryClientAclTests method getInstancesForSecondServiceWorks.

@Test
public void getInstancesForSecondServiceWorks() throws Exception {
    new SpringApplicationBuilder(MyTestConfig.class).run("--spring.application.name=testSecondServiceAcl", "--server.port=0", "--spring.cloud.consul.discovery.preferIpAddress=true", "--consul.token=2d2e6b3b-1c82-40ab-8171-54609d8ad304");
    List<ServiceInstance> instances = discoveryClient.getInstances("testSecondServiceAcl");
    assertNotNull("second service instances was null", instances);
    assertFalse("second service instances was empty", instances.isEmpty());
}
Also used : SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 34 with ServiceInstance

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

the class ConsulDiscoveryClientTests method getInstancesForServiceRespectsQueryParams.

@Test
public void getInstancesForServiceRespectsQueryParams() {
    Response<List<String>> catalogDatacenters = consulClient.getCatalogDatacenters();
    List<String> dataCenterList = catalogDatacenters.getValue();
    assertFalse("no data centers found", dataCenterList.isEmpty());
    List<ServiceInstance> instances = discoveryClient.getInstances("consul", new QueryParams(dataCenterList.get(0)));
    assertFalse("instances was empty", instances.isEmpty());
    ServiceInstance instance = instances.get(0);
    assertIpAddress(instance);
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 35 with ServiceInstance

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

the class ConsulDiscoveryClientTests method getInstancesForServiceWorks.

@Test
public void getInstancesForServiceWorks() {
    List<ServiceInstance> instances = discoveryClient.getInstances("consul");
    assertNotNull("instances was null", instances);
    assertFalse("instances was empty", instances.isEmpty());
    ServiceInstance instance = instances.get(0);
    assertIpAddress(instance);
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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