源码地址: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,引入如下依赖

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

1.2 启动类

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

1.3 配置文件

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

1.4 启动项目

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

二、配置邮件通知

2.1 pom

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

2.2 邮件配置

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

2.3 QQ邮箱设置

找到自己的QQ邮箱

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



我们将 consumer 服务下线后,

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

三、发送钉钉群通知

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



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



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



启动类:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.context.annotation.Bean;
  4. import de.codecentric.boot.admin.server.config.EnableAdminServer;
  5. import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
  6. @SpringBootApplication
  7. @EnableAdminServer
  8. public class AdminApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(AdminApplication.class, args);
  11. }
  12. @Bean
  13. public DingDingNotifier dingDingNotifier(InstanceRepository repository) {
  14. return new DingDingNotifier(repository);
  15. }
  16. }

通知类:

  1. import java.util.Map;
  2. import com.alibaba.fastjson.JSONObject;
  3. import de.codecentric.boot.admin.server.domain.entities.Instance;
  4. import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
  5. import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
  6. import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
  7. import reactor.core.publisher.Mono;
  8. public class DingDingNotifier extends AbstractStatusChangeNotifier {
  9. public DingDingNotifier(InstanceRepository repository) {
  10. super(repository);
  11. }
  12. @Override
  13. protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
  14. String serviceName = instance.getRegistration().getName();
  15. String serviceUrl = instance.getRegistration().getServiceUrl();
  16. String status = instance.getStatusInfo().getStatus();
  17. Map<String, Object> details = instance.getStatusInfo().getDetails();
  18. StringBuilder str = new StringBuilder();
  19. str.append("服务预警 : 【" + serviceName + "】");
  20. str.append("【服务地址】" + serviceUrl);
  21. str.append("【状态】" + status);
  22. str.append("【详情】" + JSONObject.toJSONString(details));
  23. return Mono.fromRunnable(() -> {
  24. DingDingMessageUtil.sendTextMessage(str.toString());
  25. });
  26. }
  27. }

