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);
}
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);
}
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());
}
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);
}
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);
}
Aggregations