1. 程式人生 > >多節點高可用Eureka叢集配置與部署

多節點高可用Eureka叢集配置與部署

前言

上一節講的是動態擴容Eureka服務,說實話,一般情況這種操作並不多,一般多用在,由於大量服務節點部署後給Eureka造成壓力突然積增,而解決的辦法。這節講的是一次啟動或部署,直接就是叢集多節點的,多用於服務節點相對穩定的場景。還有筆者這裡有實際部署和應用的經驗分享給大家,就是,我目前25個服務都註冊在一個單節點Eureka上了(無論生產還是測試環境下),測試周期接近一年,我發現Eureka可靠性和可用性還是很高的,沒有出現一次註冊中心掛了的情況。還有,我一下內容沒有花裡胡哨的套話和流程圖,想必大家最想解決的就是如何快速的解決自己的問題,所以以下基本都是貼出的程式碼,關鍵地方我會文字說明。程式碼還是基於《重新定義》的思想。

正文

首先還是建立一個父module,pom配置如下:

<groupId>cn.springcloud.book</groupId>
	<artifactId>ch3-2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>ch3-2</name>
	<description>ch3-2</description>

	<modules>
		<module>ch3-2-zuul-gateway</module>
		<module>ch3-2-eureka-server</module>
		<module>ch3-2-eureka-client</module>
	</modules>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
	</properties>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

然後建立EurekaServer服務,分別貼出pom檔案和啟動類

<groupId>cn.springcloud.book</groupId>
    <artifactId>ch3-2-eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ch3-2-eureka-server</name>
    <description>ch3-2-eureka-server</description>

    <parent>
        <groupId>cn.springcloud.book</groupId>
        <artifactId>ch3-2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
@SpringBootApplication
@EnableEurekaServer
public class Ch322EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(Ch322EurekaServerApplication.class, args);
    }
}

 然後下面的內容是多節點配置的重要部分了

我會從上到下依次貼出這個目錄的程式碼

eureka:
  server:
    use-read-only-response-cache: false
    response-cache-auto-expiration-in-seconds: 10
management:
  endpoints:
    web:
      exposure:
        include: '*'

 

server:
  port: 8761
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
    preferIpAddress: true
    metadataMap.zone: zone1
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
      zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
    availability-zones:
      region-east: zone1,zone2
  server:
      waitTimeInMsWhenSyncEmpty: 0
      enableSelfPreservation: false
server:
  port: 8762
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
    preferIpAddress: true
    metadataMap.zone: zone1
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
      zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
    availability-zones:
      region-east: zone1,zone2
  server:
      waitTimeInMsWhenSyncEmpty: 0
      enableSelfPreservation: false
server:
  port: 8763
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
    preferIpAddress: true
    metadataMap.zone: zone2
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
      zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
    availability-zones:
      region-east: zone1,zone2
  server:
      waitTimeInMsWhenSyncEmpty: 0
      enableSelfPreservation: false

 

server:
  port: 8764
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
    preferIpAddress: true
    metadataMap.zone: zone2
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
      zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
    availability-zones:
      region-east: zone1,zone2
  server:
      waitTimeInMsWhenSyncEmpty: 0
      enableSelfPreservation: false

 這裡值得解釋的是

上面我配置的是四個節點,分別在一個區域裡,用region: region-east 配置,然後這個區域有兩個空間分別是zone1和zone2

availability-zones:
  region-east: zone1,zone2
 這個配置代表了

然後8761和8762在zone1空間裡,8763和8764在zone2空間裡,請大家仔細看我上面的配置,按照配置理解用意。

還有必要解釋的是WaitTimeInMsWhenSyncEmpty(*):

這個設定為0是為了儘快讓eureka server啟動起來就提供rest api服務。預設是5分鐘,也就是如果peer節點註冊列表為空,那就等5分鐘讓其他服務有註冊上來了,然後同步過來,再對外提供rest api服務。

UseReadOnlyResponseCache(*):

目前採用的是二級快取策略,一個是讀寫快取記憶體過期策略,另一個沒有過期只有只讀快取,預設為true,表示只讀快取。

然後進入eureka-server目錄下分別執行

mvn spring-boot:run -Dspring.profiles.active=zone1a

mvn spring-boot:run -Dspring.profiles.active=zone1b

mvn spring-boot:run -Dspring.profiles.active=zone2a

mvn spring-boot:run -Dspring.profiles.active=zone1b

切記每次執行下一條命令需要再開啟一個Terminal

這裡筆者全部啟動成功了。其實這時候你們訪問http://localhost:8761/ 任意節點都是可以看到的。

但是我為了更好說明問題,這裡加入一個eureka-client,並且這個客戶端是雙節點的,分別在zone1和zone2各一個

下面是client的pom和啟動類

<groupId>cn.springcloud.book</groupId>
    <artifactId>ch3-2-eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ch3-2-eureka-client</name>
    <description>ch3-2-eureka-client</description>

    <parent>
        <groupId>cn.springcloud.book</groupId>
        <artifactId>ch3-2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
@SpringBootApplication
@EnableDiscoveryClient
public class Ch32EurekaClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(Ch32EurekaClientApplication.class, args);
	}
}

下面我同樣分別列出client的配置檔案

management:
  endpoints:
    web:
      exposure:
        include: '*'
server:
  port: 8081
spring:
  application:
    name: client
eureka:
  instance:
    metadataMap.zone: zone1
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
      zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
    availability-zones:
      region-east: zone1,zone2
server:
  port: 8082
spring:
  application:
    name: client
eureka:
  instance:
    metadataMap.zone: zone2
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
      zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
    availability-zones:
      region-east: zone1,zone2

 下面分別用命令啟動兩個節點,操作也是和eurekaServer是一樣的,進入到client目錄

分別使用mvn spring-boot:run -Dspring.profiles.active=zone1 和mvn spring-boot:run -Dspring.profiles.active=zone2

訪問http://localhost:8761/

大家可以四個節點都訪問以下,展示的頁面和這一個是一樣的。

好了,到這裡已經講解完多節點配置和應用了,

下面如果把這兩個工程打成jar在伺服器上分別啟動不同配置檔案jar的時候 

用java -Dspring.profiles.active=prod -jar xxx.jar 命令

注:對本文有異議或不明白的地方微信探討,wx:15524579896