为什么环境变量在application.yml中不起作用?

问题描述 投票:0回答:1

我有以下代码

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        System.out.println("The Env Var is "+ ((System.getenv("DOCKER_IP") == null || System.getenv("DOCKER_IP").equals("")) ? "Not Defined" : System.getenv("DOCKER_IP")));
        SpringApplication.run(Application.class, args);
    }
}
spring:
  cassandra:
    keyspace-name: "test"
    local-datacenter: "datacenter1"
    port: 9042
    contact-points:
      - ${DOCKER_IP}

当我运行时,我在控制台语句中看到 IP

环境变量为 192.168.56.101

但它尝试使用 127.0.0.1...

2023-10-30T14:10:50.073-04:00 WARN 22688 --- [ restartedMain] c.d.o.d.internal.core.ContactPoints :忽略无效的联系点 ${DOCKER_IP}:9042 (未知主机 ${DOCKER_IP}) 抑制:io.netty.channel.AbstractChannel$AnnotatedConnectException:连接被拒绝:没有更多信息:/127.0.0.1:9042 导致:java.net.ConnectException:连接被拒绝:没有更多信息

为什么会发生这种情况以及如何让它注入值?

如果我对它的值进行硬编码,那么这是我当前的解决方法,但我需要它在没有硬编码的情况下工作。

spring-boot cassandra
1个回答
0
投票

我认为这可能是一个错误,另一种方法是覆盖默认的弹簧键

export SPRING-CASSANDRA-CONTACT-POINTS = 192.168.56.101
© www.soinside.com 2019 - 2024. All rights reserved.