1.多环境场景地域分布广泛,有多个机房和分布式注册中心;在开发过程中,项目一般分为local、dev、test、uat、prod等环境;开发者调试本地代码时,每个开发者调用本地服务,彼此独立,不影响各方;开发者调试微服务模块时,只需要启动需要调试的模块和网关,其他模块和接口自动调用预设环境中的接口。2.技术要点2.1YAML文件(.yml)中的key(key)通过变量引用设置,引用整个YAML配置来替换key,需要锚点。锚点用符号“&”定义并用符号“*”引用。有两种使用锚点的方法。一种是通过"<<:"一起引入键值对:test-db:&test-db-confighost:127.0.0.1port:3306user-db:<<:*test-db-config被解析后spring:test-db:host:127.0.0.1port:3306user-db:host:127.0.0.1port:3306说明:&后面的锚点名称可以自己定义,不一定要和配置数量的关键。动态生成Map中的Key,只导入配置的值原配置eureka:user:&euser${user.name}client:service-url:devZone:http://***/eureka/*euser:http://***/eureka/spring分析后eureka:user:liudehuaclient:service-url:devZone:http://***/eureka/liudehua:http://***/eureka/2.2Eureka的region和一个zoneregion一般表示一个地理分区,如北京、保定、杭州等;azone一般标识机房或机房分区,如北京通州机房、北京朝阳机房、北京通州机房A、北京通州机房B等。3.需求实现3.1分区部署架构图3.2服务注册器配置如果:现有两台eureka服务器的地址:1.http://localhost:8761/eurekaremote[远程部署,用于远程服务注册中心]2.http:///localhost:8762/eurekaLocal[远程部署,本地服务的注册中心]remoteSystem-Service服务配置eureka:instance:appname:${spring.application.name}preferIpAddress:trueleaseRenewalIntervalInSeconds:1leaseExpirationDurationInSeconds:1instanceId:${spring.application.name}:${spring.application.instance_id:${random.value}}metadata-map:profile:${spring.profiles.active}version:${info.project.version}#选择默认区域,将首先使用。当default区的服务宕机时,会转到第二区;区域:devZone客户端:启用:真ehealthcheck:enabled:truefetch-registry:trueregister-with-eureka:trueinstance-info-replication-interval-seconds:10registry-fetch-interval-seconds:10prefer-same-zone-eureka:true可用性区域:#devZone优先级高于devZone1dev:devZone,devZone1service-url:devZone:http://localhost:8761/eurekadevZone1:http://localhost:8762/eurekaregion:devlocalSystem-Service服务配置eureka:用户:&euser${user.name}实例:appname:${spring.application.name}preferIpAddress:trueleaseRenewalIntervalInSeconds:1leaseExpirationDurationInSeconds:1instanceId:${spring.application.name}:${spring.application.instance_id:${random.value}}metadata-map:profile:${spring.profiles.active}version:${info.project.version}#选择默认zone,优先使用,当默认zone下的服务为down之后,会去到第二区;zone:${user.name}client:enabled:truehealthcheck:enabled:truefetch-registry:trueregister-with-eureka:trueinstance-info-replication-interval-seconds:10registry-fetch-interval-seconds:10prefer-same-zone-eureka:trueavailability-zones:#${user.name}优先于devZonedev:${user.name},devZoneservice-url:devZone:http://localhost:8761/eureka*euser:http://localhost:8762/eurekaregion:devlocalGateway-Service服务配置eureka:user:&euser${user.name}instance:appname:${spring.application.name}preferIpAddress:trueleaseRenewalIntervalInSeconds:1leaseExpirationDurationInSeconds:1instanceId:${spring.application.name}:${spring.application.instance_id:${random.value}}元数据映射:配置文件:${spring.profiles.active}version:${info.project.version}#选择defaultzone,优先使用,当defaultzone下的服务宕机时,会转到第二个zone;zone:${user.name}client:enabled:truehealthcheck:enabled:truefetch-registry:trueregister-with-eureka:trueinstance-info-replication-interval-seconds:10registry-fetch-interval-seconds:10prefer-same-zone-eureka:trueavailability-zones:#${user.name}优先于devZonedev:${user.name},devZoneservice-url:devZone:http://localhost:8761/eureka*euser:http://localhost:8762/eurekaregion:dev通过以上配置可以实现需求3和4;通过不同的区域可以实现要求1和2;
