在 Spring Cloud 应用篇 之 Eureka 初体验 一文中,已经介绍了 Eureka 的基本使用了,但是之前 Eureka 只使用了单节点部署,生产上,不可能只有一个节点,所以我们要实现 Eureka 的高可用,本例就在此前代码的基础上进行修改,只部署两个节点。下面进行修改:

(一)修改配置文件如下,让两个 Eureka 互相向对方注册

 
  1. spring:

  2. profiles:

  3. active: instance1

  4. application:

  5. name: eureka-service

  6.  
  7. eureka:

  8. client:

  9. #由于该应用为注册中心,设置为false,表明不向注册中心注册自己

  10. register-with-eureka: false

  11. server:

  12. enable-self-preservation: false

  13. #是否从eureka服务器获取注册信息,这里不需要

  14. fetch-registry: false

  15. logging:

  16. level:

  17. com:

  18. netflix:

  19. eureka: OFF

  20. discovery: OFF

  21.  
  22. management:

  23. endpoints:

  24. web:

  25. exposure:

  26. include: '*'

  27. endpoint:

  28. health:

  29. show-details: ALWAYS

  30.  
  31. ---

  32.  
  33. spring:

  34. profiles: instance1

  35.  
  36. server:

  37. port: 8761

  38.  
  39. eureka:

  40. client:

  41. service-url:

  42. defaultZone: http://localhost:8762/eureka/

  43.  
  44. ---

  45. spring:

  46. profiles: instance2

  47.  
  48. server:

  49. port: 8762

  50.  
  51. eureka:

  52. client:

  53. service-url:

  54. defaultZone: http://localhost:8761/eureka/

每次启动一个 Eureka 的实例,需改 spring.profiles.active 的值,还有要去掉 Single instance only 的勾选

Spring Cloud 应用篇 之 Eureka 高可用

(二)修改 spring.profiles.active 的值为 instance1,启动 Eureka

Spring Cloud 应用篇 之 Eureka 高可用

修改 spring.profiles.active 的值为 instance2,启动 Eureka

Spring Cloud 应用篇 之 Eureka 高可用

(三)启动之前写的一个服务 spring-demo-service,它的配置文件如下:

 
  1. server:

  2. port: 8281

  3.  
  4. eureka:

  5. client:

  6. serviceUrl:

  7. defaultZone: http://localhost:8761/eureka/

  8. spring:

  9. application:

  10. name: spring-demo-service

  11.  
  12. management:

  13. endpoints:

  14. web:

  15. exposure:

  16. include: '*'

  17. endpoint:

  18. health:

  19. show-details: ALWAYS

可以看到,此服务只注册到了 localhost:8761 的 Eureka

(四)访问 http://localhost:8761/,如下,spring-demo-service 已经注册进来

Spring Cloud 应用篇 之 Eureka 高可用

访问 http://localhost:8762/,如下,发现 spring-demo-service 还注册到了 localhost:8762 

Spring Cloud 应用篇 之 Eureka 高可用

从 spring-demo-service 的配置文件我们可以看到,它没有向 localhost:8762 注册,但是此时确实注册到了,主要是因为,两个注册中心 Eureka 相互注册了,所有注册到其中一个 Eureka 的服务实例信息会同步到另一个中。

(五)下面我们停止 localhost:8761 的 Eureka,再次访问 http://localhost:8761/,已经发现不能访问了,然后访问 http://localhost:8762/,发现注册列表中仍然有 spring-demo-service 的服务实例,这样,当其中一个节点的 Eureka 出现异常不可用时,还可以访问其他节点的 Eureka。

下面我们再看另一种情况,重启 spring-demo-service 服务,访问 http://localhost:8762/

Spring Cloud 应用篇 之 Eureka 高可用

 

这个时候,spring-demo-service 的服务在注册中心就没有了,那这种情况肯定是不行的啊,我们想要的结果是,其中一台注册中心挂掉后,服务还可以注册到其它的注册中心去,那要怎么解决这个问题呢?我们只能让每个服务都注册到每个注册中心去,下面我们修改 spring-demo-service 的配置文件如下:

 
  1. server:

  2. port: 8281

  3.  
  4. eureka:

  5. client:

  6. serviceUrl:

  7.       # 向每个注册中心注册

  8. defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

  9. spring:

  10. application:

  11. name: spring-demo-service

  12.  
  13. management:

  14. endpoints:

  15. web:

  16. exposure:

  17. include: '*'

  18. endpoint:

  19. health:

  20. show-details: ALWAYS

重启 spring-demo-service 服务,这个时候再访问 http://localhost:8762/,就会发现 spring-demo-service 又注册进来了,图就不贴了。至此,我们就实现了 Eureka 的高可用。生产上可能会部署不只两个节点的 Eureka,可能会有三个或更多,只要让它们两两互相注册即可。

相关文章:

  • 2021-11-11
  • 2021-09-09
  • 2021-05-05
  • 2021-05-18
  • 2022-12-23
  • 2021-04-10
  • 2021-05-02
  • 2021-08-03
猜你喜欢
  • 2021-11-27
  • 2022-12-23
  • 2021-06-22
  • 2021-08-06
  • 2021-10-05
  • 2022-01-13
  • 2021-05-10
相关资源
相似解决方案