当前位置:   article > 正文

SpringCloud 之 Eureka 配置,Eureka 集群,Eureka 监听_peer-eureka-nodes-update-interval-ms

peer-eureka-nodes-update-interval-ms

Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server,并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。

一、先来搭建一个 Eureka Server 作为注册中心

1.引入依赖

  1. <!--添加eureka服务端-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  5. </dependency>

2.写配置

作为注册中心,端口号是 8001,服务名用的 service-eureka

  1. #配置端口号
  2. server:
  3. port: 8001
  4. #服务名
  5. spring:
  6. application:
  7. name: service-eureka

这个 eureka 配置目的就是把服务添加到注册中心去
注册的地址是:http://localhost:8001/eureka/

  1. #eureka配置
  2. eureka:
  3. client:
  4. # 不把服务注册到注册中心
  5. # register-with-eureka: false
  6. fetch-registry: false
  7. service-url:
  8. defaultZone: http://localhost:8001/eureka/
  9. server:
  10. #主动失效时间
  11. eviction-interval-timer-in-ms: 30000
  12. registry-sync-retry-wait-ms: 500
  13. a-s-g-cache-expiry-timeout-ms: 60000
  14. peer-eureka-nodes-update-interval-ms: 15000
  15. renewal-threshold-update-interval-ms: 300000
  16. #测试环境关闭自我保护模式
  17. # enable-self-preservation: false

3.启动器添加注解

添加 @EnableEurekaServer 启动注册中心
由于作为注册中心没有数据库这些玩意,所以启动报错了 @SpringBootApplication 后面添加
exclude= {DataSourceAutoConfiguration.class} 

  1. @EnableEurekaServer
  2. @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
  3. public class EurekaApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaApplication.class, args);
  6. }
  7. }

4.启动并访问注册中心

访问路径:localhost:8001


这里可以看到 service-eureka 作为一个服务注册到注册中心里了

二、写一个服务作为 Eureka 的客户端注册到注册中心

1.引入依赖

  1. <!--添加eureka客户端-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>

2.写配置

微服务的端口号是 8084,服务名用的 service-admin

  1. #配置端口号
  2. server:
  3. port: 8084
  4. #服务名
  5. spring:
  6. application:
  7. name: service-admin

依然把服务添加到注册中心去
注册的地址依然是:http://localhost:8001/eureka/

  1. #eureka配置
  2. eureka:
  3. client:
  4. service-url:
  5. defaultZone: http://localhost:8001/eureka/
  6. #间隔多少秒去服务端拉去注册信息
  7. registry-fetch-interval-seconds: 10
  8. instance:
  9. #发送心跳给server端频率
  10. lease-renewal-interval-in-seconds: 30
  11. #健康检查地址
  12. health-check-url-path: /actuator/health
  13. prefer-ip-address: true

3.启动器添加注解

@EnableEurekaClient 注解是基于spring-cloud-netflix依赖,只能eureka使用
@EnableDiscoveryClient 注解是基于spring-cloud-commons依赖,并且在classpath中实现
它们的作用是一样的,所以我选择 @EnableDiscoveryClient
添加 @EnableDiscoveryClient 将服务作为客户端注册到注册中心

  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. public class AdminApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(AdminApplication.class, args);
  6. }
  7. }

4.启动并访问注册中心

访问路径:localhost:8001

可以看到 admin 服务也成功注册到注册中心,说明配置成功

三、Eureka 集群

集群这种东西对于没接触过的人来说是个高大上的东西,但是对于接触过的人来说也就那样
Eureka 集群最主要的目的还是为了保证 Eureka 的稳定性,如果有个挂了,还有其他的Eureka 能担负起责任来

