Spring Boot Admin實(shí)現(xiàn)服務(wù)健康預(yù)警功能
Over View
上一篇文章主要介紹了Spring Boot Admin的概況以及我們?nèi)绾卧谙到y(tǒng)中引入和使用Spring Boot Admin,以此來幫助我們更加了解自己的系統(tǒng),做到能快速發(fā)現(xiàn)、排查問題。本篇文章將用代碼演示Spring Boot Admin的消息通知功能,并利用這個(gè)開箱即用的特性來個(gè)性化我們的需求,優(yōu)化我們在服務(wù)治理方面的工作效率。
Spring Boot Admin內(nèi)置了多種開箱即用的系統(tǒng)通知渠道,包括郵件、Slack、Telegram、Hipchat等多種社交媒體的通知渠道。但是考慮到它所支持的大都是一些國外的主流社交媒體,在國內(nèi)的本地化可能并不是那么的友好。不過沒關(guān)系Spring Boot Admin也提供了通用的接口,使得用戶可以基于他所提供的接口來自定義通知方式。下面使用Spring Boot Admin的通知功能來實(shí)現(xiàn)基于郵件和國內(nèi)辦公軟件“飛書”的服務(wù)健康預(yù)警。
郵件預(yù)警
依賴引入
在Spring Boot Admin的服務(wù)端項(xiàng)目中引入郵件相關(guān)依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
添加配置
添加Spring Mail相關(guān)配置,我們配置好我們郵箱的Smtp服務(wù)器相關(guān)信息
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)中相關(guān)的郵件配置,以下是SBA官方提供的郵件相關(guān)參數(shù)
| 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=目標(biāo)郵箱
配置中的ignore-changes參數(shù)表示服務(wù)從一個(gè)狀態(tài)變成其他狀態(tài)時(shí)發(fā)出預(yù)警,例如:"UNKNOWN:UP" 表示服務(wù)從未知狀態(tài)變成UP時(shí),發(fā)出通知。當(dāng)其值是""時(shí),表示任何狀態(tài)變更都會(huì)發(fā)出預(yù)警。若想指定其他參數(shù),參考上面的參數(shù)表。
完成上述操作后,重啟Spring Boot Admin服務(wù)端,當(dāng)客戶端服務(wù)注冊進(jìn)來并且狀態(tài)變?yōu)閁P時(shí),我們可以收到一封郵件:

添加郵件模版
Spring Boot admin發(fā)送的郵件可以自定義模板樣式,我們使用thymeleaf語法編寫郵件模板,示例模板代碼可參考本文在Github的代碼示例倉庫,編寫完模板文件之后,將文件放入項(xiàng)目src/main/resources/templates中,并且在配置文件中增加指定模板文件的地址:
spring.boot.admin.notify.mail.template=classpath:/templates/status-changed.html
重啟Spring Boot Admin服務(wù)端,當(dāng)客戶端服務(wù)注冊進(jìn)來并且狀態(tài)變?yōu)閁P時(shí),我們可以收到一封郵件,如下是我們對(duì)郵件進(jìn)行本地化之后的樣式:

飛書預(yù)警
由于Spring Boot Admin內(nèi)置的通知渠道都是國外的社交媒體,不過它也提供了自定義通知渠道的接口,所以我們很容易就可以自定義通知渠道,下面演示集成辦公軟件飛書的通知。
獲取通知地址
飛書中提供了聊天機(jī)器人,我們只需調(diào)用機(jī)器人的WebHook就可以實(shí)現(xiàn)詳細(xì)的推送(企業(yè)微信,釘釘也具有類似功能)。

自定義通知渠道
Spring Boot Admin中提供了一個(gè)AbstractStatusChangeNotifier抽象類,我們可以通過繼承它來自定義通知渠道
public class FlyBookNotifier extends AbstractStatusChangeNotifier {
private static final String DEFAULT_MESSAGE = "#{instance.registration.name} (#{instance.id}) 狀態(tài)發(fā)生轉(zhuǎn)變 #{lastStatus} ➡️ #{instance.statusInfo.status} " +
"\n" +
"\n 實(shí)例詳情:#{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);
}
}
上面代碼是一個(gè)示例,用戶可以根據(jù)自己的需求來自定義消息體的格式和內(nèi)容。
隨后我們在Spring中創(chuàng)建該通知類的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());
}
}
最后我們在項(xiàng)目的配置文件中添加我們飛書渠道的配置信息
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服務(wù)端,當(dāng)客戶端服務(wù)注冊進(jìn)來并且狀態(tài)變?yōu)閁P時(shí),我們可以在飛書端收到Spring Boot Admin自動(dòng)推過來的預(yù)警信息:

至此,我們的自定義消息渠道就已經(jīng)完成。通過繼承AbstractStatusChangeNotifier抽象類,我們可以很輕易的自定義自己想要實(shí)現(xiàn)的推送渠道(設(shè)計(jì)模式:模板方法模式)。
總結(jié)
本文主要介紹了Spring Boot Admin中所提供的多種消息預(yù)警推送渠道,并且我們可以通過自定義消息預(yù)警渠道來滿足我們自身的需求,整個(gè)過程并不需要耗費(fèi)太多的人力和時(shí)間成本。我們用了兩個(gè)示例來演示如何實(shí)現(xiàn)Spring Boot Admin的消息預(yù)警功能,分別是郵件預(yù)警和自定義的飛書預(yù)警。
本文的示例代碼
SBA-client:https://github.com/cg837718548/sba-client-demo.git
SBA-server:https://github.com/cg837718548/sba-server-demo.git
到此這篇關(guān)于Spring Boot Admin實(shí)現(xiàn)服務(wù)健康預(yù)警功能的文章就介紹到這了,更多相關(guān)spring boot 健康預(yù)警內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Spring中Quartz調(diào)度器詳解及實(shí)例
這篇文章主要介紹了Java Spring中Quartz調(diào)度器詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
SpringBoot項(xiàng)目部署到服務(wù)器上的方法(Jar包)
這篇文章主要介紹了SpringBoot項(xiàng)目部署到服務(wù)器上的方法(Jar包),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Mybatis實(shí)現(xiàn)關(guān)聯(lián)關(guān)系映射的方法示例
本文主要介紹了Mybatis實(shí)現(xiàn)關(guān)聯(lián)關(guān)系映射的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Java基于NIO實(shí)現(xiàn)群聊系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java基于NIO實(shí)現(xiàn)群聊系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
IDEA2020.1使用LeetCode插件運(yùn)行并調(diào)試本地樣例的方法詳解
這篇文章主要介紹了IDEA2020.1使用LeetCode插件運(yùn)行并調(diào)試本地樣例的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-09-09
struts2數(shù)據(jù)處理_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Struts2框架框架使用OGNL語言和值棧技術(shù)實(shí)現(xiàn)數(shù)據(jù)的流轉(zhuǎn)處理。下面通過本文給大家分享struts2數(shù)據(jù)處理的相關(guān)知識(shí),感興趣的朋友參考下吧2017-09-09
mybatis攔截器無法注入spring bean的問題解決
本文主要介紹了mybatis攔截器無法注入spring bean的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02

