springboot admin监控告警

概述

Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含:应用状态、内存、线程、堆栈等等,比较全面的监控了Spring Boot应用的整个生命周期;如果Spring Boot应用集群非常大,每个应用都需要调用不同的接口来查看监控信息,国外一个大神开源了一个平台Spring Boot Admin可以用来监控springboot应用集群;
如果结合注册中心可自动从注册中心获取springboot应用信息并自动监控,其中一个特性比较有用,就是服务上线下线通知;

springboot admin消息通知

Spring Boot Admin 本身提供监控告警功能,但是默认只提供了Hipchat、Slack等国外流行的通讯软件的集成;默认也支持邮件通知,本示例主要考虑到国内很多公司使用钉钉进行办公交流,故扩展了钉钉机器人通知;

邮件通知

配置如下,可自行修改

spring.mail.host=smtp.163.com
spring.mail.username=XXXX@163.com
spring.mail.password=88888898989898
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# 发送给谁
spring.boot.admin.notify.mail.to=XXXXX@gmail.com
# 是谁发送出去的
spring.boot.admin.notify.mail.from=XXX@163.com
spring.boot.admin.notify.mail.enabled=false

钉钉机器人开通方法

  1. 新建一个群聊;
  2. 添加钉钉机器人;

这里写图片描述
这里写图片描述

  1. 将钉钉机器人webhook-token配置到项目中,当有服务上下线的时候即会有消息通知;
    PS:关于钉钉机器人更详细的文档请参见:
    https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.xSiICS&treeId=257&articleId=105735&docType=1#s0

注意:admin的客户端需要引入springboot actuator组件来暴露相关统计端点;

消息格式的自定义

默认的消息格式:

ADMIN-SERVER (f21947a7) status changed from DOWN to OFFLINE  http://8341a1abad80:8088/health

可自定义,默认的消息对象中包含的信息:

{
  "from": {
    "status": "UNKNOWN",
    "timestamp": 1528357849054,
    "details": {}
  },
  "to": {
    "status": "OFFLINE",
    "timestamp": 1528357867912,
    "details": {
      "exception": "org.springframework.web.client.ResourceAccessException",
      "message": "I/O error on GET request for \"http://10.42.42.210:80/health\": Connect to 10.42.42.210:80 [/10.42.42.210] failed: connect timed out; nested exception is org.apache.http.conn.ConnectTimeoutException: Connect to 10.42.42.210:80 [/10.42.42.210] failed: connect timed out"
    }
  },
  "application": {
    "id": "291f245e",
    "name": "SPARTA-F:1.0.0",
    "managementUrl": "http://10.42.42.210:80/",
    "healthUrl": "http://10.42.42.210:80/health",
    "serviceUrl": "http://10.42.42.210:80/",
    "statusInfo": {
      "status": "OFFLINE",
      "timestamp": 1528357867912,
      "details": {
        "exception": "org.springframework.web.client.ResourceAccessException",
        "message": "I/O error on GET request for \"http://10.42.42.210:80/health\": Connect to 10.42.42.210:80 [/10.42.42.210] failed: connect timed out; nested exception is org.apache.http.conn.ConnectTimeoutException: Connect to 10.42.42.210:80 [/10.42.42.210] failed: connect timed out"
      }
    },
    "source": "discovery",
    "metadata": {},
    "info": {
      "timestamp": 0,
      "values": {}
    }
  },
  "timestamp": 1528357867912,
  "type": "STATUS_CHANGE"
}

示例代码

github:https://github.com/luoyoubao/springboot-admin-demo
码云:https://gitee.com/luoyoub/springboot-admin-demo

猜你喜欢

转载自blog.csdn.net/luoyoub/article/details/80613590