如何使用Spring Cloud Config Server集中log4j2配置

问题描述 投票:0回答:1

我正在使用 Spring Cloud 配置服务器文件系统后端:docs.spring.io

我有一个多Maven项目

project:
├── pom.xml
│
├── project-config-provider
│   ├── pom.xml
│   ├── src
│   │   └── main
│   │       ├── java
│   │       │   └── com
│   │       │       └── company
│   │       │           └── project
│   │       │               └── config_provider
│   │       │                   └── Application.java
│   │       └── resources
│   │           └── log4j2-spring.yaml
│   │
│   │
│   ├── config
│   │   ├── application_config-provider--native.yaml
│   │   └── application_config-provider--aws.yaml
│   │
│   └── cloud_config
│       ├── application-config_main.yaml
│       ├── application-config_storage.yaml
│       ├── application-config_swagger.yaml
│       └── application-env_local.yaml
│
│
└── project-abc
    ├── pom.xml
    ├── src
    │   └── main
    │       ├── java
    │       │   └── com
    │       │       └── company
    │       │           └── project
    │       │               └── abc
    │       │                   └── AbcApplication.java
    │       └── resources
    │           └── log4j2-spring.yaml
    │ 
    └── config
        └── application_abc.yaml

模块

project-config-provider
是Spring Cloud配置服务器, 并且模块
project-abc
将使用project-config-provider中的一些配置。

每个模块都有一个

config
文件夹:

  • project-config-provider 包含多个 yaml 文件,每个环境一个(application_config-provider--native.yaml 用于本地测试,application_config-provider--aws.yaml 用于使用 Secretmanager 和 S3 进行 AWS 测试)

application_config-provider--native.yaml:

spring:
   application:
      name: config-provider
   profiles:
      active: native
      include: config_main
   cloud:
      config:
         server:
            native:
               search-location: cloud_config
            prefix: /config
            bootstrap: true

server.port: 8888
  • 所有其他模块只有一个 yaml 文件

application_abc.yaml:

spring:
   application:
      name: maps
      version: 1.0.0
   profiles:
      #declare the configurations to include from cloud_config folder of config-provider 
      include: config_main, config_storage, config_swagger
   config:
      import: configserver:http://${CONFIG_PROVIDER_HOST:localhost}:${CONFIG_PROVIDER_PORT:8888}/config

我想将log4j2-spring.yaml移到cloud_config文件夹下,这样它将被集中,我不必为项目的每个maven模块复制它。

问题是它似乎需要位于 src/main/resources 中。

spring spring-boot spring-cloud log4j2 spring-cloud-config
1个回答
0
投票

如果您使用 Spring Cloud Config 进行应用程序配置,那么将它用于 log4j 配置也是非常有意义的。为此,您通常会使用以下内容配置应用程序的 bootstrap.yml:

spring:
  application:
    name: abc
  cloud:
    config:
    uri: https://spring-configuration-server.company.com/

要添加日志记录配置,您需要添加 log4j-spring-boot-n.n.n.jar 作为应用程序的依赖项,然后添加

  logging
    config: "https://spring-configuration-server.company.com/log4j2.xml"

文件的路径取决于您的 SCC 服务搜索文件的配置方式。

如果您希望日志配置自动更新,您可以将 SCC 配置为向 Spring Cloud Bus 生成事件,然后通过添加 log4j-spring-cloud-client jar 并在您的应用程序中配置 Spring Cloud Bus 将您的应用程序配置为接收事件接收 SCC 服务发送的事件,或者您可以配置 Log4j 通过指定来轮询更改

© www.soinside.com 2019 - 2024. All rights reserved.