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

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

源码地址: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 启动类

@SpringBootApplication
@EnableAdminServer
public class AdminTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminTestApplication.class, args);
    }
    
  }

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.4 启动项目

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

在这里插入图片描述

二、配置邮件通知

2.1 pom

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

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

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);
	    }
}

通知类:

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());
        });
    }
}

发送工具类

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();
        }
    }
}

消息类:

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;
    }
}

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;
    }
}

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

在这里插入图片描述

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

在这里插入图片描述
在这里插入图片描述

四 总结

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2722.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 基于Chrome浏览器的HackBar_v2.2.6插件的安装与注册「建议收藏」

    基于Chrome浏览器的HackBar_v2.2.6插件的安装与注册「建议收藏」Hackbar是一款基于浏览器的简单的安全审计或者说是渗透测试工具,能够帮助您测试sql注入,XSS漏洞和站点安全性,帮助开发人员对其代码进行安全审计。本篇博客介绍的是基于Chrome浏览器的hackbar插件的安装与注册,在Firebox浏览器与Chrome浏览器上的安装类似,对在firebox的安装本篇博客不再做说明。 HackBar_v2.2.6下载和安装 1、打开Chrome…

    2022年6月1日
    280
  • android 图片识别文字,安卓手机如何识别图片中的文字?一个方法轻松解决难题…

    android 图片识别文字,安卓手机如何识别图片中的文字?一个方法轻松解决难题…现在使用安卓手机的人并不少,有时在工作生活中,需要利用安卓手机将图片中的文字识别提取出来,这个时候你会吗?相信很多人的答案是否定的,那么安卓手机如何识别图片中的文字呢?下面我们就一起来看看吧。想要利用安卓手机将图片中的文字识别提取出来,你只需要这样做就行:很简单,只要在安卓手机上下载安装一个专门的图片文字识别APP即可。那这个图片文字识别APP是什么呢?现在图片文字识别APP是很多,小编比较常用的…

    2022年4月28日
    73
  • httprunner(4)录制生成测试用例[通俗易懂]

    httprunner(4)录制生成测试用例[通俗易懂]前言写用例之前,我们应该熟悉API的详细信息。建议使用抓包工具Charles或AnyProxy进行抓包。har2case我们先来了解一下另一个项目har2case他的工作原理就是将当前主流的抓

    2022年7月31日
    5
  • 2021年最新Java学习路线图指南

    2021年最新Java学习路线图指南Java在编程语言排行榜中一直牢牢占据榜首位置,几乎所有的大中型互联网的应用系统在服务器端开发首选都是Java编程,正因如何吸引这不少年轻人投入该行业,Java虽不想其它编程语言那么复杂,但是知识体系还是很庞大的,因此想要学好并非容易之事,不少想要跨入Java编程行业的同学们通过网络搜索各式各样的学习资料,却往往缺乏系统而全面的学习路线。动力节点深知同学们的学习困难,为此整理了一套最新的2021年新版学习路线图,增加了目前企业最新应用技术,这套学习路线图,只要你完成一半基本就可以找到很不错的…

    2022年5月17日
    39
  • 大数据采集平台之ZDH_SERVER部署

    大数据采集平台之ZDH_SERVER部署目录项目源码下载源码打包部署运行项目源码数据采集平台管理端https://github.com/zhaoyachao/zdh_web数据采集平台服务https://github.com/zhaoyachao/zdh_serverweb端在线查看http://zycblog.cn:8081/login用户名:zyc密码:123456界面只是为了参考功能,底层的数据采集服务需要自己下载zdh_server部署,服务器资源有限,请手下留情如.

    2022年6月10日
    50
  • 网络通信基础知识总结报告_数据通信与计算机网络知识点总结

    网络通信基础知识总结报告_数据通信与计算机网络知识点总结1.常见术语说明 数据载荷 在具有层次化结构的网络通信过程中,上层协议传递给下层协议的数据单元(报文)都可以称之为下一层协议的载荷数据。 报文 报文是网络交换与传输的数据单元,它具有一定的内在格式,并通常都具有头部+数据载荷+尾部的基本结构。在传输过程中,报文的格式和内容可能会发生改变。 头部 …

    2022年9月21日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号