1. Multi-environment scenarios
- Wide geographical distribution, multiple computer rooms, distributed registration center implementation;
- During the development process, the project is generally divided into local, dev, test, uat, prod and other environments;
- When developers debug local code, each developer calls local services, which are independent of each other and do not affect all parties;
- When developers debug microservice modules, they only need to start the modules and gateways that need to be debugged, and other modules and interfaces automatically call the interfaces in the preset environment.
2. Technical points
2.1 The key (key) in the YAML file (.yml) is set by variable reference
Reference the whole block of YAML configuration
To replace the key, you need to use the anchor point. Anchors are defined with the symbol "&" and referenced with the symbol "*".
There are two ways to use anchors. One is to introduce key-value pairs together through "<<:":test-db: &test-db-config host: 127.0.0.1 port: 3306 user-db: <<: *test-db-config
After Spring parsing:
test-db: host: 127.0.0.1 port: 3306 user-db: host: 127.0.0.1 port: 3306
Description: The anchor name after & can be defined by yourself, and does not have to be the same as the key of the configuration.
Dynamically generate the Key in the Map, and only introduce the configured value
original configuration
eureka: user: &euser ${user.name} client: service-url: devZone: http://***/eureka/ *euser: http://***/eureka/
After parsing by spring
eureka: user: liudehua client: service-url: devZone: http://***/eureka/ liudehua: http://***/eureka/
2.2 Eureka's region and zone
- region generally refers to geographical divisions, such as Beijing division, Baoding division, Hangzhou division, etc.;
- zone generally identifies the computer room or computer room partition, such as Beijing Tongzhou computer room, Beijing Chaoyang computer room, or Beijing Tongzhou computer room A area, Beijing Tongzhou computer room B area, etc.;
3. Demand realization
3.1 Partition Deployment Architecture Diagram
3.2 Service Registrar Configuration
Suppose: the addresses of two existing eureka servers:
1. http://localhost:8761/eureka remote [remote deployment, registration center for remote services]
2. http://localhost:8762/eureka local [remote deployment, registration center for local services]
Remote System-Service Service Configuration
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
Local System-Service service configuration
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
Local Gateway-Service service configuration
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
Requirements 3 and 4 can be achieved through the above configuration;
Requirements 1 and 2 can be achieved through different regions;
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。