一、多环境场景
- 地理分布广泛,多机房,分布式注册中心实现;
- 开发过程中,项目一般分为 local,dev,test,uat,prod等环境;
- 开发人员在调试本地代码时,每个开发人员调用本地服务,相互独立,不影响各方;
- 开发人员调试微服务模块时,只需要启动需要调试的模块和网关即可,其他模块和接口自动调用预设环境中接口。
二、技术点
2.1 YAML文件(.yml)中的键(key)通过变量引用来设置
引用整块YAML配置
替换键,则需要用到锚点。锚点用符号“&”定义,并用符号“*”进行引用。
锚点有两种使用方式,一是通过“<<:”将键值对一起引入:test-db: &test-db-config host: 127.0.0.1 port: 3306 user-db: <<: *test-db-config
经过Spring解析之后:
test-db: host: 127.0.0.1 port: 3306 user-db: host: 127.0.0.1 port: 3306
说明:&后面的锚点名字,可自己定义,不必与配置量的键相同。
动态生成Map中的Key,仅引入配置的值
原始配置
eureka: user: &euser ${user.name} client: service-url: devZone: http://***/eureka/ *euser: http://***/eureka/
经过spring解析之后
eureka: user: liudehua client: service-url: devZone: http://***/eureka/ liudehua: http://***/eureka/
2.2 Eureka的region和zone
- region 一般表示地理上的分区,比如北京分区,保定分区,杭州分区等;
- zone 一般标识机房或者机房分区,比如北京通州机房,北京朝阳机房,或者 北京通州机房A区,北京通州机房B区等等表述;
三、需求实现
3.1 分区部署架构图
3.2 服务注册方配置
假如:现有两台eureka服务器的地址:
1、http://localhost:8761/eureka 远端 【远端部署,为远端服务的注册中心】
2、http://localhost:8762/eureka 本地 【远端部署,为本地服务的注册中心】
远端System-Service服务配置
eureka: instance: appname: ${spring.application.name} preferIpAddress: true leaseRenewalIntervalInSeconds: 1 leaseExpirationDurationInSeconds: 1 instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}} metadata-map: profile: ${spring.profiles.active} version: ${info.project.version} #选择默认的zone,会被优先使用,当默认的zone下的服务宕机后,会走到第二个zone; zone: devZone client: enabled: true healthcheck: enabled: true fetch-registry: true register-with-eureka: true instance-info-replication-interval-seconds: 10 registry-fetch-interval-seconds: 10 prefer-same-zone-eureka: true availability-zones: # devZone 优先级高于 devZone1 dev: devZone,devZone1 service-url: devZone: http://localhost:8761/eureka devZone1: http://localhost:8762/eureka region: dev
本地System-Service服务配置
eureka: user: &euser ${user.name} instance: appname: ${spring.application.name} preferIpAddress: true leaseRenewalIntervalInSeconds: 1 leaseExpirationDurationInSeconds: 1 instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}} metadata-map: profile: ${spring.profiles.active} version: ${info.project.version} #选择默认的zone,会被优先使用,当默认的zone下的服务宕机后,会走到第二个zone; zone: ${user.name} client: enabled: true healthcheck: enabled: true fetch-registry: true register-with-eureka: true instance-info-replication-interval-seconds: 10 registry-fetch-interval-seconds: 10 prefer-same-zone-eureka: true availability-zones: # ${user.name} 优先级高于 devZone dev: ${user.name},devZone service-url: devZone: http://localhost:8761/eureka *euser: http://localhost:8762/eureka region: dev
本地Gateway-Service服务配置
eureka: user: &euser ${user.name} instance: appname: ${spring.application.name} preferIpAddress: true leaseRenewalIntervalInSeconds: 1 leaseExpirationDurationInSeconds: 1 instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}} metadata-map: profile: ${spring.profiles.active} version: ${info.project.version} #选择默认的zone,会被优先使用,当默认的zone下的服务宕机后,会走到第二个zone; zone: ${user.name} client: enabled: true healthcheck: enabled: true fetch-registry: true register-with-eureka: true instance-info-replication-interval-seconds: 10 registry-fetch-interval-seconds: 10 prefer-same-zone-eureka: true availability-zones: # ${user.name} 优先级高于 devZone dev: ${user.name},devZone service-url: devZone: http://localhost:8761/eureka *euser: http://localhost:8762/eureka region: dev
通过上述配置可以实现需求3,4;
通过不同的region可以实现需求 1,2;
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。