SpringbootAdmin集成-钉钉告警-功能

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:

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值