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 服务,其资源清单如下所示:
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 下的服务。
评论