SpringCloud之Eureka高可用集群环境搭建

柔情只为你懂 2022-01-28 14:27 328阅读 0赞

SpringCloud之Eureka高可用集群环境搭建

注册中心集群

在微服务中,注册中心非常核心,可以实现服务治理,如果一旦注册出现故障的时候,可能会导致整个微服务无法访问,在这时候就需要对注册中心实现高可用集群模式。

Eureka集群相当简单:相互注册

Eureka高可用实际上将自己作为服务向其他服务注册中心注册自己,这样就可以形成一组相互注册的服务注册中心,从而实现服务清单的互相同步,达到高可用效果。

集群的服务名称要统一,要相同!

启动时候 报错 正常! 启动时候互相注册 不会同时启动成功的

启动类都是一样的

  1. package com.toov5;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @EnableEurekaServer //开启注册中心
  6. @SpringBootApplication
  7. public class AppEureka {
  8. public static void main(String[] args) {
  9. SpringApplication.run(AppEureka.class, args);
  10. }
  11. }

pom都一样的:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.toov5</groupId>
  4. <artifactId>EurekaClaster</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.0.1.RELEASE</version>
  10. </parent>
  11. <!-- 管理依赖 -->
  12. <dependencyManagement>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-dependencies</artifactId>
  17. <version>Finchley.M7</version>
  18. <type>pom</type>
  19. <scope>import</scope>
  20. </dependency>
  21. </dependencies>
  22. </dependencyManagement>
  23. <dependencies>
  24. <!--SpringCloud eureka-server -->
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  28. </dependency>
  29. </dependencies>
  30. <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
  31. <repositories>
  32. <repository>
  33. <id>spring-milestones</id>
  34. <name>Spring Milestones</name>
  35. <url>https://repo.spring.io/libs-milestone</url>
  36. <snapshots>
  37. <enabled>false</enabled>
  38. </snapshots>
  39. </repository>
  40. </repositories>
  41. </project>

  

看不同的yml:

  1. server:
  2. port: 9100
  3. spring:
  4. application:
  5. name: app-toov5-member
  6. #eureka:
  7. # client:
  8. # service-url:
  9. # defaultZone: http://localhost:8100/eureka
  10. ###集群地址 或者不写死 端口号改变 端口号改为对方的eureka端口号
  11. eureka:
  12. client:
  13. service-url:
  14. ## defaultZone: http://${eureka.instance.hostname}:8100/eureka/ ##其他eureka服务的地址
  15. defaultZone: http://127.0.0.1:8100/eureka/ ##其他eureka服务的地址
  16. register-with-eureka: true
  17. fetch-registry: true
  18. name: app-toov5-member
  19. #eureka:
  20. # client:
  21. # service-url:
  22. # defaultZone: http://localhost:8100/eureka
  23. ###集群地址 或者不写死 端口号改变 端口号改为对方的eureka端口号
  24. eureka:
  25. client:
  26. service-url:
  27. ## defaultZone: http://${eureka.instance.hostname}:8100/eureka/ ##其他eureka服务的地址
  28. defaultZone: http://127.0.0.1:8100/eureka/ ##其他eureka服务的地址
  29. register-with-eureka: true
  30. fetch-registry: true

  

  1. ###eureka 服务端口号
  2. server:
  3. port: 8100
  4. ###服务注册名称
  5. spring:
  6. application:
  7. name: app-toov5-member ##############要相同
  8. eureka:
  9. instance:
  10. ##注册中心ip地址
  11. hostname: 127.0.0.1
  12. ###客户端调用地址
  13. client:
  14. serviceUrl:
  15. defaultZone: http://${eureka.instance.hostname}:9100/eureka/ ##其他eureka服务的地址 互相注册 写对方的地址+端口
  16. ###因为该应用为注册中心,不会注册自己 (集群设为true)
  17. register-with-eureka: true
  18. ###因为自己为注册中心 ,不会去在该应用中的检测服务
  19. fetch-registry: true
  20. name: app-toov5-member ##############要相同
  21. eureka:
  22. instance:
  23. ##注册中心ip地址
  24. hostname: 127.0.0.1
  25. ###客户端调用地址
  26. client:
  27. serviceUrl:
  28. defaultZone: http://${eureka.instance.hostname}:9100/eureka/ ##其他eureka服务的地址 互相注册 写对方的地址+端口
  29. ###因为该应用为注册中心,不会注册自己 (集群设为true)
  30. register-with-eureka: true
  31. ###因为自己为注册中心 ,不会去在该应用中的检测服务
  32. fetch-registry: true

  

1179709-20181112231216516-1885275852.png

1179709-20181112231251998-561540761.png

发表评论

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

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

相关阅读