我正在寻求开发基于spring-cloud-gateway:2.0.2-RELEASE的网关服务器,并希望利用侦探功能进行日志记录。自从我写入日志以来,我一直在运行sleuth,我看到了Sleuth的详细信息(span ID等),但是我希望看到自动记录的邮件正文。我需要做些什么让Sleuth使用Spring-Cloud-Gateway开箱即用地记录请求/响应吗?
这是到达我的下游服务的请求标头
headers: { 'x-request-foo': '2a9c5e36-2c0f-4ad3-926c-cb20d4428462', forwarded: 'proto=http;host=localhost;for="0:0:0:0:0:0:0:1:51720"', 'x-forwarded-for': '0:0:0:0:0:0:0:1', 'x-forwarded-proto': 'http', 'x-forwarded-port': '80', 'x-forwarded-host': 'localhost', 'x-b3-traceid': '5bd33eb8050c7a32dfce6adfe68b06ca', 'x-b3-spanid': 'ba202a6d6f3e2893', 'x-b3-parentspanid': 'dfce6adfe68b06ca', 'x-b3-sampled': '0', host: 'localhost:8080' },
网关服务中的Gradle文件。
buildscript { ext { kotlinVersion = '1.2.61' springBootVersion = '2.0.6.RELEASE' springCloudVersion = 'Finchley.RELEASE' } } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-sleuth:2.0.2.RELEASE" mavenBom 'org.springframework.cloud:spring-cloud-gateway:2.0.2.RELEASE' mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } dependencies { implementation('org.springframework.cloud:spring-cloud-starter-sleuth') implementation('org.springframework.cloud:spring-cloud-starter-gateway') implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.jetbrains.kotlin:kotlin-reflect") testImplementation('org.springframework.boot:spring-boot-starter-test') }
最后是网关服务的application.yml文件...
server: servlet: contextPath: / port: 80 spring: application: name: api.gateway.ben.com sleuth: trace-id128: true sampler: probability: 1.0 cloud: gateway: routes: - id: admin-ui-2 predicates: - Path=/admin-ui-2/echo/* filters: - SetPath=/fred - AddRequestHeader=X-Request-Foo, 2a9c5e36-2c0f-4ad3-926c-cb20d4428462 - AddResponseHeader=X-Response-Foo, Bar uri: http://localhost:8080 logging: pattern: level: "[%X{X-B3-TraceId}/%X{X-B3-SpanId}] %-5p [%t] %C{2} - %m%n" level: org.springframework.web: DEBUG
答案 0 :(得分:0)
Spring Cloud Gateway已经可以记录请求和响应,您只需要将日志级别更改为TRACE。
logging:
level:
org.springframework: TRACE
或更准确地说,只是记录req / resp:
logging:
level:
org.springframework.core.codec.StringDecoder: TRACE
其他选项是在Spring Cloud Gateway中使用过滤器,并通过Log4j之类的任何记录器记录req / resp等:
routeBuilder.route(id,
r -> {
return r.path(path).and().method(requestmethod).and()
.header(routmap.getRequestheaderkey(), routmap.getRequestheadervalue()).and()
.readBody(String.class, requestBody -> {
return true;
}).filters(f -> {
f.rewritePath(rewritepathregex, replacement);
f.prefixPath(perfixpath);
f.filter(LogFilter);
return f;
}).uri(uri);
});
此文件管理器“ LogFilter”需要定义将具有日志记录逻辑的文件。