【问题标题】:Unable to utilize log4j-spring-cloud-config-client when Spring Cloud Config uses a backend other than Git or File Based当 Spring Cloud Config 使用 Git 或 File Based 以外的后端时,无法使用 log4j-spring-cloud-config-client
【发布时间】:2021-07-08 17:16:50
【问题描述】:

显然,要将 log4j-spring-cloud-config-client 与 Spring Cloud Config 一起使用,您需要利用 SearchPathLocator 功能来根据特定 URI 提取原始文件。来自 Spring-cloud-config 代码看起来只有 JGitEnvironmentRepositoryNativeEnvironmentRepository 实现了该接口并提供了该功能。

在本地运行,如果我点击以下端点,我会返回一个原始 log4j2 配置文件:http://localhost:8088/config-server-properties-poc/default/master/log4j2.xml。

当我尝试使用 S3 后端时,我得到一个 404,并且它不会尝试搜索该特定文件。我可以通过将我的文件命名为 log4j2-default.json 来解决这个问题(不支持 XML)。当我点击以下 URL 时,我可以取回我的属性,但格式不正确 http://localhost:8088/log4j2/default

格式

{
    "name": "log4j2",
    "profiles": ["default"],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [{
            "name": "log4j2",
            "source": {
                "configuration.appenders.appender[0].PatternLayout.Pattern": "${logging_pattern}",
                "configuration.appenders.appender[0].name": "Console",
                "configuration.appenders.appender[0].target": "SYSTEM_OUT",
                "configuration.appenders.appender[0].type": "Console",
                "configuration.loggers.Root.AppenderRef.ref": "Console",
                "configuration.loggers.Root.level": "info",
                "configuration.loggers.logger[0].AppenderRef.ref": "Console",
                "configuration.loggers.logger[0].additivity": "false",
                "configuration.loggers.logger[0].level": "info",
                "configuration.loggers.logger[0].name": "com.paychex",
                "configuration.loggers.logger[1].AppenderRef.ref": "Console",
                "configuration.loggers.logger[1].additivity": "false",
                "configuration.loggers.logger[1].level": "info",
                "configuration.loggers.logger[1].name": "com.paychex.traceability",
                "configuration.loggers.logger[2].AppenderRef.ref": "Console",
                "configuration.loggers.logger[2].level": "WARN",
                "configuration.loggers.logger[2].name": "org.apache.catalina.startup.VersionLoggerListener",
                "configuration.properties.property[0].name": "logging_pattern",
                "configuration.properties.property[0].value": "%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX},severity=%p,thread=%t,logger=%c,%X,%m%n",
                "configuration.properties.property[1].name": "traceability_logging_pattern",
                "configuration.properties.property[1].value": "%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ},severity=%p,thread=%t,logger=%c,%X,%m%n"
            }
        }
    ]
}

如您所见,属性被包装到 Spring Environment 对象中,属性被推送到 Map 中,因此将其剥离并让 log4j2 解析它会很棘手。

有没有人让 log4j 客户端与非 git 后端一起工作?

【问题讨论】:

    标签: spring-cloud log4j2 spring-cloud-config


    【解决方案1】:

    你是对的。 Log4j 对 Spring Cloud Config 的支持依赖于 SCC 对提供纯文本文件的支持。

    最新的 Spring Cloud Config documentation 表明通过 urls 的纯文本支持仅适用于 Git、SVN、native 和 AWS S3,但要使 S3 工作 Spring Cloud AWS 必须包含在 Config Server 中。这个issue 表示对从 S3 提供纯文本文件的支持似乎已添加到 2019 年 12 月发布的 Spring Cloud Config 2.2.1.Release 中。仍然有一个开放的issue 来添加对保险库后端的支持。

    Log4j 对 SCC 的支持是在 2019 年 6 月的 2.12.0 版本中添加的,当时 SCC 尚不支持 AWS S3。我只使用本地单元/功能测试和 Git 对其进行了测试,因为这是我雇主使用的后端。但是,根据文档,如果您可以让 SCC 使用 AWS 后端提供纯文本,那么 Log4j 应该可以正常工作,它所做的只是通过 URL 查询 SCC。

    【讨论】:

    • 感谢您的回答,我最终发现我必须更新我的 Spring Cloud Config 服务器版本才能启用使用 S3 后端提供纯文本文件的功能。我目前正在配置 spring-cloud-aws 依赖项,当我有这个工作时会更新。
    猜你喜欢
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 2019-10-07
    • 2016-02-18
    • 2018-01-27
    • 2020-10-05
    • 2017-08-17
    • 2021-05-04
    相关资源
    最近更新 更多