文章目录
在微服务架构中,backend模块会定义一个基础的配置文件,在每个微服务中,也会定义一个自己的配置文件,每个微服务通过继承方式,每个服务都需要的内容放在backend模块,避免在每个微服务中重复定义,减少管理成本。
在实际工作中,本地开发、测试和上线环节,需要使用不一样的配置信息运行服务,再加上有些服务要同时支持国内和海外,需要管理的配置信息就达到了3 * 2=6套,有的甚至同一个场景下,提供了多种资源选择,如何控制在对应运行环境下加载正确的配置信息?
举例分析
下面是公司的一个实际项目中所有配置文件,
项目结构如下
<modules>
<module>noodles-user-common</module>
<module>noodles-user-client</module>
<module>noodles-user-server</module>
<module>noodles-user-job</module>
<module>noodles-user-backend</module>
</modules>
其中noddles-user-server(提供http和rpc接口)、noddles-user-job(定时任务)是两个可以独立运行的服务。
noddles-user-backend 两个配置文件
application.yaml
authFilter:
appKey: Ape
internationalEnabled: true
defaultProductId: 1000
# ServiceInstanceMeta
serviceInstanceMeta:
serviceName: noddles-user
security.filterOrder: 20
# MySQL 连接配置
noddlesUser.mysql:
dynamicConfigEnabled: true
dynamicConfigKey: noddles_user-pyIXkE-test
dynamicConfigGroup: op-dba-mysql-conf
readWriteSplit: true
testOnBorrow: true
validationQuery: SELECT 1
# 数据库自动初始化 默认关闭
spring.datasource.initialize: false
# Actuator 配置
actuator:
project: noddles-user
fdc.projectIdentity: noddles-user
spring.profiles.active: local,sg-test,soho-test
logging.access.directory: /home/shared/log
spring:
profiles:
include: backend-oversea
# 本地环境
---
spring:
profiles