当前位置: 首页 > 知识库问答 >
问题:

如何配置spring-cloud-gateway以使用sleuth记录请求/响应主体

郎献
2023-03-14

    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' },


    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

共有1个答案

柴瀚昂
2023-03-14

Spring Cloud Gateway已经可以记录请求和响应,您只需要将日志级别改为Trace即可。

logging:
  level:
    org.springframework: TRACE

或者更准确地说,只记录REQ/RESP:

logging:
  level:
    org.springframework.core.codec.StringDecoder: TRACE

其他选择是在Spring Cloud Gateway中使用一个过滤器,并使用任何日志记录器(如log4j:

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);
                            });
 类似资料:
  • 在spring cloud sleuth文档之后,我用以下内容配置了一个应用程序属性: 然后我添加了一个logback-spring.xml 但是,在发出请求时不会记录标题

  • 我想包括spring.sleuth。将密钥传播到MDC中。这里提到,我们可以创建自己的CurrentTraceContext实现,类似于Slf4jCurrentTraceContext。 但是,无法通过获取值,因为外场传播。额外的类是包私有的:

  • 我有一个基于Spring boot sleuth的应用程序。一切正常。我现在有这样的日志: 现在,我想将自定义MDC添加到我的日志中,例如合同引用。我想要这样的日志: 我尝试了各种方法,但都没有成功: 使用Spring Sleuth Tracer添加标签; 使用MDC. put(xxx, xxx)将logging.pattern.level=%5p%mdc添加到我的application.prop

  • 我创建了一个方面来记录在控制器函数中传递的请求体: 这就是我正在尝试的 现在,spring没有与@RequestBody匹配,而是将参数与。。i、 e.泛化参数并记录传递的所有内容,而不是请求体。我只想记录请求主体,如果为空,则不会打印任何内容。

  • 我在改型API中找不到记录完整请求/响应体的相关方法。我希望在探查器中得到一些帮助(但它只提供关于响应的元数据)。我尝试在构建器中设置日志级别,但这也没有帮助: 编辑:此代码正在工作。我不知道为什么它在早些时候不起作用。可能是因为我用的是旧版本的改装。

  • 我找不到任何将Spring Cloud Sleuth与Spring Cloud Gateway集成的示例。想要一个成功集成的示例项目。