一、多環境場景
- 地理分佈廣泛,多機房,分散式註冊中心實現;
- 開發過程中,專案一般分為 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;