在使用spring cloud gateway的时候,路由一般配置为服务名 例如 lb://BASE-API-WEB/xxx/bbb 路径,我们无从知道,他真正路由到什么地方去了。

经过查看源码我发现了,

org.springframework.cloud.gateway.filter.LoadBalancerClientFilter

这个filter中 对lb请求进行了处理,转换成真正的url地址。

核心方法如下

 public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
       //lb下的url
 URI url = (URI)exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR);
        String schemePrefix = (String)exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR);
        if (url == null || !"lb".equals(url.getScheme()) && !"lb".equals(schemePrefix)) {
            return chain.filter(exchange);
        } else {
            ServerWebExchangeUtils.addOriginalRequestUrl(exchange, url);
            log.trace("LoadBalancerClientFilter url before: " + url);
            ServiceInstance instance = this.loadBalancer.choose(url.getHost());
            if (instance == null) {
                throw new NotFoundException("Unable to find instance for " + url.getHost());
            } else {
                URI uri = exchange.getRequest().getURI();
                String overrideScheme = null;
                if (schemePrefix != null) {
                    overrideScheme = url.getScheme();
                }
                //真实的url
                URI requestUrl = this.loadBalancer.reconstructURI(new LoadBalancerClientFilter.DelegatingServiceInstance(instance, overrideScheme), uri);
                log.trace("LoadBalancerClientFilter url chosen: " + requestUrl);
                exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, requestUrl);
                return chain.filter(exchange);
            }
        }
    }

其实,spring cloud gateway 已经打印了日志,但是默认的他是trace级别的,我们常用的日志级别是info级别,所有他不会打印,这就导致了我们在日志中看不到真实的url地址

以下是解决办法:

1.在配置文件中设置org.springframework.cloud.gateway.filter.LoadBalancerClientFilter的日志级别

logging.level.org.springframework.cloud.gateway.filter.LoadBalancerClientFilter=TRACE

注意 这里的配置一定要在

logging.level.org.springframework之后配置 不然会覆盖

2.重写LoadBalancerClientFilter 建立org.springframework.cloud.gateway.filter包 将 类重写 spingboot默认会从本项目中加载类,原先的类就被弃用了。

3.继承LoadBalancerClientFilter 重写filter方法,将日志级别改为info即可

  @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        URI url = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR);
        String schemePrefix = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR);
        if (url == null || !"lb".equals(url.getScheme()) && !"lb".equals(schemePrefix)) {
            return chain.filter(exchange);
        } else {
            ServerWebExchangeUtils.addOriginalRequestUrl(exchange, url);
            ServiceInstance instance = this.loadBalancer.choose(url.getHost());
            if (instance == null) {
                throw new NotFoundException("Unable to find instance for " + url.getHost());
            } else {
                URI uri = exchange.getRequest().getURI();
                String overrideScheme = null;
                if (schemePrefix != null) {
                    overrideScheme = url.getScheme();
                }

                URI requestUrl = this.loadBalancer.reconstructURI(new LoadBalancerClientFilterBean.DelegatingServiceInstance(instance, overrideScheme), uri);
                logger.info("before url = {} , url chosen = {} " ,url, requestUrl);
                exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, requestUrl);
                return chain.filter(exchange);
            }
        }
    }

最终效果如下 :

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