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

时间:2016-02-09 11:23:26

标签: 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属性中插入凭据< / p>

我尝试过这样的事情,但是效果不好:

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

可能有一种替代方法,即当URL是该文件时,要求服务器忽略安全性,但如果有一天我必须具有访问它的身份验证,我将无法执行此操作。

有我的档案:

GIT

应用dev.yml

server:
 port: 55556

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

客户端

boostrap.yml

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

依赖关系(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>

服务器

application.yml

server:
  port: 8888

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

security:
  user:
    name: user
    password: foo

bootstrap.yml

spring:
  application:
    name: ConfigExtServer

依赖项(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> 

错误

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

我跟踪了错误,它出现在 reinitializeLoggingSystem PropertySourceBootstrapConfiguration 类中:

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");
        }

它进入了捕获,例外是:

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

提前感谢您的帮助,
罗曼

1 个答案:

答案 0 :(得分:2)

您可以在GitHub中将配置客户端设置为this sample

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

bootstrap.yml

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

log4j2.component.properties

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