【问题标题】:External logging configuration with Spring Cloud Config使用 Spring Cloud Config 进行外部日志记录配置
【发布时间】:2016-05-19 08:43:36
【问题描述】:


我刚刚体验了 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

可能有另一种选择,即当 URL 是该文件时,告诉服务器忽略安全性,但如果有一天我必须通过身份验证才能访问它,我将无法做到这一点。

这是我的文件:

GIT

application-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

我跟踪了错误,它出现在 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");
        }

进入catch,异常是:

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

提前感谢您的帮助,
罗曼

【问题讨论】:

  • 您找到解决问题的方法了吗??

标签: spring-security log4j2 spring-cloud


【解决方案1】:

您可以在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 2016-04-06
    • 2016-07-21
    • 2018-05-15
    • 2012-11-04
    • 2017-12-20
    相关资源
    最近更新 更多