Spring Cloud之Eureka對服務的監控處理等


一,應用場景:多個服務啟動后可能會因為一些其他或者內在原因導致服務會掛掉,而我們維護人員不知情的情況下服務會一直掛到有人發現為止,

所以我們需要對Eureka的各個服務進行監聽,當他們發生上線,下線,啟動等,利用短信,郵箱等通知維護人員,維護人員會第一時間了解服務情況,進行處理。

二.Eureka服務狀態分類

Eureka的server端會發出5個事件通知,分別是:

2.1  EurekaInstanceCanceledEvent                     當有服務下線時會執行(掉線顧名思義,就是某個服務關閉時觸發)
2.2  EurekaInstanceRegisteredEvent                   當有服務注冊時會執行(注冊中心檢測到有新的服務注冊進來時觸發)
2.3  EurekaInstanceRenewedEvent                     當有服務續約時會執行(服務設置了心跳時間,如果下一次心跳還正常,就會把服務續約的信息更新到自身的Eureka Server中,然后再同步到其它Eureka Server中)
2.4  EurekaRegistryAvailableEvent                      Eureka 注冊中心啟動執行

2.5  EurekaServerStartedEvent                            Eureka Server 啟動時執行

那接下來就好說了

第一步 : 引入maven依賴

略: 就是基本的依賴

第二步 : 監聽服務

在網上查詢了部分資料之后,發現部分的博客缺少一些東西,會對新手有很多的誤導,在這里給出一個詳細的步驟

 

  Eureka服務端的配置

server:
  port: 9999

spring:
  application:
    name: eureka01
  security:
    user:
      name: root
      password: root

eureka:
  client:
    register-with-eureka: false #是否注冊給服務中心,本身就是注冊中心,所以不需要注冊
    service-url:
      defaultZone: http://root:root@localhost:9997/eureka/,http://root:root@localhost:9998/eureka/   #分別指向除自己外的注冊中心地址,多個地址之間用“,”號隔開
  server:
    enable-self-preservation: false   # 測試時關閉自我保護機制,保證不可用服務及時踢出
    eviction-interval-timer-in-ms: 5000 # 續期時間,即掃描失效服務的間隔時間(缺省為60*1000ms)

  

服務配置這里的 關閉自我保護和清理間隔一定要打開 (雖然我也不知道為什么)

啟動類

