Docker Stack "configs" with Spring Boot 2



我认为这是一个相当简单的问题,但我还没有看到太多的例子或任何解释使用docker-configs(v3.3+(和将该配置加载到SpringBoot中以供参考之间联系的东西。

示例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:

示例群"服务配置">

我在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一起工作的,然而,事实证明,你需要为集群中"配置"条目中的"配置"指定一个目标,然后将其映射到你的容器中,并作为外部配置加载到你的spring应用程序中。在我的情况下,我不想覆盖我的春季启动应用程序中的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

然后,在我的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"configs条目:

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

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

相关内容

最新更新