Search in sources :

Example 1 with NotFoundException

use of org.springframework.cloud.gateway.support.NotFoundException 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)

Aggregations

URI (java.net.URI)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1 NotFoundException (org.springframework.cloud.gateway.support.NotFoundException)1