Spring Boot Admin实现服务健康预警
Over View
上一篇文章主要介绍了Spring Boot Admin的概况以及我们如何在系统中引入和使用Spring Boot Admin,以此来帮助我们更加了解自己的系统,做到能快速发现、排查问题。本篇文章将用代码演示Spring Boot Admin的消息通知功能,并利用这个开箱即用的特性来个性化我们的需求,优化我们在服务治理方面的工作效率。
Spring Boot Admin内置了多种开箱即用的系统通知渠道,包括邮件、Slack、Telegram、Hipchat等多种社交媒体的通知渠道。但是考虑到它所支持的大都是一些国外的主流社交媒体,在国内的本地化可能并不是那么的友好。不过没关系Spring Boot Admin也提供了通用的接口,使得用户可以基于他所提供的接口来自定义通知方式。下面使用Spring Boot Admin的通知功能来实现基于邮件和国内办公软件“飞书”的服务健康预警。
邮件预警
依赖引入
在Spring Boot Admin的服务端项目中引入邮件相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
添加配置
添加Spring Mail相关配置,我们配置好我们邮箱的Smtp服务器相关信息
spring.mail.host=your email smtp server
spring.mail.password=your password
spring.mail.port=your email smtp server port
spring.mail.test-connection=true
spring.mail.username=837718548@qq.com
添加Spring Boot Admin(SBA)中相关的邮件配置,以下是SBA官方提供的邮件相关参数
Property name | Description | Default value |
---|---|---|
spring.boot.admin.notify.mail.enabled | Enable mail notifications | true |
spring.boot.admin.notify.mail.ignore-changes | Comma-delimited list of status changes to be ignored. Format: ":". Wildcards allowed. | "UNKNOWN:UP" |
spring.boot.admin.notify.mail.template | Resource path to the Thymeleaf template used for rendering. | "classpath:/META-INF/spring-boot-admin-server/mail/status-changed.html" |
spring.boot.admin.notify.mail.to | Comma-delimited list of mail recipients | "root@localhost" |
spring.boot.admin.notify.mail.cc | Comma-delimited list of carbon-copy recipients | |
spring.boot.admin.notify.mail.from | Mail sender | "Spring Boot Admin noreply@localhost" |
spring.boot.admin.notify.mail.additional-properties | Additional properties which can be accessed from the template |
我们这里使用如下配置
spring.boot.admin.notify.mail.from=837718548@qq.com
spring.boot.admin.notify.mail.ignore-changes=""
spring.boot.admin.notify.mail.to=目标邮箱
配置中的ignore-changes参数表示服务从一个状态变成其他状态时发出预警,例如:"UNKNOWN:UP" 表示服务从未知状态变成UP时,发出通知。当其值是""时,表示任何状态变更都会发出预警。若想指定其他参数,参考上面的参数表。
完成上述操作后,重启Spring Boot Admin服务端,当客户端服务注册进来并且状态变为UP时,我们可以收到一封邮件:
添加邮件模版
Spring Boot admin发送的邮件可以自定义模板样式,我们使用thymeleaf语法编写邮件模板,示例模板代码可参考本文在Github的代码示例仓库,编写完模板文件之后,将文件放入项目src/main/resources/templates中,并且在配置文件中增加指定模板文件的地址:
spring.boot.admin.notify.mail.template=classpath:/templates/status-changed.html
重启Spring Boot Admin服务端,当客户端服务注册进来并且状态变为UP时,我们可以收到一封邮件,如下是我们对邮件进行本地化之后的样式:
飞书预警
由于Spring Boot Admin内置的通知渠道都是国外的社交媒体,不过它也提供了自定义通知渠道的接口,所以我们很容易就可以自定义通知渠道,下面演示集成办公软件飞书的通知。
获取通知地址
飞书中提供了聊天机器人,我们只需调用机器人的WebHook就可以实现详细的推送(企业微信,钉钉也具有类似功能)。
自定义通知渠道
Spring Boot Admin中提供了一个AbstractStatusChangeNotifier抽象类,我们可以通过继承它来自定义通知渠道
public class FlyBookNotifier extends AbstractStatusChangeNotifier {
private static final String DEFAULT_MESSAGE = "#{instance.registration.name} (#{instance.id}) 状态发生转变 #{lastStatus} ➡️ #{instance.statusInfo.status} " +
"\n" +
"\n 实例详情:#{instanceEndpoint}";
private final SpelExpressionParser parser = new SpelExpressionParser();
private RestTemplate restTemplate;
private URI webhookUrl;
private Expression message;
public FlyBookNotifier(InstanceRepository repository, RestTemplate restTemplate) {
super(repository);
this.restTemplate = restTemplate;
this.message = parser.parseExpression(DEFAULT_MESSAGE, ParserContext.TEMPLATE_EXPRESSION);
}
@Override
protected Mono<Void> doNotify( InstanceEvent event, Instance instance) {
if (webhookUrl == null) {
return Mono.error(new IllegalStateException("'webhookUrl' must not be null."));
}
return Mono
.fromRunnable(() -> restTemplate.postForEntity(webhookUrl, createMessage(event, instance), Void.class));
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
protected Object createMessage(InstanceEvent event, Instance instance) {
Map<String, Object> messageJson = new HashMap<>();
messageJson.put("title", "
Spring Boot Admin实现服务健康预警的更多相关文章
- 物联网架构成长之路(30)-Spring Boot Admin微服务WebUI监控
0. 前言 一个完整的微服务解决方案包含了许多微服务,基于我们需要观察各个微服务的运行状态,因此Spring Boot 生态提供了Spring Boot Admin 这个组件来实现微服务管理WEB U ...
- Spring Cloud第十三篇 | Spring Boot Admin服务监控
本文是Spring Cloud专栏的第十三篇文章,了解前十二篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...
- SpringCloud微服务实战——搭建企业级开发框架(四十四):【微服务监控告警实现方式一】使用Actuator + Spring Boot Admin实现简单的微服务监控告警系统
业务系统正常运行的稳定性十分重要,作为SpringBoot的四大核心之一,Actuator让你时刻探知SpringBoot服务运行状态信息,是保障系统正常运行必不可少的组件. spring-b ...
- 基于spring boot admin 做监控的一些问题记录
问题一 各个健康节点权限问题 解决方式 加入权限模块 <dependency> <groupId>org.springframework.boot</groupId> ...
- Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用
Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用 1. 引言 在上一篇文章<Spring Boot (九): 微服务应用监控 Spring ...
- spring boot admin
这里记录一个spring cloud的模板,有的模块spring cloud eureka + spring boot admin + spring cloud zuul + 一个普通spring c ...
- Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知
1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...
- spring boot admin项目的集成和开发
Spring Boot Admin是一个Github上的一个开源项目,它在Spring Boot Actuator的基础上提供简洁的可视化WEB UI,是用来管理 Spring Boot 应用程序的一 ...
- Springboot监控之二:Spring Boot Admin对Springboot服务进行监控
概述 Spring Boot 监控核心是 spring-boot-starter-actuator 依赖,增加依赖后, Spring Boot 会默认配置一些通用的监控,比如 jvm 监控.类加载.健 ...
随机推荐
- C#开发BIMFACE系列31 服务端API之模型对比2:获取模型对比状态
系列目录 [已更新最新开发文章,点击查看详细] 在上一篇<C#开发BIMFACE系列30 服务端API之模型对比1:发起模型对比>中发起了2个模型对比,由于模型对比是在BIMFAC ...
- Spring5:Java Config
@Configuration @Bean @ComponentScan @ImportResource 使用Java的方式配置spring,完全不使用spring配置文件,交给java来做! 两个注解 ...
- 基于udp协议的套接字通信
服务端: import socket server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) server.bind(('127.0.0.1',8 ...
- Hbase的安装与基本操作
简介: 1安装 HBase 本节介绍HBase的安装方法,包括下载安装文件.配置环境变量.添加用户权限等. 1.1 下载安装文件 HBase是Hadoop生态系统中的一个组件,但是,Hado ...
- 敏捷与OKR实践(如何让OKR与敏捷计划共存)
僵化的详细长期计划(根据消耗的预算跟踪进度)正在敏捷组织中迅速成为对过去的褪色怀旧记忆,这由预测和非静态路线图代替.定期在这些可视化文件前聚会,您将能够学习.共享并触发重要的对话,解决依赖性并邀请服务 ...
- /sbin/mount.vboxsf: mounting failed with the error: Protocol error
公司换了新电脑,需要把之前的虚拟机的配置全部备份下来,在移动的过程中挂载共享文件夹时候出现了 /sbin/mount.vboxsf: mounting failed with the error: P ...
- Zabbix3.4安装部署
Zabbix3.4安装部署 一.系统环境 cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 关闭防火墙及selinux sy ...
- Spring5参考指南:组件扫描
文章目录 组件扫描 @Component 元注解和组合注解 组件内部定义Bean元数据 为自动检测组件命名 为自动检测的组件提供作用域 生成候选组件的索引 组件扫描 上一篇文章我们讲到了annotat ...
- 【Linux常见命令】alias命令
alias命令用于查看和设置指令的别名. 用户可利用alias,自定指令的别名. 若仅输入alias,则可列出目前所有的别名设置. alias的效力仅及于该次登入的操作.若要每次登入是即自动设好别名, ...
- 为什么LIKELY和UNLIKELY要用两个叹号
LIKELY和UNLIKELY的一般定义如下: #define LIKELY(x) (__builtin_expect(!!(x),1))#define UNLIKELY(x) (__builtin_ ...