对配置文件稍微改动,变成 3 个 Eureka 的配置
可以看到通过 “---” 符号把配置文件分为 3 个配置,分别对应为 eureka1,eureka2,eureka3 以及各自的端口号
接下来我们可以通过 --spring.profiles.active=xx 去启动对用的配置

  1. #eureka1 配置
  2. server:
  3. port: 8001
  4. spring:
  5. profiles: eureka1
  6. application:
  7. #配置服务名
  8. name: eureka1
  9. #eureka配置
  10. eureka:
  11. client:
  12. # 是否将自己注册到eureka server
  13. register-with-eureka: true
  14. # 避免eureka查找服务列表
  15. fetch-registry: false
  16. service-url:
  17. defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/,http://localhost:8003/eureka/
  18. server:
  19. #主动失效时间
  20. eviction-interval-timer-in-ms: 30000
  21. registry-sync-retry-wait-ms: 500
  22. a-s-g-cache-expiry-timeout-ms: 60000
  23. peer-eureka-nodes-update-interval-ms: 15000
  24. renewal-threshold-update-interval-ms: 300000
  25. # 关闭保护机制,尽可能提出挂掉的服务节点
  26. enable-self-preservation: false
  27. ---
  28. #eureka2 配置
  29. server:
  30. port: 8002
  31. spring:
  32. profiles: eureka2
  33. application:
  34. #配置服务名
  35. name: eureka2
  36. #eureka配置
  37. eureka:
  38. client:
  39. # 是否将自己注册到eureka server
  40. register-with-eureka: true
  41. # 避免eureka查找服务列表
  42. fetch-registry: false
  43. service-url:
  44. defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/,http://localhost:8003/eureka/
  45. server:
  46. #主动失效时间
  47. eviction-interval-timer-in-ms: 30000
  48. registry-sync-retry-wait-ms: 500
  49. a-s-g-cache-expiry-timeout-ms: 60000
  50. peer-eureka-nodes-update-interval-ms: 15000
  51. renewal-threshold-update-interval-ms: 300000
  52. # 关闭保护机制,尽可能提出挂掉的服务节点
  53. enable-self-preservation: false
  54. ---
  55. #eureka3 配置
  56. server:
  57. port: 8003
  58. spring:
  59. profiles: eureka3
  60. application:
  61. #配置服务名
  62. name: eureka3
  63. #eureka配置
  64. eureka:
  65. client:
  66. # 是否将自己注册到eureka server
  67. register-with-eureka: true
  68. # 避免eureka查找服务列表
  69. fetch-registry: false
  70. service-url:
  71. defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/,http://localhost:8003/eureka/
  72. server:
  73. #主动失效时间
  74. eviction-interval-timer-in-ms: 30000
  75. registry-sync-retry-wait-ms: 500
  76. a-s-g-cache-expiry-timeout-ms: 60000
  77. peer-eureka-nodes-update-interval-ms: 15000
  78. renewal-threshold-update-interval-ms: 300000
  79. # 关闭保护机制,尽可能提出挂掉的服务节点
  80. enable-self-preservation: false

启动命令:

  1. java -jar eureka.jar --spring.profiles.active=eureka1
  2. java -jar eureka.jar --spring.profiles.active=eureka2
  3. java -jar eureka.jar --spring.profiles.active=eureka3

或者你和我一样通过 Idea 之类的去启动的,可以这样

使用三个启动类,每个启动类配置不同的 Activc profiles 来启动
通过 defaultZone 把多个 Eureka 注册上,这样一个 Eureka 挂了,还能通过其他 Eureka 来使用

四、Eureka 监听

有些时候只是当靠 Eureka 的监控并不能满足我们的需求,比如我们需要知道,哪些服务经常挂掉又重连了之类的
我们可以弄个简单的监听器进行监听

EurekaStateChangeListener.java

  1. /**
  2. * Eureka事件监听
  3. *
  4. * @Author: author
  5. * @Date: 2018/11/6 15:29
  6. */
  7. @Component
  8. public class EurekaStateChangeListener {
  9. private static final Logger LOGGER = LoggerFactory.getLogger(EurekaStateChangeListener.class);
  10. @EventListener
  11. public void listen(EurekaInstanceCanceledEvent event) {
  12. LOGGER.info(event.getServerId() + "\t" + event.getAppName() + " 服务下线");
  13. }
  14. @EventListener
  15. public void listen(EurekaInstanceRegisteredEvent event) {
  16. InstanceInfo instanceInfo = event.getInstanceInfo();
  17. LOGGER.info(instanceInfo.getAppName() + "进行注册");
  18. }
  19. @EventListener
  20. public void listen(EurekaInstanceRenewedEvent event) {
  21. LOGGER.info(event.getServerId() + "\t" + event.getAppName() + " 服务进行续约");
  22. }
  23. @EventListener
  24. public void listen(EurekaRegistryAvailableEvent event) {
  25. LOGGER.info("注册中心 启动");
  26. }
  27. @EventListener
  28. public void listen(EurekaServerStartedEvent event) {
  29. LOGGER.info("Eureka Server 启动");
  30. }
  31. }

简单效果图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/818223
推荐阅读
相关标签
  

闽ICP备14008679号