- 其实在微服务启动时,会去nacos读取多个配置文件,例如:
- [spring.application.name].yaml,例如:nacos-order-service.yaml
- [spring.application.name]-[spring.profiles.active].yaml,例如:nacos-order-service-dev.yaml
- 其中[spring.application.name].yaml不包含环境的配置信息,我们可以在这个配置文件里面配置相应的配置,这样可以实现多个环境的配置共享了
- 我们可以根据日志可以看出加载配置文件确实是读取多个nacos的远程配置信息
-
[WARN ] 2023-12-12 23:41:36,241 --main-- [com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder] Ignore the empty nacos configuration and get it based on dataId[nacos-order-service] & group[DEFAULT_GROUP] [WARN ] 2023-12-12 23:41:36,249 --main-- [com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder] Ignore the empty nacos configuration and get it based on dataId[nacos-order-service.yaml] & group[DEFAULT_GROUP] [INFO ] 2023-12-12 23:41:36,262 --main-- [org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration] Located property source: [BootstrapPropertySource {name='bootstrapProperties-nacos-order-service-dev.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacos-order-service.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacos-order-service,DEFAULT_GROUP'}]
-
- 创建两个环境的服务,一个dev、一个test,根据实际情况创建
- 简单看一下日志打印信息
- dev环境
-
[WARN ] 2023-12-12 23:59:34,688 --main-- [com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder] Ignore the empty nacos configuration and get it based on dataId[nacos-order-service] & group[DEFAULT_GROUP] [INFO ] 2023-12-12 23:59:34,713 --main-- [org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration] Located property source: [BootstrapPropertySource {name='bootstrapProperties-nacos-order-service-dev.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacos-order-service.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacos-order-service,DEFAULT_GROUP'}] [INFO ] 2023-12-12 23:59:34,724 --main-- [com.app.order.OrderApplication] The following profiles are active: dev
-
- test环境
-
[WARN ] 2023-12-13 00:00:08,837 --main-- [com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder] Ignore the empty nacos configuration and get it based on dataId[nacos-order-service] & group[DEFAULT_GROUP] [WARN ] 2023-12-13 00:00:08,858 --main-- [com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder] Ignore the empty nacos configuration and get it based on dataId[nacos-order-service-test.yaml] & group[DEFAULT_GROUP] [INFO ] 2023-12-13 00:00:08,859 --main-- [org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration] Located property source: [BootstrapPropertySource {name='bootstrapProperties-nacos-order-service-test.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacos-order-service.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacos-order-service,DEFAULT_GROUP'}] [INFO ] 2023-12-13 00:00:08,868 --main-- [com.app.order.OrderApplication] The following profiles are active: test
-
- dev环境
- 简单看一下nacos的配置的控制台信息,里面的内容根据实际情况配置
- 简单看一下日志打印信息
- 经过测试可以看出不管是dev,还是test环境,都可以读取到了nacos中nacos-order-service.yaml配置信息
- 当nacos、服务本地同时出现相同属性时,读取配置的优先级有高低之分
- nacos中的服务名称-profile.yaml大于nacos中的服务名称.yaml大于本地的配置信息
- 举例:远程的nacos-order-service-dev.yaml > 远程的nacos-order-service.yaml > 本地配置