Eureka分区集群部署

Posted 李子捌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Eureka分区集群部署相关的知识,希望对你有一定的参考价值。

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-namezoneinstance-hostnameprot
Eureka-Ynatianzone-yantianeureka18881.com18881
Eureka-Luohuzone-luohueureka18882.com18882
Eureka-Nanshanzone-yantianeureka18883.com18883
Eureka-Baoanzone-baoaneureka18884.com18884

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下的服务。

以上是关于Eureka分区集群部署的主要内容,如果未能解决你的问题,请参考以下文章

Eureka 单机部署与集群部署

k8s部署eureka集群

部署Eureka集群实现高可用服务

k8s集群部署eureka总结

SpringCloud组件:Eureka高可用集群部署

Spring Cloud Netflix-Eureka集群数据同步