发送工具类

  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import com.alibaba.fastjson.JSONObject;
  6. public class DingDingMessageUtil {
  7. public static String access_token = "Token";
  8. public static void sendTextMessage(String msg) {
  9. try {
  10. Message message = new Message();
  11. message.setMsgtype("text");
  12. message.setText(new MessageInfo(msg));
  13. URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
  14. // 建立 http 连接
  15. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  16. conn.setDoOutput(true);
  17. conn.setDoInput(true);
  18. conn.setUseCaches(false);
  19. conn.setRequestMethod("POST");
  20. conn.setRequestProperty("Charset", "UTF-8");
  21. conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
  22. conn.connect();
  23. OutputStream out = conn.getOutputStream();
  24. String textMessage = JSONObject.toJSONString(message);
  25. byte[] data = textMessage.getBytes();
  26. out.write(data);
  27. out.flush();
  28. out.close();
  29. InputStream in = conn.getInputStream();
  30. byte[] data1 = new byte[in.available()];
  31. in.read(data1);
  32. System.out.println(new String(data1));
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }

消息类:

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

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

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



四 总结

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

Spring Cloud Admin健康检查 邮件、钉钉群通知的更多相关文章

  1. Spring Boot Actuator:健康检查、审计、统计和监控(转)

    Spring Boot Actuator可以帮助你监控和管理Spring Boot应用,比如健康检查.审计.统计和HTTP追踪等.所有的这些特性可以通过JMX或者HTTP endpoints来获得. ...

  2. Spring Boot Actuator:健康检查、审计、统计和监控

    Spring Boot Actuator可以帮助你监控和管理Spring Boot应用,比如健康检查.审计.统计和HTTP追踪等.所有的这些特性可以通过JMX或者HTTP endpoints来获得. ...

  3. spring cloud:搭建基于consul的服务提供者集群(spring cloud hoxton sr8 / spring boot 2.3.4)

    一,搭建基于consul的服务提供者集群 1,consul集群,共3个实例: 2, 服务提供者集群:共2个实例: 3,服务消费者:一个实例即可 4,consul集群的搭建,请参考: https://w ...

  4. SpringCloud学习笔记(16)----Spring Cloud Netflix之Hystrix Dashboard+Turbine集群监控

    前言: 上一节中,我们使用Hystrix Dashboard,只能看到单个应用内的服务信息.在生产环境中,我们经常是集群状态,所以我们需要用到Turbine这一应用. 作用:汇总系统内的多个服务的数据 ...

  5. Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

    本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...

  6. Spring Boot Admin实现服务健康预警

    Over View 上一篇文章主要介绍了Spring Boot Admin的概况以及我们如何在系统中引入和使用Spring Boot Admin,以此来帮助我们更加了解自己的系统,做到能快速发现.排查 ...

  7. Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知

    1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...

  8. Spring Boot Admin 监控中心

    Spring Boot Admin 监控中心 Spring Boot Admin用来收集微服务系统的健康状态.会话数量.并发数.服务资源.延迟等度量信息 服务端 建立spring-cloud-admi ...

  9. Spring Cloud Alibaba基础教程-Nacos(三)

    在Spring Cloud Alibaba基础教程-Nacos(二)当中学习了,如何使用 nacos图形化界面操作 ,使用Nacos部署集群,下面我们开始Nacos最后一篇的学习 ,如果对你有帮助,记 ...

随机推荐

  1. 【Laravel】为Eloquent 模型设置全局作用域和局部作用域进行查询

    全局作用域 所谓「全局作用域」,指的是预置过滤器在注册该「全局作用域」的模型类的所有查询中生效,不需要指定任何额外条件. 以 User 模型类为例,我们在系统中可能只想针对已经验证过邮箱的用户进行操作 ...

  2. Python 为什么要有 pass 语句?

    本文出自"Python为什么"系列,请查看全部文章 关于 Python 中的pass语句,它似乎很简单(只有 4 个字母),即使是没有任何编程经验的初学者也能很快地掌握它的用法. ...

  3. 搭建vue项目的步骤

    新建vue脚手架 vue-element-cms步骤: 1. vue create ……………(文件名)---这里取为vue-element-cms 2. 命令行工具进入这个文件夹,安装路由依赖包 n ...

  4. 点format方式输出星号字典的值是键

    dic = {'a':123,'b':456} print("{0}:{1}".format(*dic)) a:b 2020-05-08

  5. 遍历多个 txt 文件进行获取值

    import random def load_config(path): with open(path,'r') as tou: return [line for line in tou.readli ...

  6. PHP mysqli_refresh() 函数

    定义和用法 mysqli_refresh() 函数刷新表或缓存,或者重置复制服务器信息.高佣联盟 www.cgewang.com 语法 mysqli_refresh(connection,option ...

  7. PHP simplexml_load_string() 函数

    实例 转换形式良好的 XML 字符串为 SimpleXMLElement 对象,然后输出对象的键和元素: <?php$note=<<<XML<note>高佣联盟 w ...

  8. 牛客练习赛64 如果我让你查回文你还爱我吗 线段树 树状数组 manacher 计数 区间本质不同回文串个数

    LINK:如果我让你查回文你还爱我吗 了解到了这个模板题. 果然我不会写2333... 考试的时候想到了一个非常辣鸡的 线段树合并+莫队的做法 过不了不再赘述. 当然也想到了manacher不过不太会 ...

  9. 5.20 省选模拟赛 求和 组合数的性质 EGF CRT

    LINK:求和 绝妙的一道题目.没做绝对亏了. 对于第一个subtask 考虑直接递推出组合数. 对于第二个subtask 考虑EGF 设两个EGF 都只含偶数项指标且系数为1的那种 一个到n一个到m ...

  10. luogu P1452 [USACO03FALL]Beauty Contest G /【模板】旋转卡壳

    LINK:旋转卡壳 如题 是一道模板题. 容易想到n^2暴力 当然也能随机化选点 (还真有人过了 考虑旋转卡壳 其实就是对于某个点来说找到其最远的点. 在找的过程中需要借助一下个点的帮助 利用当前点到 ...