Spring Cloud Admin健康检查 邮件、钉钉群通知

开发 前端
SpringBootAdmin是一个用于管理和监控SpringBoot微服务的社区项目,可以使用客户端注册或者Eureka服务发现向服务端提供监控信息。

[[393804]]

源码地址:https://github.com/muxiaonong/Spring-Cloud/tree/master/cloudadmin

Admin 简介

官方文档:What is Spring Boot Admin?

SpringBootAdmin是一个用于管理和监控SpringBoot微服务的社区项目,可以使用客户端注册或者Eureka服务发现向服务端提供监控信息。注意,服务端相当于提供UI界面,实际的监控信息由客户端Actuator提供 通过SpringBootAdmin,你可以通过华丽大气的界面访问到整个微服务需要的监控信息,例如服务健康检查信息、CPU、内存、操作系统信息等等

本篇文章使用SpringBoot 2.3.3.RELEASE、SpringCloud Hoxton.SR6、SpringBoot Admin 2.2.3版本,此外,服务注册中心采用eureka

一、SpringCloud使用SpringBoot Admin

1.1 创建一个SpringBoot项目,命名为admin-test,引入如下依赖

<!-- Admin 服务 --> 
 <dependency> 
     <groupId>de.codecentric</groupId> 
     <artifactId>spring-boot-admin-starter-server</artifactId> 
     <version>2.2.1</version> 
 </dependency> 
 <!-- Admin 界面 --> 
 <dependency> 
     <groupId>de.codecentric</groupId> 
     <artifactId>spring-boot-admin-server-ui</artifactId> 
     <version>2.2.1</version> 
 </dependency> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

1.2 启动类

@SpringBootApplication 
@EnableAdminServer 
public class AdminTestApplication { 
 
    public static void main(String[] args) { 
        SpringApplication.run(AdminTestApplication.class, args); 
    } 
     
  } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

1.3 配置文件

spring.application.name=admin-test 
 
management.endpoints.jmx.exposure.include=* 
management.endpoints.web.exposure.include=* 
management.endpoint.health.show-details=always 
 
# spring cloud access&secret config 
alibaba.cloud.access-key=**** 
alibaba.cloud.secret-key=**** 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

1.4 启动项目

输入项目地址:http://localhost:8080/applications

二、配置邮件通知

2.1 pom

<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-mail</artifactId> 
</dependency> 
  • 1.
  • 2.
  • 3.
  • 4.

2.2 邮件配置

spring.mail.host=smtp.qq.com 
spring.mail.username=单纯QQ号 
spring.mail.password=授权码 
spring.mail.properties.mail.smpt.auth=true 
spring.mail.properties.mail.smpt.starttls.enable=true 
spring.mail.properties.mail.smpt.starttls.required=true 
 
#收件邮箱 
spring.boot.admin.notify.mail.to=xxxx@qq.com 
# 发件邮箱 
spring.boot.admin.notify.mail.from= xxxx@qq.com 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

2.3 QQ邮箱设置

找到自己的QQ邮箱

QQ邮箱 》 设置 》 账户 》红框处获取 授权码

我们将 consumer 服务下线后,

接着我们就收到了邮件通知,告诉我们服务关闭了


三、发送钉钉群通知

找到群里面的 群设置 》 智能群助手 》 添加机器人

注意:这里的自定义关键词一定要和项目的关键字匹配

获取 Webhook 到项目中,这个是后面要使用到的 

启动类:

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.Bean; 
 
import de.codecentric.boot.admin.server.config.EnableAdminServer; 
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository; 
 
@SpringBootApplication 
@EnableAdminServer 
public class AdminApplication { 
 
  public static void main(String[] args) { 
    SpringApplication.run(AdminApplication.class, args); 
  } 
     @Bean 
      public DingDingNotifier dingDingNotifier(InstanceRepository repository) { 
          return new DingDingNotifier(repository); 
      } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

