SpringCloud Gateway - 수신 요청 URL 및 해당 경로 URI 기록
저는 봄 클라우드 게이트웨이를 처음 사용합니다. 제가 원하는 것은 수신 요청을 해당 경로 URL에 기록하는 것입니다. 예를 들어, 다음과 같은 경로 구성이 있는 경우:
- id: route1
uri: http://localhost:8585/
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo/(?<segment>.*), /$\{segment}
그런 다음 'http://localhost:8080/foo/route1'의 수신 요청에 대해 다음 로그 메시지를 인쇄해야 합니다.
"수신 요청 URL 'http://localhost:8080/foo/route1'이(가) '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 Cloud Gateway 2.2부터는 다음 스위치를 사용해 보십시오.
logging:
level:
reactor:
netty: INFO
org:
springframework:
cloud:
gateway: TRACE
spring:
cloud:
gateway:
httpclient:
wiretap: true
httpserver:
wiretap: true
자세한 정보는 여기를 참조하십시오. 봄 클라우드 게이트웨이 로그 수준
아래 솔루션을 찾았습니다. 사용해 보십시오.
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);
}
악샤이
이 로그 수준을 추가하면 됩니다.로그 메시지는 필터보다 더 상세하지만 적어도 클래스를 만들 필요는 없습니다.
application.yml:
logging:
level:
org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping: DEBUG
필터 솔루션의 장점은 TRACE로 변경하여 경로를 찾을 수 없는 경우에도 기록할 수 있다는 것입니다.
logging:
level:
org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping: TRACE
언급URL : https://stackoverflow.com/questions/54117061/springcloudgateway-log-incoming-request-url-and-corresponding-route-uri
'programing' 카테고리의 다른 글
numpy의 배열에서 축은 어떻게 인덱싱됩니까? (0) | 2023.07.23 |
---|---|
장고에서 한 앱에서 다른 앱으로 외부 키. (0) | 2023.07.23 |
Tensorflow - ValueError: NumPy 배열을 Tensor로 변환하지 못했습니다(지원되지 않는 개체 유형 플로트). (0) | 2023.07.18 |
사전의 값과 키를 교환하려면 어떻게 해야 합니까? (0) | 2023.07.18 |
장고 1.7은 장고.core.exceptions를 던집니다.AppRegistryNotReady: 모델이 아직 로드되지 않았습니다. (0) | 2023.07.18 |