在一些场景下,我们需要监听eureka服务中心的一些状态,譬如某个微服务挂掉了,我们希望能监听到,并给管理员发送邮件通知或钉钉告警。
一、Eureka的监听事件,可以用来监控、告警
EurekaInstanceRegisteredEvent 服务注册事件EurekaInstanceRenewedEvent 服务续约事件,续约即心跳EurekaRegistryAvailableEvent Eureka注册中心启动事件EurekaServerStartedEvent Eureka Server启动事件EurekaInstanceCanceledEvent 服务下线事件package com.mimaxueyuan.cloud.eureka.event;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.cloud.netflix.eureka.server.event.EurekaServerStartedEvent;import org.springframework.context.event.EventListener;import org.springframework.stereotype.Component;import com.netflix.appinfo.InstanceInfo;@Componentpublic class KevinEventLinstener { private Logger logger = LoggerFactory.getLogger(KevinEventLinstener.class); @EventListener public void listen(EurekaInstanceRegisteredEvent event) { InstanceInfo instanceInfo = event.getInstanceInfo(); String ip = instanceInfo.getIPAddr(); String id = instanceInfo.getInstanceId(); logger.info(">>>>>>>>" + id + "已经注册到Eureka,IP=" + ip); } @EventListener public void listen(EurekaInstanceRenewedEvent event) { InstanceInfo instanceInfo = event.getInstanceInfo(); String id = instanceInfo.getInstanceId(); logger.info(">>>>>>>>"+id+"续约事件触发..."); } @EventListener public void listen(EurekaRegistryAvailableEvent event) { logger.info(">>>>>>>>注册中心启动事件触发..."); } @EventListener public void listen(EurekaServerStartedEvent event) { logger.info(">>>>>>>>EurekaServer启动事件触发..."); } @EventListener public void listen(EurekaInstanceCanceledEvent event) { String id = event.getServerId(); logger.info(">>>>>>>>"+id+"从Eureka下线..."); }}
二、注解@DiscoveryClient与@EurekaDiscoveryClient
package com.mimaxueyuan.producer.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class EurekaController { // 以下两个客户端类,用哪个都可以,DiscoveryClient抽象程度更高,是一个接口,EurekaDiscoveryClient只是他的抽象类 @Autowired private DiscoveryClient discoveryClient; // eureka使用展示 @GetMapping("/eureka/test") public String eureka() { System.out.println("eureka使用展示:/eureka/get---start"); // ----------------- 以下的代码使用DiscoveryClient------------------- // 查询所有注册到Eureka上的服务: System.out.println("[discoveryClient]-查询所有注册到Eureka上的服务:"); for (String string : discoveryClient.getServices()) { System.out.println(string); } // 查询某一个provider的所有Service实例 System.out.println("[discoveryClient]-查询某一个provider的所有Service实例"); Listinstances = discoveryClient.getInstances("mima-cloud-producer"); for (ServiceInstance instance : instances) { System.out.println("host:" + instance.getHost() + ",port:" + instance.getPort() + ",serviceId=" + instance.getServiceId() + ",uri=" + instance.getUri()); System.out.println("metadata=" + instance.getMetadata()); } // TODO 已经不推荐使用 ServiceInstance localServiceInstance = discoveryClient.getLocalServiceInstance(); return "eureka's demo, please to see console output"; }}