首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:docker容器: java.lang.IllegalArgumentException:未能解析值"${CASSANDRA_HOST}“中的占位符'CASSANDRA_HOST‘

错误:docker容器: java.lang.IllegalArgumentException:未能解析值"${CASSANDRA_HOST}“中的占位符'CASSANDRA_HOST‘
EN

Stack Overflow用户
提问于 2021-11-09 15:25:18
回答 2查看 900关注 0票数 3

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

环境变量是在Spring容器中创建的。我测试了其他的环境变量,有些工作了,而其他的则不起作用。所以,我不明白哪里有错误。

错误:

代码语言:javascript
运行
复制
 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,中,我将变量设置如下:

代码语言:javascript
运行
复制
cassandra.contactpoints= ${CASSANDRA_HOST}

在类CassandraCoonfig,中,我将@值设置为读取IP:

代码语言:javascript
运行
复制
@Configuration 
public class CassandraConfig extends  AbstractCassandraConfiguration {

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

POM.XML中,我使用的是标记< filtering >

代码语言:javascript
运行
复制
<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-23 13:48:04

我用下面的命令解决了这个问题,在docker-compose.yml中构建了一座桥:

代码语言:javascript
运行
复制
networks:
  net:
    driver: "bridge"

这是完整的docker-compose.yml

代码语言:javascript
运行
复制
version: '3'

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

networks:
      net:
        driver: "bridge"

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

这在我的设想中解决了。

票数 1
EN

Stack Overflow用户

发布于 2022-07-26 05:12:20

我的解决办法是

代码语言:javascript
运行
复制
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
    PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    placeholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
    return placeholderConfigurer;
}

设置是否忽略不可解析的占位符。默认值为"false":如果占位符无法解析,将引发异常。将此标志切换为"true“,以保留占位符字符串在这种情况下的状态,将其留给其他占位符配置器来解决。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69900773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档