Docker 堆栈 "configs" 与 Spring 启动 2

Docker Stack "configs" with Spring Boot 2

我认为这是一个相当简单的问题,但是我没有看到太多示例或任何解释使用 docker 配置(v 3.3+)和将该配置加载到 Spring开机参考

样本docker-stack.yml

version: '3.3'
services:
  test-service:
    image: myrepo/test-service:1.0.0
    configs:
      - service-config
    networks:
      - test-network
configs:
  service-config:
    external:true

networks:
  test-network:

样本群"service-config".

我在 Portainer 中将其作为新 "configs" 条目输入。

services:
  test-service:
    key1: sample value
    key2: sample two

我正在尝试将此配置加载到 Spring 中,这样我就可以在组件中引用此配置中的值。

通过@ConfigurationProperties

@ConfigurationProperties("services.test-service")
public MyBeanName myBean() { return new MyBeanName(); }

或通过@Value:

@Value("${services.test-service.key1}")
private String key1;

如何将此 docker "configs" 配置加载到 Spring。这必须足够简单.. 大声笑。谢谢!

很抱歉延迟回复这个问题,或者至少发布解决方案...

我们花了更多时间研究配置如何与 docker 一起工作,但事实证明您需要为 "configs" 条目中的 "config" 指定一个目标您的 swarm 集群,然后将其映射到您的容器中,并作为外部配置加载到您的 spring 应用程序。就我而言,我不想覆盖 spring 启动应用程序中的 application.yml,只是想获取额外的配置。所以我选择了设置:

--spring.config.additional-location=file:/configs/sample-config.yml

假设我创建了一个名为 "sample-config" 的 docker 配置项并具有以下数据:

Configs Entry => "sample-config" 

services:
  test-service:
    key1: sample value
    key2: sample two

然后在我的 compose/stack 文件中,我需要引用 "configs" 条目,并提供与我在 "spring.config.additional-location" 设置中指定的文件相对应的目标文件。

version: '3.3'

services:

  test-service:
    image: myrepo/test-service:1.0.0
    configs:
      - source: sample-config
        target: /configs/sample-config.yml
    networks:
      - test-network

configs:
  sample-config:
    external:true

networks:
  test-network:

然后在我的 Dockerfile 中,我将指定以下内容以在启动 jar/app 时加载 "sample-config" 配置条目:

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar", "--spring.config.additional-location=file:/configs/sample-config.yml"]

这允许我访问配置条目,这些条目使用@Value 和@ConfigurationProperties 注释从外部加载到我的spring 应用程序。