[英]How to check what values are consumed by a spring boot java application from the Linux command line or in the docker container
[英]How can I set parameters from docker command line to configure spring boot application?
我有一个带有yml的spring boot应用程序,可以在docker containter中配置它。 像这样的东西:
spring: application:
name: micro-example
config:
uri: ${vcap.services.config-service.credentials.uri:http://xxx.xxx.xx.73:8888}
正如你所看到的,我的配置中有一个ip硬编码,这是一个坏主意,因为编译仅适用于服务器。 是否存在外部化ip的方法,或者从docker命令行或更好的想法设置它?
有很多不同的方法可以做到:
1)设置环境变量(在shell中使用export VCAP_SERVICES_CONFIG-SERVICE_CREDENTIALS_URI='http://example.com'
,或在Dockerfile
ENV
)
2)将其作为JVM参数传递( java -Dvcap.services.config-service.credentials.uri=http://example.com -jar app.jar
)
3)将其作为命令行参数传递( java -jar app.jar --vcap.services.config-service.credentials.uri=http://example.com
)
4)Spring Boot还从config/application.properties
或application.properties
读取值,这些值与可执行JAR文件位于同一目录中,因此可以提供此文件(可以使用VOLUME
)并覆盖JAR中的设置
另见: http : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
从我的角度来看,你基本上有一些选择:
docker run -e SPRING_CONFIG_URI="http://xxx.xxx.xx.73:8888 user/image:tag"
环境变量传递,例如通过使用以下命令启动应用程序: docker run -e SPRING_CONFIG_URI="http://xxx.xxx.xx.73:8888 user/image:tag"
docker run --name configserver my/configserver:latest
并启动您的服务,然后使用链接docker run --link configserver:configserver my/service:latest
。 警告是你将被绑定到同一个docker主机。 http://configserver:8888
并使用docker run --add-host configserver:xxx.xxx.xx.73 user/image:tag
启动容器docker run --add-host configserver:xxx.xxx.xx.73 user/image:tag
docker run --dns=your-dns-server user/image:tag
注入一个可以作为运行容器的DNS服务器的自定义服务,并在dns服务器中注册您的依赖服务,该服务器可以具有动态配置,如consul或SkyDNS + etcd 。 而最后一种方法的好处是,您可以将容器动态链接到托管泊坞容器的跨节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.