1.config
@Configuration
@ConditionalOnProperty(
prefix = "spring.boot.admin.notify.dingtalk",
name = {"webhook-token"}
)
@AutoConfigureBefore({NotifierTriggerConfiguration.class, CompositeNotifierConfiguration.class})
public class DingTalkNotifierConfiguration {
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = "spring.boot.admin.notify.dingtalk")
public DingRobotNotifier dingTalkNotifier(InstanceRepository repository) {
return new DingRobotNotifier(repository);
}
}
2.notifier
@Slf4j
public class DingRobotNotifier extends AbstractStatusChangeNotifier {
private RestTemplate restTemplate = new RestTemplate();
/**
* webhook-token
*/
private String webhookToken;
/**
* 是否可用
*/
private Boolean enable;
/**
* 消息类型
*/
private String msgtype;
/**
* 是否推送给所有成员
*/
private Boolean atAll;
/**
* 指定手机号
*/
private List atMobiles;
/**
* 指定人员
*/
private List atUserIds;
/**
* 自定义关键词
*/
private String title;
public DingRobotNotifier(InstanceRepository repository) {
super(repository);
}
@Override
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
HttpEntity<Map<String, Object>> message = createMessage(instance);
return Mono.fromRunnable(() -> restTemplate.postForEntity(webhookToken, message, Void.class));
}
private HttpEntity<Map<String, Object>> createMessage(Instance instance) {
//最外层
HashMap<String, Object> finalMap = new HashMap<>();
//msgtype
finalMap.put("msgtype", this.msgtype);
//text
HashMap<String, String> contentMap = new HashMap<>();
contentMap.put("content", this.getMessage(instance));
finalMap.put("text", contentMap);
//at
HashMap<String, Object> atInsideMap = new HashMap<>();
atInsideMap.put("isAtAll", this.atAll);
atInsideMap.put("atMobiles", this.atMobiles);
atInsideMap.put("atUserIds", this.atUserIds);
finalMap.put("at", atInsideMap);
log.info("finalMap:{}", JSON.toJSONString(finalMap));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return new HttpEntity<>(finalMap, headers);
}
private String getMessage(Instance instance) {
String status = instance.getStatusInfo().getStatus();
String serviceName = instance.getRegistration().getName();
String serviceUrl = instance.getRegistration().getServiceUrl();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Map<String, Object> details = instance.getStatusInfo().getDetails();
StringBuilder str = new StringBuilder();
str.append(">>>>>>>>服务告警<<<<<<<<" + "\n");
str.append("【服务名称】" + serviceName + "\n");
str.append("【预警时间】" + formatter.format(LocalDateTime.now()) + "\n");
str.append("【服务地址】" + serviceUrl + "\n");
str.append("【服务状态】" + status + "(" + ServerStatusInfo.statusInfo(status) + ")" + "\n");
str.append("【预警详情】" + JSON.toJSONString(details) + "\n\n\n");
return str.toString();
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String getWebhookToken() {
return webhookToken;
}
public void setWebhookToken(String webhookToken) {
this.webhookToken = webhookToken;
}
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Boolean getEnable() {
return enable;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
public Boolean getAtAll() {
return atAll;
}
public void setAtAll(Boolean atAll) {
this.atAll = atAll;
}
public List getAtUserIds() {
return atUserIds;
}
public void setAtUserIds(List atUserIds) {
this.atUserIds = atUserIds;
}
public void setAtMobiles(List atMobiles) {
this.atMobiles = atMobiles;
}
}
3.enum
@Getter
@AllArgsConstructor
public enum ServerStatusInfo {
STATUS_UP("UP", "服务上线"),
STATUS_DOWN("DOWN", "服务下线"),
STATUS_OFFLINE("OFFLINE", "服务离线"),
STATUS_RESTRICTED("RESTRICTED", "服务受限"),
STATUS_OUT_OF_SERVICE("OUT_OF_SERVICE", "无法使用"),
STATUS_UNKNOWN("UNKNOWN", "未知");
private String status;
private String describe;
public static String statusInfo(String status) {
for (ServerStatusInfo enumValue : ServerStatusInfo.values()) {
if (enumValue.getStatus().equals(status)) {
return enumValue.getDescribe();
}
}
return "未知";
}
}
4.nacos配置
spring:
boot:
admin:
ui:
title: 服务监控中心
brand: <span>服务监控中心</span>
notify:
dingtalk:
webhook-token: ***************************
enabled: true
msgtype: text
atAll: false
atMobiles: *************
atUserIds: