文章目录
  1. 1. 7天学会使用springcloud(六)
    1. 1.1. eureka集群配置
    2. 1.2. 分地域分区
    3. 1.3. 分区服务架构图

7天学会使用springcloud(六)

eureka集群配置

eureka的集群配置需要几个必要条件

支持注册中心集群

application.yml available-replicas条件为:

  1. eureka.instance.appname 必须等于 spring.application.name 并且不可缺省,所以直接占位符 appname: ${spring.application.name}
  2. prefer-ip-address: 必须为false 或者缺省
  3. fetch-registry 必须为false 或者缺省
  4. register-with-eureka: 必须为true
  5. instance-id: 必须为当前可访问的服务地址,否则eureka就会被视作down的状态

配置文件满足以上几个条件了,那么就可以eureka集群了:

paste image

看到general info下的environment的值是product,这个值如果在配置文件里配置:

eureka:
      environment: product
    

我的服务上就会看到如下:

paste image

分地域分区

eureka提供了region和zone两个概念来进行分区,这两个概念均来自于亚马逊的AWS:

  • region:可以简单理解为地理上的分区,比如亚洲地区,或者华北地区,再或者北京等等,没有具体大小的限制。根据项目具体的情况,可以自行合理划分region。
  • zone:可以简单理解为region内的具体机房,比如说region划分为北京,然后北京有两个机房,就可以在此region之下划分出zone1,zone2两个zone。

分区服务架构图

paste image

如图所示,有一个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/
      
      
  1. Service-1

    @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




** ps 参考博客: **

https://segmentfault.com/a/1190000014107639

https://www.jianshu.com/p/2ca32773b3e5

文章目录
  1. 1. 7天学会使用springcloud(六)
    1. 1.1. eureka集群配置
    2. 1.2. 分地域分区
    3. 1.3. 分区服务架构图