Eureka分区集群部署

举报
李子捌 发表于 2021/11/09 09:11:43 2021/11/09
【摘要】 1、简介Eureka集群提供了分区功能,这种功能设计理念来源于亚马逊云AWS创造的两个基础概念region(地域)和zone(可用区):region:地域就是物理意义上的不同地域,在服务部署时会选择在用户需求大的区域建设多个机房并部署服务,这样可以减少网络不稳定带来的问题zone:一个region下可以划分为多个zone。也就是说一个区域的多个机房服务器,按照一定规则划分为不同的可用区(比如...

1、简介

Eureka集群提供了分区功能,这种功能设计理念来源于亚马逊云AWS创造的两个基础概念region(地域)和zone(可用区)

  • region:地域就是物理意义上的不同地域,在服务部署时会选择在用户需求大的区域建设多个机房并部署服务,这样可以减少网络不稳定带来的问题
  • zone:一个region下可以划分为多个zone。也就是说一个区域的多个机房服务器,按照一定规则划分为不同的可用区(比如说一个区域有三个机房,这三个机房就可以划分为三个zone)。通过划分zone,可以达到容灾的效果,如果一个zone故障,其他zone仍然可以对外提供服务。

结合上面两个概念可知,当公司存在Eureka Server分地域部署的时候,我们可以采用Eureka集群提供的分区功能。这样可以保证某一个region中的zone注册的服务优先于另一个zone中注册的服务被调用,当前者不可用时,再去选择其他zone中注册的服务发起调用,这样可以保证服务调用的延迟降低。


Region和Zone的关系图:


2、集群部署

2.1 资源清单

本次一共搭建四台Eureka Server服务,其资源清单如下所示:

application-name

zone

instance-hostname

prot

Eureka-Ynatian

zone-yantian

eureka18881.com

18881

Eureka-Luohu

zone-luohu

eureka18882.com

18882

Eureka-Nanshan

zone-yantian

eureka18883.com

18883

Eureka-Baoan

zone-baoan

eureka18884.com

18884

Eureka Server Cluster集群图(Region-深圳、zone-罗湖、zone-盐田、zone-南山、zone-宝安):

这里搭建的Eureka Server Cluster 是单个Region,包含四个zone,如果公司的需要的Eureka Server高可用要求特别高,可以在Region的每个zone中部署多个Eureka Server服务器。需要做分区域,参照Region Shenzhen配置多个Region即可。


2.2 配置文件

Eureka-Ynatian的Eureka Server配置文件

server:
  port: 18881

spring:
  application:
    name: Eureka-Ynatian

eureka:
  instance:
    hostname: eureka18881.com
  client:
    prefer-same-zone-eureka: true
    register-with-eureka: true
    region: shenzhen
    availability-zones:
      shenzhen: zone-yantian,zone-luohu,zone-nanshan,zone-baoan
    fetch-registry: true
    service-url:
      zone-yantian: http://eureka18881.com:18881/eureka/
      zone-luohu: http://eureka18882.com:18882/eureka/
      zone-nanshan: http://eureka18883.com:18883/eureka/
      zone-baoan: http://eureka18884.com:18884/eureka/

Eureka-Luohu的Eureka Server配置文件

server:
  port: 18882

spring:
  application:
    name: Eureka-Luohu

eureka:
  instance:
    hostname: eureka18882.com
  client:
    prefer-same-zone-eureka: true
    register-with-eureka: true
    region: shenzhen
    availability-zones:
      shenzhen: zone-luohu,zone-yantian,zone-nanshan,zone-baoan
    fetch-registry: true
    service-url:
      zone-yantian: http://eureka18881.com:18881/eureka/
      zone-luohu: http://eureka18882.com:18882/eureka/
      zone-nanshan: http://eureka18883.com:18883/eureka/
      zone-baoan: http://eureka18884.com:18884/eureka/

Eureka-Nanshan的Eureka Server配置文件

server:
  port: 18883

spring:
  application:
    name: Eureka-Nanshan

eureka:
  instance:
    hostname: eureka18883.com
  client:
    prefer-same-zone-eureka: true
    register-with-eureka: true
    region: shenzhen
    availability-zones:
      shenzhen: zone-nanshan,zone-luohu,zone-yantian,zone-baoan
    fetch-registry: true
    service-url:
      zone-yantian: http://eureka18881.com:18881/eureka/
      zone-luohu: http://eureka18882.com:18882/eureka/
      zone-nanshan: http://eureka18883.com:18883/eureka/
      zone-baoan: http://eureka18884.com:18884/eureka/

Eureka-Baoan的Eureka Server配置文件

server:
  port: 18884

spring:
  application:
    name: Eureka-Baoan

eureka:
  instance:
    hostname: eureka18884.com
  client:
    prefer-same-zone-eureka: true
    register-with-eureka: true
    region: shenzhen
    availability-zones:
      shenzhen: zone-baoan,zone-nanshan,zone-luohu,zone-yantian
    fetch-registry: true
    service-url:
      zone-yantian: http://eureka18881.com:18881/eureka/
      zone-luohu: http://eureka18882.com:18882/eureka/
      zone-nanshan: http://eureka18883.com:18883/eureka/
      zone-baoan: http://eureka18884.com:18884/eureka/


2.3 启动集群

依次启动集群中四个Eureka Server服务,等待集群之间稳定后,访问Eureka Server的Dashboard,此时在四台Eureka Server中的任意一台都可以看到相同的服务注册信息Instance currently registered with Eureka,以及DS Replicaes信息(这个不相同哦,不需要复制自己……),此时说明Eureka Server Cluster已经正常启动了。


2.4 服务注册

Eureka Server分区本质上除了达到高可用之外,最主要是为了给客户端提供最优的服务,以此来达到服务的最快响应。在Eureka Server中注册两个服务Server-01和Server-02,Server-01中metadata-map zone选择zone-yantian,Server-02中metadata-map zone选择zone-louhu。(注意两个服务的服务名均为Server)


Server-01配置文件

server:
  port: 19991

spring:
  application:
    name: Server

eureka:
  instance:
    prefer-ip-address: true
    metadata-map:
      zone: zone-yantian
  client:
    region: shenzhen
    availability-zones:
      shenzhen: zone-yantian,zone-luohu,zone-nanshan,zone-baoan
    service-url:
      zone-yantian: http://eureka18881.com:18881/eureka/
      zone-luohu: http://eureka18882.com:18882/eureka/
      zone-nanshan: http://eureka18883.com:18883/eureka/
      zone-baoan: http://eureka18884.com:18884/eureka/
    fetch-registry: true
    register-with-eureka: true
    prefer-same-zone-eureka: true

Server-01中提供一个Rest Api端点

@RestController
@RequestMapping("/zone")
public class ZoneController {

    @GetMapping
    public String zone() {
        return "Server zone-yantian";
    }

}

Server-02配置文件

server:
  port: 19992

spring:
  application:
    name: Server

eureka:
  instance:
    prefer-ip-address: true
    metadata-map:
      zone: zone-luohu
  client:
    region: shenzhen
    availability-zones:
      shenzhen: zone-luohu,zone-yantian,zone-nanshan,zone-baoan
    service-url:
      zone-yantian: http://eureka18881.com:18881/eureka/
      zone-luohu: http://eureka18882.com:18882/eureka/
      zone-nanshan: http://eureka18883.com:18883/eureka/
      zone-baoan: http://eureka18884.com:18884/eureka/
    fetch-registry: true
    register-with-eureka: true
    prefer-same-zone-eureka: true

Server-02中提供一个Rest Api端点

@RestController
@RequestMapping("/zone")
public class ZoneController {

    @GetMapping
    public String zone() {
        return "Server zone-luohu";
    }

}


此时访问Eureka dashboard可以看到Server 有两个实例注册到了Eureka Server Cluster中。


2.4 服务消费

创建一个服务消费者Consumer-01,用于测试服务调用分发的具体情况。

Consumer-01配置文件,其中指定metadata-map.zone为zone-luohu

server:
  port: 17771

spring:
  application:
    name: Consumer-01

eureka:
  instance:
    prefer-ip-address: true
    metadata-map:
      zone: zone-luohu
  client:
    region: shenzhen
    availability-zones:
      shenzhen: zone-luohu,zone-yantian,zone-nanshan,zone-baoan
    service-url:
      zone-yantian: http://eureka18881.com:18881/eureka/
      zone-luohu: http://eureka18882.com:18882/eureka/
      zone-nanshan: http://eureka18883.com:18883/eureka/
      zone-baoan: http://eureka18884.com:18884/eureka/
    fetch-registry: true
    register-with-eureka: true
    prefer-same-zone-eureka: true

提供一个Rest Api访问端点:

@RestController
@RequestMapping("/consumer")
public class ZoneController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping
    public String zone() {
        return restTemplate.getForObject("http://server/zone", String.class);
    }

}

