快速上手 SpringBoot 飛書消息推送
“SpringBoot 飛書推送組件 Getting Start。”
上一篇介紹 基于 Prometheus + Grafana + Alertmanager + 飛書通知的智能監(jiān)控平臺 中提到我們有時候會把一些信息推送到工作交流平臺----飛書(或釘釘), 本文專題介紹一下飛書推送組件 ----
feishu-notification-spring-boot-starter的使用.
0. 前置條件
jdk 1.8
Spring boot 2.x
Spring web 版本 >= 5.2
我們推薦JavaFamily 所有
2.3.2-xxx版本的組件工作在SpringBoot 2.3.2.RELEASE為最佳!
1. 引入依賴
Maven Central Release
<dependency>
<groupId>club.javafamily</groupId>
<artifactId>feishu-notification-spring-boot-starter</artifactId>
<version>2.3.2-beta.8</version>
</dependency>2. 配置
2.1 飛書通知配置
創(chuàng)建你自己的飛書 WebHook 機器人, 在 application.yml 中配置飛書通知的 webhook 地址
javafamily:
notify:
feishu:
hook-url: https://open.feishu.cn/open-apis/bot/v2/hook/09973b31-0c1a-4924-b900-6173bb429644
enabled: true # 是否開啟通知, 用于不同環(huán)境下的區(qū)分(開發(fā), 測試, 生產(chǎn)), 默認為 true所有的源碼和配置都可以在 https://github.com/JavaFamilyClub/notification-manager/tree/main/feishu-notification-spring-boot-starter 找到.

2.2 抑制策略
當(dāng)我們需要對通知進行抑制時(如: 通過飛書通知一些接口異常、服務(wù)宕機等信息, 有時候并不需要一直推送通知消息), 此時, 就可以通過抑制策略進行通知消息的抑制!
javafamily:
notify:
feishu:
hook-url: https://open.feishu.cn/open-apis/bot/v2/hook/31a65e6b-0dab-491c-8de9-df3d16c19050
inhibit:
enabled: on # 默認為 off
ttl: 1h # 代表同一個消息, 1h 只推送一次通過指定
inhibit屬性進行抑制配置, 目前支持的屬性有:
enabled: 是否開啟抑制
ttl: 抑制時效(同樣的通知多久發(fā)送一次)通知抑制是通過 javafamily-cache 組件 提供組件服務(wù)與配置, 因此,
feishu-notification-spring-boot-starter同樣支持JavaFamilyClub/javafamily-cache組件的全部配置. 如:
javafamily:notify:feishu:hook-url: https://open.feishu.cn/open-apis/bot/v2/hook/31a65e6b-0dab-491c-8de9-df3d16c19050inhibit:enabled: on # 默認為 offttl: 1h # 代表同一個消息, 1h 只推送一次cache:type: caffeine # rediskey-prefix: demo- # 緩存 key 前綴time-to-live: 20s # 緩存 expire 時間caffeine: # caffeine 緩存相關(guān)配置max-size: 500weak-keys: onsoft-values: onrecord-stats: on
需要注意,
cache.time-to-live與inhibit.ttl如果都配置, 則inhibit.ttl優(yōu)先級更高(生效).更多配置請查看 JavaFamilyClub/javafamily-cache (地址: https://github.com/JavaFamilyClub/javafamily-cache)


2.3 restTemplate 配置
發(fā)送 webhook 請求底層是通過封裝的
resttemplate進行請求, 而restTemplate是通過 javafamily-resttemplate-starter 提供組件服務(wù)與配置, 因此,feishu-notification-spring-boot-starter天生支持javafamily-resttemplate-starter組件的全部配置.如: 配置代理(支持 http 及 socks 代理)
javafamily:
notify:
feishu:
hook-url: http://open.feishu.cn/open-apis/bot/v2/hook/09973b31-0c1a-4924-b900-6173bb429644
http:
proxy:
type: http # type: socks
host: 192.168.56.27
port: 10080更多
restTemplate的配置請參考: javafamily-resttemplate-starter (地址: https://github.com/JavaFamilyClub/javafamily-core/tree/main/javafamily-resttemplate-starter))


3. 注入 FeiShuNotifyHandler
@SpringBootTest
public class FeiShuNotifyTests {
@Autowired
private FeiShuNotifyHandler feiShuNotifyHandler;4. 創(chuàng)建 Request, 發(fā)送通知
Text 通知
@Test
void testNotifyText() {
final String response = feiShuNotifyHandler.notify(
FeiShuTextNotifyRequest.of("這是一個測試數(shù)據(jù)!"));
log.info(response);
}
Post 通知
@Test
void testNotifyPost() {
final FeiShuPostNotifyRequest request = FeiShuPostNotifyRequest.of(
"項目更新通知(測試)",
new BaseTextTagContentItem("(測試)項目有更新: "),
new LinkTagContentItem("請查看",
"https://github.com/orgs/JavaFamilyClub/projects/3"));
final String response = feiShuNotifyHandler.notify(request);
log.info(response);
}
Card 通知
@Test
void testNotifyCard() {
String dataTime = "2022-06-05 23:00:00";
int shouldCount = 20, actualCount = 20;
String status = actualCount < shouldCount ? "異常" : "正常";
String content = "數(shù)據(jù)時次: " + dataTime
+ "\n應(yīng)收收據(jù)個數(shù): " + shouldCount
+ "\n實收數(shù)據(jù)個數(shù): " + actualCount
+ "\n監(jiān)控狀態(tài): **" + status + "**";
final FeiShuCardNotifyRequest request
= FeiShuCardNotifyRequest.of("測試xxx數(shù)據(jù)監(jiān)控", content,
"立即前往系統(tǒng)查看 :玫瑰:? ? \uD83D\uDDA5?",
"https://github.com/orgs/JavaFamilyClub/projects/3");
final String response = feiShuNotifyHandler.notify(request);
log.info(response);
}
5. 示例代碼
所有的示例代碼都在 https://github.com/JavaFamilyClub/notification-manager/tree/main/examples
組件使用示例: https://github.com/JavaFamilyClub/notification-manager/tree/main/examples/demo-notification-manager抑制通知示例: https://github.com/JavaFamilyClub/notification-manager/tree/main/examples/demo-notification-manager-inhibit
如果有任何相關(guān)的問題都可以加入 QQ/微信群一起討論, 學(xué)習(xí), 進步. 此外如果有任何對于本公眾號的意見和建議也歡迎大家留言積極批評指正, 最后, 愿你我都能成為更好的自己.
我是帥帥, 一個集帥氣, 幽默與內(nèi)涵, 并且熱愛編程, 擁抱開源, 喜歡烹飪與旅游的暖男, 我們下期再見. 拜了個拜!
老規(guī)矩別忘了哦, 點擊原文鏈接跳轉(zhuǎn)到我們官方的博客平臺哦.
悄悄話
————
每文一句
————
Don't aim for success if you really want it. Just stick to what you love and believe in, and it will come naturally.
少一些功利主義的追求, 多一些不為什么的堅持.
日常求贊
————
你們白漂的力量就是我拖更的史詩級動力, 點贊, 評論, 再看, 贊賞, 看都看到這了, 隨便點一個咯.
關(guān)注加好友
拉你進大佬交流群
————————————————
