Spring Boot Admin实现服务健康预警功能

 更新时间:2020年05月15日 10:23:53   作者:东溪陈姓少年  
这篇文章主要介绍了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: "<from-status>:<to-status>". 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", "👹警告&👼提醒");
 messageJson.put("text", getText(event, instance));

 HttpHeaders headers = new HttpHeaders();
 headers.setContentType(MediaType.APPLICATION_JSON);
 return new HttpEntity<>(messageJson, headers);
 }


 protected String getText(InstanceEvent event, Instance instance) {
 Map<String, Object> root = new HashMap<>();
 root.put("event", event);
 root.put("instance", instance);
 root.put("instanceEndpoint", instance.getEndpoints().toString());
 root.put("lastStatus", getLastStatus(event.getInstance()));
 StandardEvaluationContext context = new StandardEvaluationContext(root);
 context.addPropertyAccessor(new MapAccessor());
 return message.getValue(context, String.class);
 }


 public URI getWebhookUrl() {
 return webhookUrl;
 }

 public void setWebhookUrl(URI webhookUrl) {
 this.webhookUrl = webhookUrl;
 }

 public String getMessage() {
 return message.getExpressionString();
 }

 public void setMessage(String message) {
 this.message = parser.parseExpression(message, ParserContext.TEMPLATE_EXPRESSION);
 }
}

上面代码是一个示例,用户可以根据自己的需求来自定义消息体的格式和内容。
随后我们在Spring中创建该通知类的bean

@Configuration
public static class NotifierConfiguration {
 @Bean
 @ConditionalOnMissingBean
 @ConfigurationProperties("spring.boot.admin.notify.flybook")
 public FlyBookNotifier flyBookNotifier(InstanceRepository repository) {
 return new FlyBookNotifier(repository, new RestTemplate());
 }
}

最后我们在项目的配置文件中添加我们飞书渠道的配置信息

spring.boot.admin.notify.flybook.ignore-changes=""
spring.boot.admin.notify.flybook.webhook-url=https://open.feishu.cn/open-apis/bot/hook...

完成上述操作后,重启Spring Boot Admin服务端,当客户端服务注册进来并且状态变为UP时,我们可以在飞书端收到Spring Boot Admin自动推过来的预警信息:

至此,我们的自定义消息渠道就已经完成。通过继承AbstractStatusChangeNotifier抽象类,我们可以很轻易的自定义自己想要实现的推送渠道(设计模式:模板方法模式)。

总结

本文主要介绍了Spring Boot Admin中所提供的多种消息预警推送渠道,并且我们可以通过自定义消息预警渠道来满足我们自身的需求,整个过程并不需要耗费太多的人力和时间成本。我们用了两个示例来演示如何实现Spring Boot Admin的消息预警功能,分别是邮件预警和自定义的飞书预警。

本文的示例代码
SBA-client:https://github.com/cg837718548/sba-client-demo.git
SBA-server:https://github.com/cg837718548/sba-server-demo.git

到此这篇关于Spring Boot Admin实现服务健康预警功能的文章就介绍到这了,更多相关spring boot 健康预警内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java Spring中Quartz调度器详解及实例

    Java Spring中Quartz调度器详解及实例

    这篇文章主要介绍了Java Spring中Quartz调度器详解及实例的相关资料,需要的朋友可以参考下
    2017-02-02
  • SpringBoot项目部署到服务器上的方法(Jar包)

    SpringBoot项目部署到服务器上的方法(Jar包)

    这篇文章主要介绍了SpringBoot项目部署到服务器上的方法(Jar包),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • Java数组判断是否越界的示例代码

    Java数组判断是否越界的示例代码

    在Java编程中,避免数组越界是十分重要的,本文介绍了两种常见的判断数组是否越界的方法:一是通过数组的length属性来判断索引是否合法;二是通过捕获ArrayIndexOutOfBoundsException异常来处理越界问题,感兴趣的朋友跟随小编一起看看吧
    2024-09-09
  • Mybatis实现关联关系映射的方法示例

    Mybatis实现关联关系映射的方法示例

    本文主要介绍了Mybatis实现关联关系映射的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • SpringBoot Admin 使用指南(推荐)

    SpringBoot Admin 使用指南(推荐)

    这篇文章主要介绍了SpringBoot Admin 使用指南(推荐),Spring Boot Admin 是一个管理和监控你的 Spring Boot 应用程序的应用程序,非常具有实用价值,需要的朋友可以参考下
    2018-01-01
  • Java实现九九乘法表的小例子

    Java实现九九乘法表的小例子

    九九乘法表一般为三角形,每个数分别和从1到自身的数相乘然后把结果列出来,即要用到两层循环,外层是从1到9for(i=1;i<=9;i++),内层是当前数和从1到自身相乘for(j=1;j<=i;j++)
    2013-09-09
  • Java基于NIO实现群聊系统

    Java基于NIO实现群聊系统

    这篇文章主要为大家详细介绍了Java基于NIO实现群聊系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • IDEA2020.1使用LeetCode插件运行并调试本地样例的方法详解

    IDEA2020.1使用LeetCode插件运行并调试本地样例的方法详解

    这篇文章主要介绍了IDEA2020.1使用LeetCode插件运行并调试本地样例的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2020-09-09
  • struts2数据处理_动力节点Java学院整理

    struts2数据处理_动力节点Java学院整理

    Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理。下面通过本文给大家分享struts2数据处理的相关知识,感兴趣的朋友参考下吧
    2017-09-09
  • mybatis拦截器无法注入spring bean的问题解决

    mybatis拦截器无法注入spring bean的问题解决

    本文主要介绍了mybatis拦截器无法注入spring bean的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-02-02

最新评论