在浏览器访问该端点,无论怎么刷新都会输出Server zone-louhu,说明请求一直打到了Server-02

修改Consumer-01的配置文件,修改metadata-map:zone为zone-yantian

server:
  port: 17771

spring:
  application:
    name: Consumer-01

eureka:
  instance:
    prefer-ip-address: true
    metadata-map:
      zone: zone-yantian
  client:
    region: shenzhen
    availability-zones:
      shenzhen: zone-luohu,zone-yantian,zone-nanshan,zone-baoan
    service-url:
      zone-yantian: http://eureka18881.com:18881/eureka/
      zone-luohu: http://eureka18882.com:18882/eureka/
      zone-nanshan: http://eureka18883.com:18883/eureka/
      zone-baoan: http://eureka18884.com:18884/eureka/
    fetch-registry: true
    register-with-eureka: true
    prefer-same-zone-eureka: true

重新访问浏览器,请求Consumer-01提供的端点,此时无论怎么刷新都会输出Server zone-yantian,说明请求一直打到了Server-01

2.5 配置详解

在服务注册和服务消费中有几个非常重要的配置项prefer-same-zone-eureka和eureka.instance.metadata-map.zone


prefer-same-zone-eureka

eureka:
  client:
    region: shenzhen
    availability-zones:
      shenzhen: zone-luohu,zone-yantian,zone-nanshan,zone-baoan
    service-url:
      zone-yantian: http://eureka18881.com:18881/eureka/
      zone-luohu: http://eureka18882.com:18882/eureka/
      zone-nanshan: http://eureka18883.com:18883/eureka/
      zone-baoan: http://eureka18884.com:18884/eureka/
  • true:获取eureka.client.availability-zones下的zone列表,选择第一个zone进行服务注册,如果第一个注册失败,依次选择其他zone进行服务注册
  • false:获取eureka.client.service-url列表,选择第一个service-url地址进行服务注册,如果第一个地址注册失败,依次选择其他service-url服务地址注册



eureka.instance.metadata-map.zone

eureka:
  instance:
    metadata-map:
      zone: zone-yantian

eureka.instance.metadata-map.zone配置项用于标识服务提供者和服务消费者属于那个zone,服务消费者通过ribbon在Eureka Server中拉去服务列表。如果一个zone中有多个服务,则轮询每个zone中的服务列表;如果zone中的服务均不能访问,则尝试访问其他zone下的服务。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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