Eureka partition cluster deployment

Keywords: Spring Cloud eureka Cloud Native

1. Introduction

Eureka cluster provides partition function. The design concept of this function comes from the two basic concepts of region and zone created by Amazon cloud AWS:

  • Region: region refers to different regions in the physical sense. During service deployment, multiple computer rooms will be built and services will be deployed in areas with high user demand, which can reduce the problems caused by network instability
  • Zone: a region can be divided into multiple zones. In other words, multiple computer room servers in a region are divided into different available zones according to certain rules (for example, if there are three computer rooms in a region, the three computer rooms can be divided into three zones). By dividing zones, the effect of disaster recovery can be achieved. If one zone fails, other zones can still provide services.

According to the above two concepts, when Eureka Server is deployed in different regions, we can use the partition function provided by Eureka cluster. This can ensure that the services registered in a zone in a region are called prior to those registered in another zone. When the current one is unavailable, select the services registered in other zones to initiate the call. This can ensure that the delay of service call is reduced.

Relationship diagram of Region and Zone:

2. Cluster deployment

2.1 resource list

Four Eureka Server services are set up this time, and the resource list is as follows:

application-namezoneinstance-hostnameprot
Eureka-Ynatianzone-yantianeureka18881.com18881
Eureka-Luohuzone-luohueureka18882.com18882
Eureka-Nanshanzone-yantianeureka18883.com18883
Eureka-Baoanzone-baoaneureka18884.com18884

Eureka Server Cluster (Region Shenzhen, zone Luohu, zone Yantian, zone Nanshan, zone Bao'an):

The Eureka Server Cluster built here is a single Region, including four zones. If the company needs Eureka servers with high availability requirements, multiple Eureka Server servers can be deployed in each zone of the Region. If you need to make a sub Region, you can configure multiple regions by referring to Region Shenzhen.

2.2 configuration file

Eureka Server configuration file for Eureka ynati

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 Server profile for Eureka Luohu

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's Eureka Server profile

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 Server profile for Eureka Baoan

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 start the cluster

Start the four Eureka Server services in the cluster in turn. After the clusters stabilize, access the Eureka Server Dashboard. At this time, you can see the same service registration information Instance currently registered with Eureka and DS Replicaes information in any of the four Eureka servers (this is different, you don't need to replicate yourself...), This indicates that Eureka Server Cluster has started normally.

2.4 service registration

In addition to achieving high availability, Eureka Server partition is mainly to provide optimal services to clients, so as to achieve the fastest response of services. Register two services Server-01 and Server-02 in Eureka Server. Select zone Yantian for the metadata map zone in Server-01 and zone Louhu for the metadata map zone in Server-02. (note that the service name of both services is Server)

Server-01 profile

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

A Rest Api endpoint is provided in Server-01

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

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

}

Server-02 profile

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

A Rest Api endpoint is provided in Server-02

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

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

}

At this point, visit Eureka dashboard and you can see that two instances of Eureka Server are registered in Eureka Server Cluster.

2.4 service consumption

Create a service consumer Consumer-01 to test the specific situation of service call distribution.
Consumer-01 configuration file, where metadata-map.zone is specified as 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

Provide a Rest Api access endpoint:

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

    @Autowired
    private RestTemplate restTemplate;

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

}

When the browser accesses the endpoint, no matter how to refresh, it will output server zone Louhu, indicating that the request has been called to Server-02

Modify the configuration file of Consumer-01, and modify the metadata map: zone to 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

Re visit the browser and request the endpoint provided by Consumer-01. At this time, no matter how to refresh, the server zone Yantian will be output, indicating that the request has been called to Server-01

2.5 configuration details

There are several very important configuration items in service registration and service consumption: Prefer same zone Eureka and 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: get the list of zones under eureka.client.availability-zones, and select the first zone for service registration. If the first registration fails, select other zones in turn for service registration
  • false: obtain the eureka.client.service-url list and select the first service URL address for service registration. If the first address registration fails, select other service URL service addresses in turn for registration

eureka.instance.metadata-map.zone

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

The eureka.instance.metadata-map.zone configuration item is used to identify which zone the service provider and service consumer belong to. The service consumer pulls the service list from Eureka Server through the ribbon. If there are multiple services in a zone, poll the service list in each zone; If none of the services in the zone can be accessed, try to access the services in other zones.

Posted by AndieB on Sat, 30 Oct 2021 08:25:52 -0700