带有Spring Boot 2的Docker堆栈“配置”

字节压缩

我认为这是一个相当简单的问题,但是我没有看到太多示例或任何真正的例子来说明使用docker configs(v 3.3+)和将该配置加载到Spring Boot中以供参考之间的联系。

样本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中。这必须足够简单..大声笑。谢谢!

字节压缩

很抱歉延迟回答此问题,或至少发布解决方案...

深入研究了configs与docker的工作方式,但是事实证明,您需要为swarm集群中“ configs”条目中保存的“ config”指定目标,然后将其映射到容器中,并且作为外部配置加载到您的spring应用程序。就我而言,我不想覆盖我的spring boot应用程序中的application.yml,只是想获取其他配置。所以我选择了以下设置:

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

可以说我创建了一个名为“ sample-config”的docker configs条目,并包含以下数据:

Configs Entry => "sample-config" 

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

然后,在我的撰写/堆栈文件中,我需要引用“ 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应用程序中的配置条目。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章