SpringCloudGateway - 记录传入请求 url 和相应的路由 URI
SpringCloudGateway - Log incoming request url and corresponding route URI
我是 spring 云网关的新手,我想要的是将传入请求记录到相应的路由 url,例如如果我有以下路由配置:
- id: route1
uri: http://localhost:8585/
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo/(?<segment>.*), /$\{segment}
然后对于“http://localhost:8080/foo/route1”的传入请求,应打印以下日志消息。
"Incoming request url 'http://localhost:8080/foo/route1' is routed to 'http://localhost:8585/route1'"
是否有任何简单的方法可以实现此目的,或者我可以通过设置日志级别来实现此目的。
你能帮忙吗
你可以用一个简单的 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);
}
}
在日志中生成以下内容。
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
从spring云网关2.2开始,请尝试以下开关:
logging:
level:
reactor:
netty: INFO
org:
springframework:
cloud:
gateway: TRACE
spring:
cloud:
gateway:
httpclient:
wiretap: true
httpserver:
wiretap: true
更多信息请参考这里,spring cloud gateway log levels
我找到了以下解决方案,试一试:
application.yml
logging:
level:
reactor:
netty: INFO
org:
springframework:
cloud:
gateway: INFO
Bean 配置:
@Bean
HttpClient httpClient() {
return HttpClient.create().wiretap("LoggingFilter", LogLevel.INFO, AdvancedByteBufFormat.TEXTUAL);
}
阿克谢
我是 spring 云网关的新手,我想要的是将传入请求记录到相应的路由 url,例如如果我有以下路由配置:
- id: route1
uri: http://localhost:8585/
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo/(?<segment>.*), /$\{segment}
然后对于“http://localhost:8080/foo/route1”的传入请求,应打印以下日志消息。
"Incoming request url 'http://localhost:8080/foo/route1' is routed to 'http://localhost:8585/route1'"
是否有任何简单的方法可以实现此目的,或者我可以通过设置日志级别来实现此目的。
你能帮忙吗
你可以用一个简单的 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);
}
}
在日志中生成以下内容。
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
从spring云网关2.2开始,请尝试以下开关:
logging:
level:
reactor:
netty: INFO
org:
springframework:
cloud:
gateway: TRACE
spring:
cloud:
gateway:
httpclient:
wiretap: true
httpserver:
wiretap: true
更多信息请参考这里,spring cloud gateway log levels
我找到了以下解决方案,试一试:
application.yml
logging:
level:
reactor:
netty: INFO
org:
springframework:
cloud:
gateway: INFO
Bean 配置:
@Bean
HttpClient httpClient() {
return HttpClient.create().wiretap("LoggingFilter", LogLevel.INFO, AdvancedByteBufFormat.TEXTUAL);
}
阿克谢