SpringCloud的Region和Zone的区别

1、Region和Zone是一对多的关系。
2、服务注册和发现都会按先指定的Region,然后根据指定的Region寻找第一个Zone。
3、Zone相当于组成Region的基本单元。

样例:

分区服务的部署架构图

SpringCloud的Region和Zone的区别
介绍:如上图所示,一个region:beijing,下面有zone-1和zone-2两个分区,每个分区内有一个注册中心Eureka Server和一个服务提供者Service。我们在zone-1内创建一个
Consumer-1服务消费者的话,其会优先调用同一个zone内的Service-1,当Service-1不可用时,才会去调用zone-2内的Service-2。

配置文件:
(1)Eureka Server-1:
spring:

  application:

    name: Server-1

server:

  port: 30000

eureka:

  instance:

    prefer-ip-address: true

    status-page-url-path: /actuator/info

    health-check-url-path: /actuator/health

    hostname: localhost

  client:

    register-with-eureka: true

    fetch-registry: true

    prefer-same-zone-eureka: true
 
    #地区

    region: beijing

    availability-zones:

      beijing: zone-1,zone-2

    service-url:

      zone-1: http://localhost:30000/eureka/

      zone-2: http://localhost:30001/eureka/
(2)Eureka Server-2:
spring:

  application:

    name: Server-2

server:

  port: 30001

eureka:

  instance:

    prefer-ip-address: true

    status-page-url-path: /actuator/info

    health-check-url-path: /actuator/health

    hostname: localhost

  client:

    register-with-eureka: true

    fetch-registry: true

    prefer-same-zone-eureka: true
 
    #地区

    region: beijing

    availability-zones:

      beijing: zone-2,zone-1

    service-url:

      zone-1: http://localhost:30000/eureka/

      zone-2: http://localhost:30001/eureka/
(3)Service1
测试代码
@RestController

public class HiController {
 
    @Value("${zone.name}")
 
    private String zoneName;
 

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
 
    public String hi() {
 
        return zoneName;
 
    }
}
}
配置文件
spring:

  application:

    name: service

server:

  port: 30010

eureka:

  instance:

    prefer-ip-address: true

    status-page-url-path: /actuator/info

    health-check-url-path: /actuator/health

    metadata-map:

      zone: zone-1

  client:

    register-with-eureka: true

    fetch-registry: true

    prefer-same-zone-eureka: true
 
    #地区

    region: beijing

    availability-zones:

      beijing: zone-1,zone-2

    service-url:

      zone-1: http://localhost:30000/eureka/

      zone-2: http://localhost:30001/eureka/

zone.name: zone-1
(4)Service2
spring:

  application:

    name: service

server:

  port: 30011

eureka:

  instance:

    prefer-ip-address: true

    status-page-url-path: /actuator/info

    health-check-url-path: /actuator/health

    metadata-map:

      zone: zone-2

  client:

    register-with-eureka: true

    fetch-registry: true

    prefer-same-zone-eureka: true
 
    #地区

    region: beijing

    availability-zones:

      beijing: zone-2,zone-1

    service-url:

      zone-1: http://localhost:30000/eureka/

      zone-2: http://localhost:30001/eureka/

zone.name: zone-2
(5)Consumer-1
测试代码
@RestController

public class HiController {
 
    @Autowired
 
    private RestTemplate restTemplate;

    @RequestMapping(value="/consumer")
 
    public String hi() {
 
        return restTemplate.getForObject("http://service/hi", String.class);
 
    }
}
}
配置文件
spring:

  application:

    name: consumer

server:

  port: 30030

eureka:

  instance:

    prefer-ip-address: true

    status-page-url-path: /actuator/info

    health-check-url-path: /actuator/health

    metadata-map:

      zone: zone-1

  client:

    register-with-eureka: true

    fetch-registry: true

    prefer-same-zone-eureka: true
 
    #地区

    region: beijing

    availability-zones:

      beijing: zone-1,zone-2

    service-url:

      zone-1: http://localhost:30000/eureka/

      zone-2: http://localhost:30001/eureka/


总结
1、如果eureka.client.prefer-same-zone-eureka为false,则不按照优先加载同一个zone下的服务,按照eureka.client.service-url下的数据结构为map的第一个EntrySet声明的注册中心来注册。如果为true,会先通过指定的Region取availablility-zones的第一个Zone1,然后在service-url下的Zone1取第一个注册中心地址注册。只在第一个注册失败的情况下,才会依次向其它的注册中心注册,总共重试3次,如果3个service-url都没有注册成
功,则注册失败。每隔一个心跳时间,会再次尝试。
2、通过eureka.client.region指定服务消费者和服务供着属于哪个Region
3、服务消费者和服务提供者分别属于哪个Zone,均是通过eureka.instance.metadata-map.zone来判定的