简体   繁体   English

使用 Spring Cloud Config 进行外部日志配置

[英]External logging configuration with Spring Cloud Config


I just experienced Spring Cloud Config to have some configuration files outside of my projects.我刚刚体验了 Spring Cloud Config,在我的项目之外有一些配置文件。 I followed the instructions to set up a client and a server (linked to a git) and it worked great !我按照说明设置了客户端和服务器(链接到 git),效果很好!
Basically I have different application.yml for each profile I have and inside these files there is a server port property and also an URL to the logging config file (one log4j2.yml per profile) which is also on my git repository.基本上,我对每个配置文件都有不同的 application.yml,在这些文件中,有一个服务器端口属性和一个指向日志配置文件的 URL(每个配置文件一个 log4j2.yml),它也在我的 git 存储库中。 Without authentication it works well: the client asks the server for the application.yml file which matches its profile.如果没有身份验证,它运行良好:客户端向服务器询问与其配置文件匹配的 application.yml 文件。 Then, the server finds the file and return to the client the port and the log4j2 config file.然后,服务器找到该文件并将端口和 log4j2 配置文件返回给客户端。
I have what I want which is a different level of logging depending on the client's profile.我有我想要的不同级别的日志记录,具体取决于客户的个人资料。
When I set up the authentication with spring-security (with the default username and a simple password) the client recovers the port but when it tries to acess to the log4j2 config file, the server returns a 401 error saying that the client isn't authorized to access this file.当我使用 spring-security(使用默认用户名和简单密码)设置身份验证时,客户端恢复端口,但是当它尝试访问 log4j2 配置文件时,服务器返回 401 错误,表示客户端不是有权访问此文件。
This might be due to the client not knowing the credentials to access the file inside the application.yml and I don't know if this is possible to insert credentials in the logging.config property这可能是由于客户端不知道访问 application.yml 中文件的凭据,我不知道是否可以在 logging.config 属性中插入凭据

I tried something like this but it's not working as well :我尝试过这样的事情,但效果不佳:

logging:
 config: http://user:password@localhost:8888/....../log4j2.yml

There might be an alternative which consists in saying to the server to ignore the security when the URL is that file but if one day I must have authentication to access it, I won't be able to do it.可能有一种替代方法,即当 URL 是该文件时,告诉服务器忽略安全性,但如果有一天我必须进行身份验证才能访问它,我将无法做到。

There's my files:有我的文件:

GIT通用电气

application-dev.yml应用程序-dev.yml

server:
 port: 55556

logging:
 config: http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml

CLIENT客户

boostrap.yml boostrap.yml

spring:
  application:
    name: ConfigExtClient
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8888
      username: user
      password: foo

Dependencies (pom.xml)依赖项 (pom.xml)

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>1.1.0.M4</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>


    </dependencies>

SERVER服务器

application.yml应用程序.yml

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: URLtoGit

security:
  user:
    name: user
    password: foo

bootstrap.yml引导程序.yml

spring:
  application:
    name: ConfigExtServer

dependencies (pom.xml)依赖项 (pom.xml)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>1.1.0.M4</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

</dependencies> 

ERROR错误

Logging config file location 'http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml' cannot be opened and will be ignored

I tracked the error and it appears in the PropertySourceBootstrapConfiguration class in reinitializeLoggingSystem :我跟踪了错误,它出现在reinitializeLoggingSystemPropertySourceBootstrapConfiguration类中:

try {
            ResourceUtils.getURL(logConfig).openStream().close();
            system.initialize(new LoggingInitializationContext(environment),
                    logConfig, logFile);
        }
        catch (Exception ex) {
            PropertySourceBootstrapConfiguration.logger
                    .warn("Logging config file location '" + logConfig
                            + "' cannot be opened and will be ignored");
        }

It goes into catch and the exception is :它进入 catch 并且异常是:

Server returned HTTP response code: 401 for URL: http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml

Thank you in advance for your help,预先感谢您的帮助,
Romain罗曼

You can set your config client like this sample in the GitHub.您可以在 GitHub 中像此示例一样设置您的配置客户端。

it needs the log4j2.component.properties and bootstrap.yml configuration...它需要log4j2.component.propertiesbootstrap.yml配置...

bootstrap.yml引导程序.yml

logging:
  config: http://configServerAddress:8888/yourAppName/yourSpringProfile/gitBranch/log4j2.xml

log4j2.component.properties log4j2.component.properties

log4j.configurationFile=http://configServerAddress:8888/yourAppName/yourSpringProfile/gitBranch/log4j2.xml
log4j2.configurationUserName=guest
log4j2.configurationPassword=guest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM