spring-boot - 错误 :docker Spring boot container : java. lang.IllegalArgumentException:无法解析值 'CASSANDRA_HOST' 中的占位符 "${CASSANDRA_HOST}"

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

我在 docker 容器内运行一个 spring boot 应用程序,它通过 docker 网络(驱动桥)将另一个 Cassandra 容器链接起来。问题是我正在使用一个名为 CASSANDRA_HOST 的环境变量 linux保存 Cassandra 的容器 IP 和 spring boot could 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 行。

enter image description here

最佳答案

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

networks:
  net:
    driver: "bridge"

这是完整的docker-compose.yml:

version: '3'

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

networks:
      net:
        driver: "bridge"

在容器中的 docker 镜像中有了这个网络,您可以检查网络并找到所需的 IP。然后用这个 IP 设置环境变量。

它在我的场景中解决了。

关于spring-boot - 错误 :docker Spring boot container : java. lang.IllegalArgumentException:无法解析值 'CASSANDRA_HOST' 中的占位符 "${CASSANDRA_HOST}",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69900773/

相关文章:

mongodb - docker compose 中 mongodb 的卷配置

mysql - tomcat + mysql + war 使用 docker-compose.yml

java - 如何在Spring boot中针对不同文件中的不同日志级别进行日志记录

hibernate - 使用 spring boot 和 cloud foundry 将 Redis 集成为非平台 oracle 数据库的 Hibernate 二级缓存

java - 为什么我的 spring 应用程序不断打开与云图集 mongoDB 的新连接?

java - 未使用自定义 DefaultTokenServices bean

docker - 什么是 .dockerfile 扩展名?

docker - 启动 Docker 桌面时 IntelliJ 弹出 "Cannot execute command"

Windows 容器 - 如何提交容器

docker - 如何暴露mysql端口?