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

港控/mmm° 2022-05-13 15:24 576阅读 0赞

前言

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

正文

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

  1. <groupId>cn.springcloud.book</groupId>
  2. <artifactId>ch3-2</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>pom</packaging>
  5. <name>ch3-2</name>
  6. <description>ch3-2</description>
  7. <modules>
  8. <module>ch3-2-zuul-gateway</module>
  9. <module>ch3-2-eureka-server</module>
  10. <module>ch3-2-eureka-client</module>
  11. </modules>
  12. <parent>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-parent</artifactId>
  15. <version>2.0.3.RELEASE</version>
  16. <relativePath/> <!-- lookup parent from repository -->
  17. </parent>
  18. <properties>
  19. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  20. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  21. <java.version>1.8</java.version>
  22. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  23. </properties>
  24. <dependencyManagement>
  25. <dependencies>
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-dependencies</artifactId>
  29. <version>${spring-cloud.version}</version>
  30. <type>pom</type>
  31. <scope>import</scope>
  32. </dependency>
  33. </dependencies>
  34. </dependencyManagement>
  35. <dependencies>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-actuator</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-web</artifactId>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-test</artifactId>
  47. <scope>test</scope>
  48. </dependency>
  49. </dependencies>

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

  1. <groupId>cn.springcloud.book</groupId>
  2. <artifactId>ch3-2-eureka-server</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>jar</packaging>
  5. <name>ch3-2-eureka-server</name>
  6. <description>ch3-2-eureka-server</description>
  7. <parent>
  8. <groupId>cn.springcloud.book</groupId>
  9. <artifactId>ch3-2</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
  12. </parent>
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  22. </dependency>
  23. </dependencies>
  24. <build>
  25. <plugins>
  26. <plugin>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-maven-plugin</artifactId>
  29. </plugin>
  30. </plugins>
  31. </build>
  32. @SpringBootApplication
  33. @EnableEurekaServer
  34. public class Ch322EurekaServerApplication {
  35. public static void main(String[] args) {
  36. SpringApplication.run(Ch322EurekaServerApplication.class, args);
  37. }
  38. }

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

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

70

  1. eureka:
  2. server:
  3. use-read-only-response-cache: false
  4. response-cache-auto-expiration-in-seconds: 10
  5. management:
  6. endpoints:
  7. web:
  8. exposure:
  9. include: '*'
  10. server:
  11. port: 8761
  12. spring:
  13. application:
  14. name: eureka-server
  15. eureka:
  16. instance:
  17. hostname: localhost
  18. preferIpAddress: true
  19. metadataMap.zone: zone1
  20. client:
  21. register-with-eureka: true
  22. fetch-registry: true
  23. region: region-east
  24. service-url:
  25. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  26. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  27. availability-zones:
  28. region-east: zone1,zone2
  29. server:
  30. waitTimeInMsWhenSyncEmpty: 0
  31. enableSelfPreservation: false
  32. server:
  33. port: 8762
  34. spring:
  35. application:
  36. name: eureka-server
  37. eureka:
  38. instance:
  39. hostname: localhost
  40. preferIpAddress: true
  41. metadataMap.zone: zone1
  42. client:
  43. register-with-eureka: true
  44. fetch-registry: true
  45. region: region-east
  46. service-url:
  47. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  48. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  49. availability-zones:
  50. region-east: zone1,zone2
  51. server:
  52. waitTimeInMsWhenSyncEmpty: 0
  53. enableSelfPreservation: false
  54. server:
  55. port: 8763
  56. spring:
  57. application:
  58. name: eureka-server
  59. eureka:
  60. instance:
  61. hostname: localhost
  62. preferIpAddress: true
  63. metadataMap.zone: zone2
  64. client:
  65. register-with-eureka: true
  66. fetch-registry: true
  67. region: region-east
  68. service-url:
  69. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  70. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  71. availability-zones:
  72. region-east: zone1,zone2
  73. server:
  74. waitTimeInMsWhenSyncEmpty: 0
  75. enableSelfPreservation: false
  76. server:
  77. port: 8764
  78. spring:
  79. application:
  80. name: eureka-server
  81. eureka:
  82. instance:
  83. hostname: localhost
  84. preferIpAddress: true
  85. metadataMap.zone: zone2
  86. client:
  87. register-with-eureka: true
  88. fetch-registry: true
  89. region: region-east
  90. service-url:
  91. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  92. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  93. availability-zones:
  94. region-east: zone1,zone2
  95. server:
  96. waitTimeInMsWhenSyncEmpty: 0
  97. enableSelfPreservation: false

这里值得解释的是

上面我配置的是四个节点,分别在一个区域里,用region: region-east 配置,然后这个区域有两个空间分别是zone1和zone2

  1. availability-zones:
  2. region-east: zone1,zone2
  3. 这个配置代表了

然后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

70 1

70 2

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

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

下面是client的pom和启动类

  1. <groupId>cn.springcloud.book</groupId>
  2. <artifactId>ch3-2-eureka-client</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>jar</packaging>
  5. <name>ch3-2-eureka-client</name>
  6. <description>ch3-2-eureka-client</description>
  7. <parent>
  8. <groupId>cn.springcloud.book</groupId>
  9. <artifactId>ch3-2</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
  12. </parent>
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  22. </dependency>
  23. </dependencies>
  24. <build>
  25. <plugins>
  26. <plugin>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-maven-plugin</artifactId>
  29. </plugin>
  30. </plugins>
  31. </build>
  32. @SpringBootApplication
  33. @EnableDiscoveryClient
  34. public class Ch32EurekaClientApplication {
  35. public static void main(String[] args) {
  36. SpringApplication.run(Ch32EurekaClientApplication.class, args);
  37. }
  38. }

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

70 3

  1. management:
  2. endpoints:
  3. web:
  4. exposure:
  5. include: '*'
  6. server:
  7. port: 8081
  8. spring:
  9. application:
  10. name: client
  11. eureka:
  12. instance:
  13. metadataMap.zone: zone1
  14. client:
  15. register-with-eureka: true
  16. fetch-registry: true
  17. region: region-east
  18. service-url:
  19. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  20. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  21. availability-zones:
  22. region-east: zone1,zone2
  23. server:
  24. port: 8082
  25. spring:
  26. application:
  27. name: client
  28. eureka:
  29. instance:
  30. metadataMap.zone: zone2
  31. client:
  32. register-with-eureka: true
  33. fetch-registry: true
  34. region: region-east
  35. service-url:
  36. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  37. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  38. availability-zones:
  39. region-east: zone1,zone2

下面分别用命令启动两个节点,操作也是和eurekaServer是一样的,进入到client目录

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

70 4

访问http://localhost:8761/

70 5

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

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

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

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

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

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

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODAwMzM4OQ_size_16_color_FFFFFF_t_70

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODAwMzM4OQ_size_16_color_FFFFFF_t_70 1

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODAwMzM4OQ_size_16_color_FFFFFF_t_70 2

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

发表评论

表情:
评论列表 (有 0 条评论,576人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Redis

    本文使用的是redis5.0.7版本 一、主从复制 > 单个Redis如果因为某种原因宕机的话,可能会导致Redis服务不可用,可以使用主从复制实现一主多从,主节点负责