springboot+logback:占位符可以在本地解析,但不能在openshift中解析

7uzetpgm  于 2021-07-14  发布在  Java
关注(0)|答案(2)|浏览(464)

假设你有这种 logback-spring.xml 使用占位符(仅示例):

<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <springProperty name="appName" source="spring.application.name" scope="context"/>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
          <encoder>
             <pattern>${appName}</pattern>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>

如果你有这个文件在里面的话,这个工作就完美了 resources 或者通过 logging.level 财产。
但是,当我尝试通过configmap在openshift中绑定此文件时,会发生以下错误:

ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalArgumentException: Could not resolve placeholder 'appName' in value "<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <springProperty name="appName" source="spring.application.name" scope="context"/>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
          <encoder>
             <pattern>${appName}</pattern>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>
"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
    at org.springframework.core.env.AbstractPropertyResolver.resolveNestedPlaceholders(AbstractPropertyResolver.java:230)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:88)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:62)
    at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:535)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:308)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:281)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:216)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82)
    at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:362)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)
    at******
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

openshift中的configmap如下所示:

kind: ConfigMap
apiVersion: v1
metadata:

******

data:
  logback-spring.xml: |
    <configuration>
        <include resource="org/springframework/boot/logging/logback/base.xml"/>
        <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
        <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
        <springProperty name="appName" source="spring.application.name" scope="context"/>
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
              <encoder>
                 <pattern>${appName}</pattern>
            </encoder>
        </appender>
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
        </root>
    </configuration>

我在部署配置的“环境”部分以以下方式绑定该配置:

我做错什么了?提前谢谢!

rpppsulh

rpppsulh1#

而不是

<springProperty name="appName" source="spring.application.name" scope="context"/>

试试这个-

<springProperty name="appName" source="${bundle:application:spring.application.name}" scope="context"/>

如果您必须从configmap中读取某些内容作为属性,可以尝试以下操作-

<springProperty name="logLevel" source="${env:LOG_LEVEL:-INFO}" scope="context"/>

如果该属性不在配置Map中,我提供了一个默认值。

w7t8yxp5

w7t8yxp52#

我设法使它与卷一起工作。以下是部署配置的一部分:

spec:
  volumes:
    - name: logback-configmap-volume
      configMap:
        name: logback-configmap
  containers:
    - name: xxx
      volumeMounts:
        - name: logback-configmap-volume
          mountPath: /config
      env:
        - name: logging.config
          value: /config/logback-spring.xml

请告诉我是否可以不使用卷来实现相同的结果。

相关问题