0
点赞
收藏
分享

微信扫一扫

速盾:cdn部署在哪?

mm_tang 2024-07-24 阅读 25

文章目录


在微服务架构中,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: local

rpcServer.zkNode: false
logging.access.enabled: false
actuator.enabled: false

---
# 测试服务环境
spring:
  profiles: test

logging.config: classpath:log4j2.test.yaml

---
# 线上服务环境
spring:
  profiles: online

remind.env: 线上

---
# 测试服务环境
spring:
  profiles: bj-test

alimq.topics:
  userLogin:
    cluster: noddles_test
    topic: JIALIDUN_NOODLES_USER_LOGIN_TEST
    consumerId: GID_JIALIDUN_NOODLES_USER_LOGIN_TEST

---
# 线上服务环境
spring:
  profiles: bj-online

noddlesUser.mysql:
  dynamicConfigEnabled: true
  dynamicConfigKey: noddles-user-online-noddles_user-online
  dynamicConfigGroup: op-dba-mysql-conf
  readWriteSplit: true
  testOnBorrow: true
  validationQuery: SELECT 1

alimq.topics:
  userLogin:
    cluster: noddles_online
    topic: JIALIDUN_NOODLES_USER_LOGIN_ONLINE
    consumerId: GID_JIALIDUN_NOODLES_USER_LOGIN_ONLINE

---
# 大兴测试机房
spring:
  profiles: soho-test

# ZooKeeper 配置
zkServers: zk1-test,zk2-test,zk3-test,zk4-test,zk5-test

---
# 廊坊测试机房
spring:
  profiles: rz-test

# ZooKeeper 配置
zkServers: zk1-test,zk2-test,zk3-test,zk4-test,zk5-test

---
# 大兴线上机房
spring:
  profiles: dx-online

zkServers: dx-zk1,dx-zk2,dx-zk3,dx-zk4,dx-zk5

---
# 廊坊线上机房
spring:
  profiles: lf-online

zkServers: lf-zk1,lf-zk2,lf-zk3,lf-zk4,lf-zk5

---
# 阿里新加坡线上机房
spring:
  profiles: alisg-online

zkServers: lf-zk1,lf-zk2,lf-zk3,lf-zk4,lf-zk5

application-backend-oversea.yaml

# 海外测试服务环境
spring:
  profiles: sg-test

noddlesUser.mysql:
  dynamicConfigEnabled: true
  dynamicConfigKey: noddles_sg_user-NwQVE1-test
  dynamicConfigGroup: op-dba-mysql-conf
  readWriteSplit: true
  testOnBorrow: true
  validationQuery: SELECT 1
  
alimq.topics:
  userLogin:
    cluster: noddles_sg_test
    topic: JIALIDUN_NOODLES_USER_LOGIN_TEST
    consumerId: GID_JIALIDUN_NOODLES_USER_LOGIN_TEST

---
# 海外线上服务环境
spring:
  profiles: sg-online

noddlesUser.mysql:
  dynamicConfigEnabled: true
  dynamicConfigKey: noddles_sg_user-r5vJDb-online
  dynamicConfigGroup: op-dba-mysql-conf
  readWriteSplit: true
  testOnBorrow: true
  validationQuery: SELECT 1

alimq.topics:
  userLogin:
    cluster: noddles_sg_online
    topic: JIALIDUN_NOODLES_USER_LOGIN_ONLINE
    consumerId: GID_JIALIDUN_NOODLES_USER_LOGIN_ONLINE

---
# 测试环境海外机房
spring:
  profiles: qcbj3-test

# ZooKeeper 配置
zkServers: soho-zk1,soho-zk2,soho-zk3,soho-zk4,soho-zk5

---
# 线上海外虚拟机房
spring:
  profiles: alibj1-online

zkServers: lf-zk1,lf-zk2,lf-zk3,lf-zk4,lf-zk5

---
# 线上新加坡机房
spring:
  profiles: alisg-online

zkServers: lf-zk1,lf-zk2

noddles-user-job 配置文件

spring:
  profiles:
    include: backend

common.xxl.job.executor:
  appname: noddles-user-job

mailer:
  biz: noddles

---
# 配置类型:region+环境相关配置
# 配置用途:用于同时与region和环境相关的配置,或者仅与region相关的配置
# region-环境:新加坡-测试
spring:
  profiles: sg-test

common.xxl.job.executor:
  appname: noddles-user-job-sg

---
# 配置类型:region+环境相关配置
# 配置用途:用于同时与region和环境相关的配置,或者仅与region相关的配置
spring
  profiles: sg-online

common.xxl.job.executor:
  appname: noddles-user-job-sg

noddles-user-server 配置文件

authFilter:
  appKey: Ape
  internationalEnabled: true
  defaultProductId: 1000

# ServiceInstanceMeta
serviceInstanceMeta:
  serviceName: noddles-user

common.xxl.job.executor:
  appname: noddles-user

# Web Server 配置
server:
  port: 8080
  tomcat:
    maxThreads: 500

# Rpc Server 配置
rpcServer:
  port: 5000
  zkNode: /noddles-user/rpc/v1 
---
spring.profiles.include: backend

---
# 配置类型:region+环境相关配置
# 配置用途:用于同时与region和环境相关的配置,或者仅与region相关的配置
# region-环境:新加坡-测试
spring:
  profiles: sg-test

common.xxl.job.executor:
  appname: noddles-user-sg

---
# 配置类型:region+环境相关配置
# 配置用途:用于同时与region和环境相关的配置,或者仅与region相关的配置
# region-环境:新加坡-线上
spring:
  profiles: sg-online

common.xxl.job.executor:
  appname: noddles-user-sg

问题:server和Job启动时对应加载的数据库配置为哪一个?

先公布答案时noddles_sg_user-NwQVE1-test这个数据库,位于soho-test环境下。

以noodles-user-server项目启动为例,对数据配置扫描加载顺序进行分析。

启动noodles-user-server,加载对应配置信息,扫描到spring.profiles.include: backend,解析并合并application-backend.yaml中配置信息(springBoot按照规定进行二者的关联和查找),在application-backend.yaml,指明了在默认情况下要使用环境spring.profiles.active: local,sg-test,soho-test,这样配置之后,会将公共配置信息和 local,sg-test,soho-test配置信息都进行扫描加载,相同配置项,后加载的覆盖之前加载,公共配置的数据库为noddles_user-pyIXkE-test,local没有配置数据库,soho-test也没有配置数据库信息,sg-test配置了noddles_sg_user-NwQVE1-test,因此最终生效的是noddles_sg_user-NwQVE1-test数据库,详细扫描并加载数据配置顺序如下图:
在这里插入图片描述

总结

在多文件,多profile环境下,配置信息扫描加载顺序为「同一个文件,从上到下-》遇到include,递归扫描加载-〉spring.profiles.active多个环境,从左到右依次扫描加载」,后加载的总是覆盖先加载的。

举报

相关推荐

速盾:cdn转发ip

速盾:cdn配置ssl

速盾:cdn 刷新时间

速盾:海外cdn高防

速盾:cdn能防御ddos吗?

速盾:cdn和udp的区别?

速盾:cdn都能防御哪些攻击?

0 条评论