ERROR :docker Spring boot container : java.lang.IllegalArgumentException: Could not resolve placeholder 'CASSANDRA_HOST' in value "${CASSANDRA_HOST}"

ERROR :docker Spring boot container : java.lang.IllegalArgumentException: Could not resolve placeholder 'CASSANDRA_HOST' in value "${CASSANDRA_HOST}"

我是 运行 docker 容器内的 spring 启动应用程序,它通过 docker 网络(驱动桥)链接另一个 Cassandra 容器。问题是我正在使用一个名为 CASSANDRA_HOST 的环境变量 linux 来保存 Cassandra 的容器 IP 和 spring 引导 could not resolve placeholder 'CASSANDRA_HOST' in value "${CASSANDRA_HOST}".

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

错误:

 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.getProperty(PropertySourcesPlaceholderConfigurer.java:137)
                at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.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[=10=](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}

在classCassandraCoonfig中,我设置了@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 行。

我使用以下命令解决了在 docker-compose.yml 中构建桥梁的问题:

networks:
  net:
    driver: "bridge"

这是完整的docker-compose.yml:

version: '3'

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

networks:
      net:
        driver: "bridge"

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

它在我的场景中解决了。