赞
踩
Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server,并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。
- <!--添加eureka服务端-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
作为注册中心,端口号是 8001,服务名用的 service-eureka
- #配置端口号
- server:
- port: 8001
-
- #服务名
- spring:
- application:
- name: service-eureka
这个 eureka 配置目的就是把服务添加到注册中心去
注册的地址是:http://localhost:8001/eureka/
- #eureka配置
- eureka:
- client:
- # 不把服务注册到注册中心
- # register-with-eureka: false
- fetch-registry: false
- service-url:
- defaultZone: http://localhost:8001/eureka/
- server:
- #主动失效时间
- eviction-interval-timer-in-ms: 30000
- registry-sync-retry-wait-ms: 500
- a-s-g-cache-expiry-timeout-ms: 60000
- peer-eureka-nodes-update-interval-ms: 15000
- renewal-threshold-update-interval-ms: 300000
- #测试环境关闭自我保护模式
- # enable-self-preservation: false
添加 @EnableEurekaServer 启动注册中心
由于作为注册中心没有数据库这些玩意,所以启动报错了 @SpringBootApplication 后面添加
exclude= {DataSourceAutoConfiguration.class}
- @EnableEurekaServer
- @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
- public class EurekaApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaApplication.class, args);
- }
- }
访问路径:localhost:8001
这里可以看到 service-eureka 作为一个服务注册到注册中心里了
- <!--添加eureka客户端-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
微服务的端口号是 8084,服务名用的 service-admin
- #配置端口号
- server:
- port: 8084
-
- #服务名
- spring:
- application:
- name: service-admin
依然把服务添加到注册中心去
注册的地址依然是:http://localhost:8001/eureka/
- #eureka配置
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8001/eureka/
- #间隔多少秒去服务端拉去注册信息
- registry-fetch-interval-seconds: 10
- instance:
- #发送心跳给server端频率
- lease-renewal-interval-in-seconds: 30
- #健康检查地址
- health-check-url-path: /actuator/health
- prefer-ip-address: true
@EnableEurekaClient 注解是基于spring-cloud-netflix依赖,只能eureka使用
@EnableDiscoveryClient 注解是基于spring-cloud-commons依赖,并且在classpath中实现
它们的作用是一样的,所以我选择 @EnableDiscoveryClient
添加 @EnableDiscoveryClient 将服务作为客户端注册到注册中心
- @EnableDiscoveryClient
- @SpringBootApplication
- public class AdminApplication {
- public static void main(String[] args) {
- SpringApplication.run(AdminApplication.class, args);
- }
- }
访问路径:localhost:8001
可以看到 admin 服务也成功注册到注册中心,说明配置成功
集群这种东西对于没接触过的人来说是个高大上的东西,但是对于接触过的人来说也就那样
Eureka 集群最主要的目的还是为了保证 Eureka 的稳定性,如果有个挂了,还有其他的Eureka 能担负起责任来
对配置文件稍微改动,变成 3 个 Eureka 的配置
可以看到通过 “---” 符号把配置文件分为 3 个配置,分别对应为 eureka1,eureka2,eureka3 以及各自的端口号
接下来我们可以通过 --spring.profiles.active=xx 去启动对用的配置
- #eureka1 配置
- server:
- port: 8001
-
- spring:
- profiles: eureka1
- application:
- #配置服务名
- name: eureka1
-
- #eureka配置
- eureka:
- client:
- # 是否将自己注册到eureka server
- register-with-eureka: true
- # 避免eureka查找服务列表
- fetch-registry: false
- service-url:
- defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/,http://localhost:8003/eureka/
- server:
- #主动失效时间
- eviction-interval-timer-in-ms: 30000
- registry-sync-retry-wait-ms: 500
- a-s-g-cache-expiry-timeout-ms: 60000
- peer-eureka-nodes-update-interval-ms: 15000
- renewal-threshold-update-interval-ms: 300000
- # 关闭保护机制,尽可能提出挂掉的服务节点
- enable-self-preservation: false
-
- ---
- #eureka2 配置
- server:
- port: 8002
-
- spring:
- profiles: eureka2
- application:
- #配置服务名
- name: eureka2
-
- #eureka配置
- eureka:
- client:
- # 是否将自己注册到eureka server
- register-with-eureka: true
- # 避免eureka查找服务列表
- fetch-registry: false
- service-url:
- defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/,http://localhost:8003/eureka/
- server:
- #主动失效时间
- eviction-interval-timer-in-ms: 30000
- registry-sync-retry-wait-ms: 500
- a-s-g-cache-expiry-timeout-ms: 60000
- peer-eureka-nodes-update-interval-ms: 15000
- renewal-threshold-update-interval-ms: 300000
- # 关闭保护机制,尽可能提出挂掉的服务节点
- enable-self-preservation: false
-
- ---
- #eureka3 配置
- server:
- port: 8003
-
- spring:
- profiles: eureka3
- application:
- #配置服务名
- name: eureka3
-
- #eureka配置
- eureka:
- client:
- # 是否将自己注册到eureka server
- register-with-eureka: true
- # 避免eureka查找服务列表
- fetch-registry: false
- service-url:
- defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/,http://localhost:8003/eureka/
- server:
- #主动失效时间
- eviction-interval-timer-in-ms: 30000
- registry-sync-retry-wait-ms: 500
- a-s-g-cache-expiry-timeout-ms: 60000
- peer-eureka-nodes-update-interval-ms: 15000
- renewal-threshold-update-interval-ms: 300000
- # 关闭保护机制,尽可能提出挂掉的服务节点
- enable-self-preservation: false
启动命令:
- java -jar eureka.jar --spring.profiles.active=eureka1
- java -jar eureka.jar --spring.profiles.active=eureka2
- java -jar eureka.jar --spring.profiles.active=eureka3
或者你和我一样通过 Idea 之类的去启动的,可以这样
使用三个启动类,每个启动类配置不同的 Activc profiles 来启动
通过 defaultZone 把多个 Eureka 注册上,这样一个 Eureka 挂了,还能通过其他 Eureka 来使用
有些时候只是当靠 Eureka 的监控并不能满足我们的需求,比如我们需要知道,哪些服务经常挂掉又重连了之类的
我们可以弄个简单的监听器进行监听
EurekaStateChangeListener.java
- /**
- * Eureka事件监听
- *
- * @Author: author
- * @Date: 2018/11/6 15:29
- */
- @Component
- public class EurekaStateChangeListener {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(EurekaStateChangeListener.class);
-
- @EventListener
- public void listen(EurekaInstanceCanceledEvent event) {
- LOGGER.info(event.getServerId() + "\t" + event.getAppName() + " 服务下线");
- }
-
- @EventListener
- public void listen(EurekaInstanceRegisteredEvent event) {
- InstanceInfo instanceInfo = event.getInstanceInfo();
- LOGGER.info(instanceInfo.getAppName() + "进行注册");
- }
-
- @EventListener
- public void listen(EurekaInstanceRenewedEvent event) {
- LOGGER.info(event.getServerId() + "\t" + event.getAppName() + " 服务进行续约");
- }
-
- @EventListener
- public void listen(EurekaRegistryAvailableEvent event) {
- LOGGER.info("注册中心 启动");
- }
-
- @EventListener
- public void listen(EurekaServerStartedEvent event) {
- LOGGER.info("Eureka Server 启动");
- }
- }
简单效果图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。