Eureka的5个监听事件,实现自定义监控报警
2018-09-04 本文已影响75人
无妄zxc
image.png
正如大家在图中所看到的那样,eureka定义了5个监听事件,分别是:
EurekaServerStartedEvent - Eureka服务端启动事件
EurekaRegistryAvailableEvent - Eureka服务端可用事件
EurekaInstanceRegisteredEvent - Eureka客户端服务注册事件
EurekaInstanceRenewedEvent - Eureka客户端续约事件
EurekaInstanceCanceledEvent - Eureka客户端下线事件
通过查看代码我们可以发现EurekaInstanceRegisteredEvent 和EurekaServerStartedEvent事件会在eureka服务启动后被触发。
首先触发Eureka服务器EurekaInstanceRegisteredEvent,然后判断如果eureka服务器已经在运行,则触发EurekaServerStartedEvent。
@Override
public void start() {
new Thread(new Runnable() {
@Override
public void run() {
try {
//TODO: is this class even needed now?
eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext);
log.info("Started Eureka Server");
publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig()));
EurekaServerInitializerConfiguration.this.running = true;
publish(new EurekaServerStartedEvent(getEurekaServerConfig()));
}
catch (Exception ex) {
// Help!
log.error("Could not initialize Eureka servlet context", ex);
}
}
}).start();
}
这两个事件不是我们的重点,我们来看一下剩余的三个事件。
EurekaInstanceRegisteredEvent 该事件在每次有微服务进行注册时触发。
private void handleRegistration(InstanceInfo info, int leaseDuration,
boolean isReplication) {
log("register " + info.getAppName() + ", vip " + info.getVIPAddress()
+ ", leaseDuration " + leaseDuration + ", isReplication "
+ isReplication);
publishEvent(new EurekaInstanceRegisteredEvent(this, info, leaseDuration,
isReplication));
}
EurekaInstanceRenewedEvent续约事件
public boolean renew(final String appName, final String serverId,
boolean isReplication) {
log("renew " + appName + " serverId " + serverId + ", isReplication {}"
+ isReplication);
List<Application> applications = getSortedApplications();
for (Application input : applications) {
if (input.getName().equals(appName)) {
InstanceInfo instance = null;
for (InstanceInfo info : input.getInstances()) {
if (info.getId().equals(serverId)) {
instance = info;
break;
}
}
publishEvent(new EurekaInstanceRenewedEvent(this, appName, serverId,
instance, isReplication));
break;
}
}
return super.renew(appName, serverId, isReplication);
}
EurekaInstanceCanceledEvent客户端下线触发事件
private void handleCancelation(String appName, String id, boolean isReplication) {
log("cancel " + appName + ", serverId " + id + ", isReplication " + isReplication);
publishEvent(new EurekaInstanceCanceledEvent(this, appName, id, isReplication));
}
通过对这5个事件进行监听,我们可以实现注册中心报警
@Component
public class EurekaStateChangeListener {
@EventListener
public void listen(EurekaInstanceCanceledEvent eurekaInstanceCanceledEvent) {
//服务断线事件
String appName = eurekaInstanceCanceledEvent.getAppName();
String serverId = eurekaInstanceCanceledEvent.getServerId();
System.out.println(appName);
System.out.println(serverId);
System.out.println("EurekaInstanceCanceledEvent");
}
@EventListener
public void listen(EurekaInstanceRegisteredEvent event) {
InstanceInfo instanceInfo = event.getInstanceInfo();
System.out.println(instanceInfo);
System.out.println("EurekaInstanceRegisteredEvent");
}
@EventListener
public void listen(EurekaInstanceRenewedEvent event) {
event.getAppName();
event.getServerId();
System.out.println("EurekaInstanceRenewedEvent");
}
@EventListener
public void listen(EurekaRegistryAvailableEvent event) {
System.out.println("EurekaRegistryAvailableEvent");
}
@EventListener
public void listen(EurekaServerStartedEvent event) {
System.out.println("EurekaServerStartedEvent");
//Server启动
}
}