update 优化Monitor监控服务信息事件通知
This commit is contained in:
parent
52b0fa9a54
commit
e74f0ca6f8
@ -39,6 +39,11 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -0,0 +1,44 @@
|
||||
package org.dromara.monitor.admin.event;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 通知事件
|
||||
*
|
||||
* @author AprilWind
|
||||
*/
|
||||
@Data
|
||||
public class NotifierEvent implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 实例注册名称
|
||||
*/
|
||||
private String registName;
|
||||
|
||||
/**
|
||||
* 实例状态名称
|
||||
*/
|
||||
private String statusName;
|
||||
|
||||
/**
|
||||
* 实例ID
|
||||
*/
|
||||
private String instanceId;
|
||||
|
||||
/**
|
||||
* 实例状态
|
||||
*/
|
||||
private String status;
|
||||
|
||||
/**
|
||||
* 服务URL
|
||||
*/
|
||||
private String serviceUrl;
|
||||
|
||||
}
|
@ -6,6 +6,8 @@ import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
|
||||
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
|
||||
import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.common.core.utils.SpringUtils;
|
||||
import org.dromara.monitor.admin.event.NotifierEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@ -38,46 +40,24 @@ public class CustomNotifier extends AbstractEventNotifier {
|
||||
String status = ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus();
|
||||
// 获取服务URL
|
||||
String serviceUrl = instance.getRegistration().getServiceUrl();
|
||||
String statusName = null;
|
||||
|
||||
// 根据实例状态执行相应的操作
|
||||
switch (status) {
|
||||
// 服务上线(实例成功启动并可以正常处理请求)
|
||||
case STATUS_UP:
|
||||
statusName = "服务上线";
|
||||
break;
|
||||
|
||||
// 服务离线(实例被手动或自动地从服务中移除)
|
||||
case STATUS_OFFLINE:
|
||||
statusName = "服务离线";
|
||||
break;
|
||||
|
||||
// 服务受限(表示实例在某些方面受限,可能无法完全提供所有服务)
|
||||
case STATUS_RESTRICTED:
|
||||
statusName = "服务受限";
|
||||
break;
|
||||
|
||||
// 停止服务状态(表示实例已被标记为停止提供服务,可能是计划内维护或测试)
|
||||
case STATUS_OUT_OF_SERVICE:
|
||||
statusName = "停止服务状态";
|
||||
break;
|
||||
|
||||
// 服务下线(实例因崩溃、错误或其他原因停止运行)
|
||||
case STATUS_DOWN:
|
||||
statusName = "服务下线";
|
||||
break;
|
||||
|
||||
// 服务未知异常(监控系统无法确定实例的当前状态)
|
||||
case STATUS_UNKNOWN:
|
||||
statusName = "服务未知异常";
|
||||
break;
|
||||
|
||||
// 默认情况(没有匹配的状态)
|
||||
default:
|
||||
break;
|
||||
}
|
||||
String statusName = switch (status) {
|
||||
case STATUS_UP -> "服务上线"; // 实例成功启动并可以正常处理请求
|
||||
case STATUS_OFFLINE -> "服务离线"; //实例被手动或自动地从服务中移除
|
||||
case STATUS_RESTRICTED -> "服务受限"; //表示实例在某些方面受限,可能无法完全提供所有服务
|
||||
case STATUS_OUT_OF_SERVICE -> "停止服务状态"; //表示实例已被标记为停止提供服务,可能是计划内维护或测试
|
||||
case STATUS_DOWN -> "服务下线"; //实例因崩溃、错误或其他原因停止运行
|
||||
case STATUS_UNKNOWN -> "服务未知异常"; //监控系统无法确定实例的当前状态
|
||||
default -> "未知状态"; //没有匹配的状态
|
||||
};
|
||||
log.info("Instance Status Change: 状态名称【{}】, 注册名称【{}】, 实例ID【{}】, 状态【{}】, 服务URL【{}】",
|
||||
statusName, registName, instanceId, status, serviceUrl);
|
||||
NotifierEvent notifier = new NotifierEvent();
|
||||
notifier.setRegistName(registName);
|
||||
notifier.setStatusName(statusName);
|
||||
notifier.setInstanceId(instanceId);
|
||||
notifier.setStatus(status);
|
||||
notifier.setServiceUrl(serviceUrl);
|
||||
SpringUtils.context().publishEvent(notifier);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package org.dromara.monitor.admin.notifier;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.monitor.admin.event.NotifierEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 信息通知
|
||||
*
|
||||
* @author AprilWind
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@Service
|
||||
public class InfoNotifier {
|
||||
|
||||
/**
|
||||
* 异步处理通知事件的方法
|
||||
* <p>
|
||||
* 该方法会处理 `NotifierEvent` 事件,执行通知相关的操作,如发送邮件或短信
|
||||
*
|
||||
* @param notifier 事件对象,包含了需要通知的详细信息,包括注册名称、状态名称、实例 ID、状态和服务 URL
|
||||
*/
|
||||
@Async
|
||||
@EventListener
|
||||
public void infoNotification(NotifierEvent notifier) {
|
||||
// 在这里添加处理通知事件的逻辑
|
||||
// 例如,依据 notifier 对象的信息发送邮件或短信通知
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user