4

我希望开发一个基于 spring-cloud-gateway:2.0.2-RELEASE 的网关服务器,并希望利用 sleuth 进行日志记录。自从我写入日志以来,我一直在运行 sleuth,我看到了 Sleuth 详细信息(跨度 ID 等),但我希望看到自动记录的消息正文。我需要做些什么来让 Sleuth 使用 Spring-Cloud-Gateway 开箱即用地记录请求/响应吗?

这是到达我的下游服务的请求标头

    标题:
       { 'x-request-foo': '2a9c5e36-2c0f-4ad3-926c-cb20d4428462',
         转发:'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',
         主机:'本地主机:8080'},

网关服务中的 Gradle 文件..

    构建脚本 {
        分机{
            kotlinVersion = '1.2.61'
            springBootVersion = '2.0.6.RELEASE'
            springCloudVersion = 'Finchley.RELEASE'
        }
    }
    依赖管理{
        进口{
            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}"
        }
    }
    依赖{
        实施('org.springframework.cloud:spring-cloud-starter-sleuth')
        实施('org.springframework.cloud:spring-cloud-starter-gateway')
        实施(“org.jetbrains.kotlin:kotlin-stdlib-jdk8”)
        实施(“org.jetbrains.kotlin:kotlin-reflect”)
        testImplementation('org.springframework.boot:spring-boot-starter-test')
    }

最后是网关服务的 application.yml 文件...

    服务器:
      小服务程序:
        上下文路径:/
      端口:80
    春天:
      应用:
        名称:api.gateway.ben.com
      侦探:
        跟踪 id128:真
        采样器:
          概率:1.0
      云:
        网关:
          路线:
          - id:管理员-ui-2
            谓词:
            - 路径=/admin-ui-2/echo/*
            过滤器:
            - SetPath=/弗雷德
            - AddRequestHeader=X-Request-Foo, 2a9c5e36-2c0f-4ad3-926c-cb20d4428462
            - AddResponseHeader=X-Response-Foo, 酒吧
            网址:http://localhost:8080
    记录:
      图案:
        级别:“[%X{X-B3-TraceId}/%X{X-B3-SpanId}] %-5p [%t] %C{2} - %m%n”
      等级:
        org.springframework.web:调试

4

1 回答 1

2

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”你需要定义哪些有日志记录逻辑。

于 2019-05-16T02:22:54.437 回答