在spring云中的bootstrap.yml上设置配置文件,以定位不同的配置服务器 [英] Set profile on bootstrap.yml in spring cloud to target different config server
问题描述
我使用docker撰写来运行我所有的微服务。对于每个服务,我给它一个短的主机名。
I use docker compose to run all my micro services. For each service I give it a short hostname.
version: '2'
services:
config:
image: springbox-config-server
restart: always
ports:
- "8890:8890"
discovery:
image: springbox-eureka
restart: always
ports:
- "8763:8763"
因此,在我的微服务中,我必须使用其短主机名来定位配置服务器。
Therefore, in my micro service I have to target the configserver with its short hostname.
spring:
application:
name: myservice
cloud:
config:
uri: http://config:8890
fail-fast: true
然而,当我在没有Docker的IDE中本地运行它们时,无法解析短主机名
However, when I run them locally in my IDE without docker, the short hostname can't be resolved.
所以我正在寻找一种解决方案来根据我的环境定位不同的配置服务器。
So I'm looking for a solution to target different config server according to my environment.
推荐答案
我找到解决方案。基本上,我们使用弹簧配置文件来丰富引导文件。例如
I find the solution. Basically, we use spring profile to enrich the bootstrap file. For example
spring:
application:
name: myservice
cloud:
config:
uri: http://config:8890
fail-fast: true
---
spring:
profiles: development
cloud:
config:
uri: http://localhost:8890
好消息是,我们不需要重写配置文件中的所有属性。默认属性是继承的。例如,当启用开发配置文件时,我的应用程序名称将从默认名为always myservice继承。
The good news is that we don't have to rewrite all properties in a profile. The default properties are inherited. For instance, when the development profile is enabled, my application name is inherited from the default one called always myservice.
要激活配置文件,请使用以下属性启动服务
To activate the profile, start the service with the following property
-Dspring.profiles.active=development
这篇关于在spring云中的bootstrap.yml上设置配置文件,以定位不同的配置服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!