Java System.getenv无法解析docker-compose中定义的环境变量

时间:2020-01-09 19:05:20

标签: java docker-compose environment-variables

我有这个样本docker-compose.yml:

version: "2"

networks:
  default:
    external:
      name: simple-monitor_zeebe_network

services:
  ods:
    container_name: auto-auction-service
    image: cargeeks/auto-auction-service:SNAPSHOT
    environment:
      - SERVICE_LOG_LEVEL=debug
      - client.broker.contactPoint=172.20.0.6:26500
    ports:
      - "9200:7100"
    networks:
      - default

在微服务的某个地方,我有类似这样的代码,试图解析 client.broker.contactPoint 环境变量。

 String contactPoint = System.getenv("client.broker.contactPoint");
            if (StringUtils.isEmpty(contactPoint)) {
                _logger.info("Environment variable " + ENV_CONTACT_POINT + " is not defined.  Using default " + DEFAULT_CONTACT_POINT);
                contactPoint = DEFAULT_CONTACT_POINT;
            }

当我对上面定义的docker-compose.yml文件运行docker-compose up时,contactPoint始终定义为DEFAULT_CONTACT_POINT。

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

尝试不使用点分隔的变量名。某些环境不允许这样做,或者尝试将其解释为层次结构引用。在Docker-compose文件和代码中都使用下划线。