【问题标题】:ERROR :docker Spring boot container : java.lang.IllegalArgumentException: Could not resolve placeholder 'CASSANDRA_HOST' in value "${CASSANDRA_HOST}"错误:docker Spring boot 容器:java.lang.IllegalArgumentException:无法解析值“${CASSANDRA_HOST}”中的占位符“CASSANDRA_HOST”
【发布时间】:2021-12-22 07:32:30
【问题描述】:

我在 docker 容器中运行一个 spring boot 应用程序,它连接了另一个 Cassandra 的容器和一个 docker 网络(驱动桥)。问题是我使用了一个名为CASSANDRA_HOST的环境变量linux来保存Cassandra的容器IP和spring bootcould not resolve placeholder 'CASSANDRA_HOST' in value "${CASSANDRA_HOST}"

环境变量在 Spring Boot 容器中创建正常。我测试了其他环境变量,有些有效,有些无效。所以,我不明白错误在哪里。

错误:

 Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'CASSANDRA_HOST' in value "${CASSANDRA_HOST}"
                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.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:137)
                at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:133)
                at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:85)
                at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString(PropertySourcesPropertyResolver.java:74)
                at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:151)
                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.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175)
                at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936)
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1321)
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
                at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
                at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
                at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
                ... 59 more

application.properties中,我将变量设置如下:

cassandra.contactpoints= ${CASSANDRA_HOST}

CassandraCoonfig类中,我设置@value读取IP:

@Configuration 
public class CassandraConfig extends  AbstractCassandraConfiguration {

    @Value("${cassandra.contactpoints}")    
    private String contactPoints;

POM.XML 中,我使用标签< filtering >:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

编辑: 这是spring容器中所有环境变量的打印。 CASSANDRA_HOST 在第 14 行。

【问题讨论】:

  • 你在运行spring boot应用的docker容器中定义了环境变量了吗?
  • 是的,我愿意。我在 spring boot 容器和 docker-compose.yml 中设置了环境标签:environment: - CASSANDRA_HOST=${CASSANDRA_HOST}。你有什么想法吗?
  • 能否在容器内运行 bash,然后打印环境变量?这将有助于消除我认为的问题的一个潜在来源。这只会帮助确认 env var 是否实际设置。
  • 好的,我编辑了问题并添加了打印内容。如您所见,CASSANDRA_HOST 就在那里。太奇怪了。

标签: java spring-boot docker docker-compose cassandra-3.0


【解决方案1】:

我通过以下命令解决了在docker-compose.yml 中构建网桥的问题:

networks:
  net:
    driver: "bridge"

这是完整的docker-compose.yml

version: '3'

services:
  ... (your code)
  networks: 
    - net

networks:
      net:
        driver: "bridge"

使用 docker 映像中的此网络,在容器中检查网络,您将找到所需的 IP。然后用这个 IP 设置环境变量。

在我的场景中解决了。

【讨论】:

    猜你喜欢
    • 2018-02-20
    • 2018-06-17
    • 2021-04-09
    • 2012-09-03
    • 2016-01-20
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多