SpringCloudGateway - 记录传入请求 url 和相应的路由 URI [英] SpringCloudGateway - Log incoming request url and corresponding route URI

查看:25
本文介绍了SpringCloudGateway - 记录传入请求 url 和相应的路由 URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 spring cloud gateway 的新手,我想要的是将传入请求记录到相应的路由 url,例如如果我有以下路由配置:

I am new to spring cloud gateway, what I want is to log incoming request to corresponding route url, For e.g. if I have following route config:

      - id: route1
        uri: http://localhost:8585/
        predicates:
        - Path=/foo/**
        filters:
        - RewritePath=/foo/(?<segment>.*), /$\{segment}

然后对于 'http://localhost:8080/foo/route1' 的传入请求如下应该打印日志消息.

Then for a incoming request of 'http://localhost:8080/foo/route1' following log message should be printed.

"传入请求 url 'http://localhost:8080/foo/route1'路由到'http://localhost:8585/route1'"

"Incoming request url 'http://localhost:8080/foo/route1' is routed to 'http://localhost:8585/route1'"

是否有任何简单的方法可以实现这一点,或者我可以通过设置日志级别来实现这一点.

Is there any easy way of achieving this or can I achieve this just by setting log level.

你能帮忙吗

推荐答案

你可以用一个简单的 GlobalFilter 做到这一点.

You could do it with a simple GlobalFilter.

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.util.Collections;
import java.util.Set;

import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;

public class LoggingFilter implements GlobalFilter {
    Log log = LogFactory.getLog(getClass());

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        Set<URI> uris = exchange.getAttributeOrDefault(GATEWAY_ORIGINAL_REQUEST_URL_ATTR, Collections.emptySet());
        String originalUri = (uris.isEmpty()) ? "Unknown" : uris.iterator().next().toString();
        Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
        URI routeUri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
        log.info("Incoming request " + originalUri + " is routed to id: " + route.getId()
                + ", uri:" + routeUri);
        return chain.filter(exchange);
    }
}

在日志中生成以下内容.

produces the following in the logs.

2019-01-09 15:36:32.422  INFO 6870 --- [or-http-epoll-2] LoggingFilter                      : Incoming request http://localhost:8080/api/configserver/foo/default is routed to id: CompositeDiscoveryClient_CONFIGSERVER, uri:http://192.168.0.112:8888/foo/default

这篇关于SpringCloudGateway - 记录传入请求 url 和相应的路由 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