1、Eureka原理粗講java
2、Eureka幾大事件
git
EurekaInstanceCanceledEvent:失效事件。github
EurekaInstanceRegisteredEvent:註冊事件。spring
EurekaInstanceRenewedEvent:心跳事件。docker
EurekaRegistryAvailableEvent:可用事件。服務器
EurekaServerStartedEvent:啓動事件。微信
ApplicationListener:Spring事件監聽器 服務擴展失效通知。app
代碼以下:代碼文件在Eureka-server端ide
package com.github.wxiaoqi.security.center.listener; import com.netflix.discovery.shared.Applications; import com.netflix.eureka.EurekaServerContextHolder; import com.netflix.eureka.registry.PeerAwareInstanceRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent; import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent; import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent; import org.springframework.cloud.netflix.eureka.server.event.EurekaRegistryAvailableEvent; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; /** * 用於監聽eureka服務停機通知 * @Description:用於監聽eureka服務停機通知 * @Date:11:25 2017/11/10 */ @Configuration @EnableScheduling public class EurekaInstanceCanceledListener implements ApplicationListener { private Logger log = LoggerFactory.getLogger(EurekaInstanceCanceledListener.class); @Override public void onApplicationEvent(ApplicationEvent applicationEvent) { // 服務掛掉事件 if (applicationEvent instanceof EurekaInstanceCanceledEvent) { EurekaInstanceCanceledEvent event = (EurekaInstanceCanceledEvent) applicationEvent; // 獲取當前Eureka實例中的節點信息 PeerAwareInstanceRegistry registry = EurekaServerContextHolder.getInstance().getServerContext().getRegistry(); Applications applications = registry.getApplications(); // 遍歷獲取已註冊節點中與當前失效節點ID一致的節點信息 applications.getRegisteredApplications().forEach((registeredApplication) -> { registeredApplication.getInstances().forEach((instance) -> { if (instance.getInstanceId().equals(event.getServerId())) { log.info("服務:" + instance.getAppName() + " 掛啦。。。"); // // TODO: 2017/9/3 擴展消息提醒 郵件、手機短信、微信等 } }); }); } if (applicationEvent instanceof EurekaInstanceRegisteredEvent) { EurekaInstanceRegisteredEvent event = (EurekaInstanceRegisteredEvent) applicationEvent; log.info("服務:" + event.getInstanceInfo().getAppName() + " 註冊成功啦。。。"); } if (applicationEvent instanceof EurekaInstanceRenewedEvent) { EurekaInstanceRenewedEvent event = (EurekaInstanceRenewedEvent) applicationEvent; log.info("心跳檢測服務:" + event.getInstanceInfo().getAppName() + "。。"); } if (applicationEvent instanceof EurekaRegistryAvailableEvent) { log.info("服務 Aualiable。。"); } } }
測試效果:spring-boot
1:啓動Eureka-server
2:啓動Eureka-client:user-center
3:關閉Eureka-client:user-center
3、Eureka常見問題
踢出已關停的節點
因爲Eureka自我保護模式,以及心跳週期長的緣由,經常會遇到Eureka Server不踢出已關停的節點的問題。
server端:
eureka: server: enable-self-preservation: false # 設爲false,關閉自我保護 eviction-interval-timer-in-ms: 4000 # 清理間隔(單位毫秒,默認60*1000)
client端:
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ healthcheck: enabled: true # 開啓健康檢查(須要spring-boot-starter-actuator依賴) instance: lease-renewal-interval-in-seconds: 10 # 租期更新時間間隔(默認30秒) lease-expiration-duration-in-seconds: 30 # 租期到期時間(默認90秒)
多網卡環境下的IP選擇問題
Eureka會選擇IP合法(標準ipv4地址)、索引值最小(eth0,eht1中eth0優先)且不在忽略列表中(可在application配置文件中配置忽略哪些網卡)的網卡地址做爲服務IP。
配置:(????)
eureka: instance: # 多網卡制定IP,docker部署推薦 ip-address: 127.0.0.1 # 手動指定IP地址 prefer-ip-address: true # 註冊時使用ip而不是主機名
服務感知慢
Eureka的wiki上有一句話,大意是一個服務啓動後最長可能須要2分鐘時間才能被其它服務感知。
eureka.instance.leaseRenewallntervallnSecods(在生產中,最好堅持使用默認值,由於在服務器內部有一些計算,他們對續約作出假設)。