《重新定义Spring Cloud实战》——3.5.2 构建Multi Zone Eureka Serve
3.5.2 构建Multi Zone Eureka Server
前面的小节简单介绍了Eureka的Zone及Region设计,这里我们来演示下如何构建Multi Zone的Eureka Server,同时演示下默认的ZoneAffinity特性。
1. Eureka Server实例
这里我们启动四个Eureka Server实例,配置两个zone:zone1及zone2,每个zone都有两个Eureka Server实例,这两个zone配置在同一个region:region-east上。
它们的配置文件分别如代码清单3-9、代码清单3-10、代码清单3-11、代码清单3-12所示:
代码清单3-9 ch3-2\ch3-2-eureka-server\src\main\resources\application-zone1a.yml
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
metadataMap.zone: zone1
client:
register-with-eureka: true
fetch-registry: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-east: zone1,zone2
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
代码清单3-10 ch3-2\ch3-2-eureka-server\src\main\resources\application-zone1b.yml
server:
port: 8762
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
metadataMap.zone: zone1
client:
register-with-eureka: true
fetch-registry: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-east: zone1,zone2
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
代码清单3-11 ch3-2\ch3-2-eureka-server\src\main\resources\application-zone2a.yml
server:
port: 8763
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
metadataMap.zone: zone2
client:
register-with-eureka: true
fetch-registry: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-east: zone1,zone2
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
代码清单3-12 ch3-2\ch3-2-eureka-server\src\main\resources\application-zone2b.yml
server:
port: 8764
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
metadataMap.zone: zone2
client:
register-with-eureka: true
fetch-registry: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-east: zone1,zone2
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
上面我们配置了四个Eureka Server的配置文件,可以看到我们通过eureka.instance.metadataMap.zone设置了每个实例所属的zone。接下来分别使用这四个proflie启动这四个Eureka Server,如下:
mvn spring-boot:run -Dspring.profiles.active=zone1a
mvn spring-boot:run -Dspring.profiles.active=zone1b
mvn spring-boot:run -Dspring.profiles.active=zone2a
mvn spring-boot:run -Dspring.profiles.active=zone2b
2. Eureka Client实例
这里我们配置两个Eureka Client,分别属于zone1及zone2,其配置文件如代码清单3-13、代码清单3-14、代码清单3-15所示:
代码清单3-13 ch3-2\ch3-2-eureka-server\src\main\resources\application.yml
management:
endpoints:
web:
exposure:
include: '*'
这里我们暴露所有的endpoints,方便后面验证。
代码清单3-14 ch3-2\ch3-2-eureka-server\src\main\resources\application-zone1.yml
server:
port: 8081
spring:
application:
name: client
eureka:
instance:
metadataMap.zone: zone1
client:
register-with-eureka: true
fetch-registry: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-east: zone1,zone2
代码清单3-15 ch3-2\ch3-2-eureka-server\src\main\resources\application-zone2.yml
server:
port: 8082
spring:
application:
name: client
eureka:
instance:
metadataMap.zone: zone2
client:
register-with-eureka: true
fetch-registry: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-east: zone1,zone2
接着使用如下命令分别启动这两个client:
mvn spring-boot:run -Dspring.profiles.active=zone1
mvn spring-boot:run -Dspring.profiles.active=zone2
3. Zuul Gateway实例
这里我们新建一个zuul工程来演示Eureka使用metadataMap的zone属性时的ZoneAffinity特性。
配置文件如代码清单3-16、3-17所示:
代码清单3-16 ch3-2\ch3-2-zuul-gateway\src\main\resources\application-zone1.yml
server:
port: 10001
eureka:
instance:
metadataMap.zone: zone1
client:
register-with-eureka: true
fetch-registry: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-east: zone1,zone2
代码清单3-17 ch3-2\ch3-2-zuul-gateway\src\main\resources\application-zone2.yml
server:
port: 10002
eureka:
instance:
metadataMap.zone: zone2
client:
register-with-eureka: true
fetch-registry: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-east: zone1,zone2
接下来使用这两个profile分别启动gateway如下:
mvn spring-boot:run -Dspring.profiles.active=zone1
mvn spring-boot:run -Dspring.profiles.active=zone2
4.验证ZoneAffinity
访问http://localhost:10001/client/actuator/env,部分结果如下:
{
"activeProfiles": [
"zone1"
] //......
}
访问http://localhost:10002/client/actuator/env,部分结果如下:
{
"activeProfiles": [
"zone2"
] //......
}
可以看到,通过请求gateway的/client/actuator/env,访问的是Eureka Client实例的/actuator/env接口,处于zone1的gateway返回的activeProfiles为zone1,处于zone2的gateway返回的activeProfiles为zone2。从这个表象看gateway路由时对client的实例是ZoneAffinity的。有兴趣的读者可以去研读源码,看看gateway是如何实现Eureka的ZoneAffinity的。
- 点赞
- 收藏
- 关注作者
评论(0)