package com.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
 * Hello world!
 *
 */ @SpringBootApplication @EnableEurekaServer public class EurekaServer extends WebSecurityConfigurerAdapter{ public static void main( String[] args ) { SpringApplication.run(EurekaServer.class, args); } @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable();//關閉csrf  super.configure(http); } }

 

  服務監聽類

 

package com.web.event;

import com.netflix.appinfo.InstanceInfo;
import lombok.extern.log4j.Log4j2; import org.springframework.cloud.netflix.eureka.server.event.*; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Log4j2 @Component public class EurekaStateListener { //private final static Logger logger = LoggerFactory.getLogger(EurekaStateListener.class);  @EventListener public void listen(EurekaInstanceCanceledEvent event) { log.info("服務{}已下線", event.getAppName()); log.info("server地址信息{}", event.getServerId()); } @EventListener public void listen(EurekaInstanceRegisteredEvent event) { InstanceInfo instanceInfo = event.getInstanceInfo(); log.info("服務{}進行注冊", instanceInfo.getAppName()+ instanceInfo.getHostName() +" "+ instanceInfo.getIPAddr() +" "+ instanceInfo.getPort()); } @EventListener public void listen(EurekaInstanceRenewedEvent event) { log.info("服務{}進行續約", event.getServerId() +" "+ event.getAppName()); } @EventListener public void listen(EurekaRegistryAvailableEvent event) { log.info("注冊中心啟動,{}", System.currentTimeMillis()); } @EventListener public void listen(EurekaServerStartedEvent event) { log.info("注冊中心服務端啟動,{}", System.currentTimeMillis()); } }

這里只是簡單的記錄,實際項目還是要做大量的通知,如果有注冊中心是集群,挨個如上配置即可,我這就是集群的配置,以一個作文樣列,就不每個都展示出來了。

 

第三步 : 客戶端

       客戶端配置

server:
  port: 8032 spring: application: name: productAclient eureka: instance: lease-renewal-interval-in-seconds: 1 #每間隔10s,向服務端發送一次心跳 lease-expiration-duration-in-seconds: 2 #如果我2s之內沒有給你發心跳,就代表我“死”了,將我踢出掉。 prefer-ip-address: true hostname: 192.168.1.83 instance-id: ${eureka.instance.hostname}:${server.port} client: service-url: defaultZone: http://root:root@localhost:9998/eureka/,http://root:root@localhost:9997/eureka/,http://root:root@localhost:9999/eureka/

這里的instance配置一定要寫,很多新手忘記寫這個導致服務下線無法被監聽,因為服務下線后eureka沒有將該服務踢出

 

啟動類

package com.web;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * Hello world! * */ @SpringBootApplication @EnableEurekaClient public class productA_Client { public static void main( String[] args ) { SpringApplication.run(productA_Client.class); } }

 

第四步 : 測試看效果

啟動注冊中心

2019-07-10 11:10:33.966  INFO 17696 --- [      Thread-30] c.n.e.r.PeerAwareInstanceRegistryImpl    : Changing status to UP
2019-07-10 11:10:33.974  INFO 17696 --- [      Thread-30] e.s.EurekaServerInitializerConfiguration : Started Eureka Server 2019-07-10 11:10:33.977 INFO 17696 --- [ Thread-30] com.web.event.EurekaStateListener : 注冊中心啟動,1562728233974 2019-07-10 11:10:33.978 INFO 17696 --- [ Thread-30] com.web.event.EurekaStateListener : 注冊中心服務端啟動,1562728233978 2019-07-10 11:10:33.990 INFO 17696 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9998 (http) with context path '' 2019-07-10 11:10:33.991 INFO 17696 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9998

服務注冊

2019-07-10 11:10:35.860  INFO 17696 --- [nio-9998-exec-3] com.web.event.EurekaStateListener        : 服務PRODUCTACLIENT192.168.1.83  192.168.1.83  8031進行注冊
2019-07-10 11:10:35.875  INFO 17696 --- [nio-9998-exec-3] c.n.e.registry.AbstractInstanceRegistry  : Registered instance PRODUCTACLIENT/192.168.1.83:8031 with status UP (replication=true) 2019-07-10 11:10:35.876 INFO 17696 --- [nio-9998-exec-3] com.web.event.EurekaStateListener : 服務PRODUCTACLIENT192.168.1.83 192.168.1.83 8030進行注冊 2019-07-10 11:10:35.876 INFO 17696 --- [nio-9998-exec-3] c.n.e.registry.AbstractInstanceRegistry : Registered instance PRODUCTACLIENT/192.168.1.83:8030 with status UP (replication=true) 2019-07-10 11:10:36.389 INFO 17696 --- [nio-9998-exec-4] com.web.event.EurekaStateListener : 服務PRODUCTACLIENT192.168.1.83 192.168.1.83 8031進行注冊 2019-07-10 11:10:36.389 INFO 17696 --- [nio-9998-exec-4] c.n.e.registry.AbstractInstanceRegistry : Registered instance PRODUCTACLIENT/192.168.1.83:8031 with status UP (replication=true) 2019-07-10 11:10:36.390 INFO 17696 --- [nio-9998-exec-4] com.web.event.EurekaStateListener : 服務192.168.1.83:8030 PRODUCTACLIENT進行續約 2019-07-10 11:10:36.391 INFO 17696 --- [nio-9998-exec-4] com.web.event.EurekaStateListener : 服務192.168.1.83:8031 PRODUCTACLIENT進行續約 2019-07-10 11:10:37.407 INFO 17696 --- [nio-9998-exec-5] com.web.event.EurekaStateListener : 服務192.168.1.83:8030 PRODUCTACLIENT進行續約 2019-07-10 11:10:37.408 INFO 17696 --- [nio-9998-exec-5] com.web.event.EurekaStateListener : 服務192.168.1.83:8031 PRODUCTACLIENT進行續約 2019-07-10 11:10:38.415 INFO 17696 --- [nio-9998-exec-6] com.web.event.EurekaStateListener : 服務192.168.1.83:8030 PRODUCTACLIENT進行續約 2019-07-10 11:10:38.416 INFO 17696 --- [nio-9998-exec-6] com.web.event.EurekaStateListener : 服務192.168.1.83:8031 PRODUCTACLIENT進行續約 2019-07-10 11:10:38.967 INFO 17696 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms

停止8082端口的服務

2018-07-24 09:41:59.599  INFO 16524 --- [a-EvictionTimer] com.lh.event.EurekaStateListener         : 服務MEMBER已下線
2018-07-24 09:41:59.600  INFO 16524 --- [a-EvictionTimer] com.lh.event.EurekaStateListener         : server地址信息DESKTOP-SHDQ5I0:member:8082
2018-07-24 09:41:59.601  INFO 16524 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Cancelled instance MEMBER/DESKTOP-SHDQ5I0:member:8082 (replication=false)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM