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

Rom*_*n D 5 spring-security log4j2 spring-cloud


我刚刚体验过Spring Cloud Config,在我的项目之外有一些配置文件.我按照说明建立了一个客户端和一个服务器(链接到一个git),它工作得很好!
基本上我对每个配置文件都有不同的application.yml,在这些文件中有一个服务器端口属性,还有一个日志配置文件的URL(每个配置文件一个log4j2.yml),它也在我的git存储库中.没有身份验证它运行良好:客户端要求服务器提供与其配置文件匹配的application.yml文件.然后,服务器找到该文件并将端口和log4j2配置文件返回给客户端.
我有我想要的,这是一个不同级别的日志记录,具体取决于客户端的配置文件.
当我使用spring-security设置身份验证(使用默认用户名和简单密码)时,客户端恢复端口,但当它尝试访问log4j2配置文件时,服务器返回401错误,说明客户端不是有权访问此文件.
这可能是由于客户端不知道访问application.yml中的文件的凭据,我不知道是否可以在logging.config属性中插入凭据

我尝试过这样的东西,但是效果不好:

logging:
 config: http://user:password@localhost:8888/....../log4j2.yml
Run Code Online (Sandbox Code Playgroud)

可能有一种替代方法,即当URL是该文件时,告诉服务器忽略安全性但是如果有一天我必须具有访问它的身份验证,我将无法做到.

有我的文件:

GIT

应用dev.yml

server:
 port: 55556

logging:
 config: http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml
Run Code Online (Sandbox Code Playgroud)

客户

boostrap.yml

spring:
  application:
    name: ConfigExtClient
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8888
      username: user
      password: foo
Run Code Online (Sandbox Code Playgroud)

依赖关系(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>
Run Code Online (Sandbox Code Playgroud)

服务器

application.yml

server:
  port: 8888

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

security:
  user:
    name: user
    password: foo
Run Code Online (Sandbox Code Playgroud)

bootstrap.yml

spring:
  application:
    name: ConfigExtServer
Run Code Online (Sandbox Code Playgroud)

依赖项(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> 
Run Code Online (Sandbox Code Playgroud)

错误

Logging config file location 'http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml' cannot be opened and will be ignored
Run Code Online (Sandbox Code Playgroud)

我跟踪了错误,它出现在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");
        }
Run Code Online (Sandbox Code Playgroud)

它进入catch并且例外是:

Server returned HTTP response code: 401 for URL: http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml
Run Code Online (Sandbox Code Playgroud)


罗曼,提前感谢您的帮助

Moj*_*aba 2

您可以像GitHub 中的示例一样设置您的配置客户端。

它需要log4j2.component.propertiesbootstrap.yml配置...

bootstrap.yml

logging:
  config: http://configServerAddress:8888/yourAppName/yourSpringProfile/gitBranch/log4j2.xml
Run Code Online (Sandbox Code Playgroud)

log4j2.组件.属性

log4j.configurationFile=http://configServerAddress:8888/yourAppName/yourSpringProfile/gitBranch/log4j2.xml
log4j2.configurationUserName=guest
log4j2.configurationPassword=guest
Run Code Online (Sandbox Code Playgroud)