多节点高可用Eureka集群配置与部署
前言上一节讲的是动态扩容Eureka服务,说实话,一般情况这种操作并不多,一般多用在,由于大量服务节点部署后给Eureka造成压力突然积增,而解决的办法。这节讲的是一次启动或部署,直接就是集群多节点的,多用于服务节点相对稳定的场景。还有笔者这里有实际部署和应用的经验分享给大家,就是,我目前25个服务都注册在一个单节点Eureka上了(无论生产还是测试环境下),测试周期接近一年,我发现Eurek..
前言
上一节讲的是动态扩容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
大家可以四个节点都访问以下,展示的页面和这一个是一样的。
好了,到这里已经讲解完多节点配置和应用了,
下面如果把这两个工程打成jar在服务器上分别启动不同配置文件jar的时候
用java -Dspring.profiles.active=prod -jar xxx.jar 命令
注:大家可能看到我的副本均是不可用的,原因是我四个节点都是在本地,需要改hosts文件,
由于我在当前Demo没有做这些,所以后期我在本地补了一部分,这回所有节点都是可用的,如下图需要改几个地方。
对本文有异议或不明白的地方微信探讨,wx:15524579896
更多推荐
所有评论(0)