 通知类:

import java.util.Map; 
 
import com.alibaba.fastjson.JSONObject; 
 
import de.codecentric.boot.admin.server.domain.entities.Instance; 
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository; 
import de.codecentric.boot.admin.server.domain.events.InstanceEvent; 
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier; 
import reactor.core.publisher.Mono; 
 
public class DingDingNotifier extends AbstractStatusChangeNotifier  { 
  public DingDingNotifier(InstanceRepository repository) { 
        super(repository); 
    } 
    @Override 
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) { 
        String serviceName = instance.getRegistration().getName(); 
        String serviceUrl = instance.getRegistration().getServiceUrl(); 
        String status = instance.getStatusInfo().getStatus(); 
        Map<String, Object> details = instance.getStatusInfo().getDetails(); 
        StringBuilder str = new StringBuilder(); 
        str.append("服务预警 : 【" + serviceName + "】"); 
        str.append("【服务地址】" + serviceUrl); 
        str.append("【状态】" + status); 
        str.append("【详情】" + JSONObject.toJSONString(details)); 
        return Mono.fromRunnable(() -> { 
            DingDingMessageUtil.sendTextMessage(str.toString()); 
        }); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

发送工具类

import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
 
import com.alibaba.fastjson.JSONObject; 
 
public class DingDingMessageUtil { 
  public static String access_token = "Token"
    public static void sendTextMessage(String msg) { 
        try { 
            Message message = new Message(); 
            message.setMsgtype("text"); 
            message.setText(new MessageInfo(msg)); 
            URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + access_token); 
            // 建立 http 连接 
            HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
            conn.setDoOutput(true); 
            conn.setDoInput(true); 
            conn.setUseCaches(false); 
            conn.setRequestMethod("POST"); 
            conn.setRequestProperty("Charset""UTF-8"); 
            conn.setRequestProperty("Content-Type""application/Json; charset=UTF-8"); 
            conn.connect(); 
            OutputStream out = conn.getOutputStream(); 
            String textMessage = JSONObject.toJSONString(message); 
            byte[] data = textMessage.getBytes(); 
            out.write(data); 
            out.flush(); 
            out.close(); 
            InputStream in = conn.getInputStream(); 
            byte[] data1 = new byte[in.available()]; 
            in.read(data1); 
            System.out.println(new String(data1)); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

消息类:

public class Message { 
  private String msgtype; 
    private MessageInfo text; 
    public String getMsgtype() { 
        return msgtype; 
    } 
    public void setMsgtype(String msgtype) { 
        this.msgtype = msgtype; 
    } 
    public MessageInfo getText() { 
        return text; 
    } 
    public void setText(MessageInfo text) { 
        this.text = text; 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
public class MessageInfo { 
    private String content; 
    public MessageInfo(String content) { 
        this.content = content; 
    } 
    public String getContent() { 
        return content; 
    } 
    public void setContent(String content) { 
        this.content = content; 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

我们下线一个服务后,就可以看到钉钉群就发了消息的通知

同时,当我们启动服务的时候,也会有消息通知我们服务启动了

 

四 总结

上面就是我们对admin 健康检查的实际应用,在企业中一般会有短信通知+钉钉群通知和邮件,感兴趣的小伙伴可以去试试看,还是挺好玩的,还有一个就是微信通知,在服务号 模板消息感兴趣的小伙伴可以自行去研究看看,大家加油~

 

责任编辑:姜华 来源: 牧小农
相关推荐

2023-03-02 07:20:10

GRPC服务健康检查协议

2022-02-28 07:40:23

Nacos注册中心客户端

2023-03-03 08:19:35

KubernetesgRPC

2023-03-01 08:33:37

gRPC健康检查代码

2017-08-25 10:20:46

Docker容器机制

2020-02-17 15:17:57

钉钉

2021-04-21 09:16:04

Python开发钉钉群机器人

2020-04-14 15:33:37

Python 钉钉机器人

2021-07-15 10:25:15

集群节点检查

2021-05-29 14:14:16

阿里云钉钉低代码开发

2021-09-18 16:10:48

Spring BootJava微服务

2023-10-14 15:36:14

PodKubernetes

2024-02-27 17:30:11

2020-12-07 06:29:13

SpringBoot

2023-05-09 07:34:25

Docker健康检查方式

2018-08-10 12:56:00

大数据

2023-08-22 20:48:06

模型钉钉阿里云

2020-06-10 14:01:46

阿里云钉钉Windows

2022-12-06 08:00:16

awscli工具监控

2024-02-02 09:36:14

自定义排序钉钉群
点赞
收藏

51CTO技术栈公众号