《重新定义Spring Cloud实战》——3.5.2 构建Multi Zone Eureka Serve

举报
华章计算机 发表于 2019/06/04 15:09:49 2019/06/04
【摘要】 本书摘自《重新定义Spring Cloud实战》——书中第3章,第3.5.2节,作者是许进、叶志远、钟尊发、蔡波斯、方志朋、郭芳碧、朱德明。

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的。


【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。