gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:42:05 26 4
gpt4 key购买 nike

我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com