多节点高可用Eureka集群配置与部署

前言

上一节讲的是动态扩容Eureka服务,说实话,一般情况这种操作并不多,一般多用在,由于大量服务节点部署后给Eureka造成压力突然积增,而解决的办法。这节讲的是一次启动或部署,直接就是集群多节点的,多用于服务节点相对稳定的场景。还有笔者这里有实际部署和应用的经验分享给大家,就是,我目前25个服务都注册在一个单节点Eureka上了(无论生产还是测试环境下),测试周期接近一年,我发现Eureka可靠性和可用性还是很高的,没有出现一次注册中心挂了的情况。还有,我一下内容没有花里胡哨的套话和流程图,想必大家最想解决的就是如何快速的解决自己的问题,所以以下基本都是贴出的代码,关键地方我会文字说明。代码还是基于《重新定义》的思想。

正文

首先还是创建一个父module,pom配置如下:

cn.springcloud.book
	ch3-2
	0.0.1-SNAPSHOT
	pom

	ch3-2
	ch3-2

	
		ch3-2-zuul-gateway
		ch3-2-eureka-server
		ch3-2-eureka-client
	

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.3.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
		Finchley.RELEASE
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

然后创建EurekaServer服务,分别贴出pom文件和启动类

cn.springcloud.book
    ch3-2-eureka-server
    0.0.1-SNAPSHOT
    jar

    ch3-2-eureka-server
    ch3-2-eureka-server

    
        cn.springcloud.book
        ch3-2
        0.0.1-SNAPSHOT
        ../pom.xml 
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
@SpringBootApplication
@EnableEurekaServer
public class Ch322EurekaServerApplication {

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

 然后下面的内容是多节点配置的重要部分了

我会从上到下依次贴出这个目录的代码

多节点高可用Eureka集群配置与部署_第1张图片

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

多节点高可用Eureka集群配置与部署_第2张图片

多节点高可用Eureka集群配置与部署_第3张图片

这里笔者全部启动成功了。其实这时候你们访问http://localhost:8761/ 任意节点都是可以看到的。

但是我为了更好说明问题,这里加入一个eureka-client,并且这个客户端是双节点的,分别在zone1和zone2各一个

下面是client的pom和启动类

cn.springcloud.book
    ch3-2-eureka-client
    0.0.1-SNAPSHOT
    jar

    ch3-2-eureka-client
    ch3-2-eureka-client

    
        cn.springcloud.book
        ch3-2
        0.0.1-SNAPSHOT
        ../pom.xml 
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
@SpringBootApplication
@EnableDiscoveryClient
public class Ch32EurekaClientApplication {

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

下面我同样分别列出client的配置文件

多节点高可用Eureka集群配置与部署_第4张图片

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

多节点高可用Eureka集群配置与部署_第5张图片

访问http://localhost:8761/

多节点高可用Eureka集群配置与部署_第6张图片

大家可以四个节点都访问以下,展示的页面和这一个是一样的。

好了,到这里已经讲解完多节点配置和应用了,

下面如果把这两个工程打成jar在服务器上分别启动不同配置文件jar的时候 

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

注:大家可能看到我的副本均是不可用的,原因是我四个节点都是在本地,需要改hosts文件,

由于我在当前Demo没有做这些,所以后期我在本地补了一部分,这回所有节点都是可用的,如下图需要改几个地方。

多节点高可用Eureka集群配置与部署_第7张图片

多节点高可用Eureka集群配置与部署_第8张图片

多节点高可用Eureka集群配置与部署_第9张图片

对本文有异议或不明白的地方微信探讨,wx:15524579896

你可能感兴趣的:(多节点高可用Eureka集群配置与部署)