快速上手 SpringBoot 飞书消息推送

JavaFamily

共 5125字,需浏览 11分钟

 · 2022-08-25

SpringBoot 飞书推送组件 Getting Start。


    上一篇介绍 基于 Prometheus + Grafana + Alertmanager + 飞书通知的智能监控平台 中提到我们有时候会把一些信息推送到工作交流平台----飞书(或钉钉), 本文专题介绍一下飞书推送组件 ---- 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 飞书通知配置

创建你自己的飞书 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 # 是否开启通知, 用于不同环境下的区分(开发, 测试, 生产), 默认为 true

所有的源码和配置都可以在 https://github.com/JavaFamilyClub/notification-manager/tree/main/feishu-notification-spring-boot-starter 找到.

2.2 抑制策略

当我们需要对通知进行抑制时(如: 通过飞书通知一些接口异常、服务宕机等信息, 有时候并不需要一直推送通知消息), 此时, 就可以通过抑制策略进行通知消息的抑制!

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: 抑制时效(同样的通知多久发送一次)

通知抑制是通过 javafamily-cache 组件 提供组件服务与配置, 因此, 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-df3d16c19050      inhibit:        enabled: on # 默认为 off        ttl: 1h # 代表同一个消息, 1h 只推送一次
cache: type: caffeine # redis key-prefix: demo- # 缓存 key 前缀 time-to-live: 20s # 缓存 expire 时间 caffeine: # caffeine 缓存相关配置 max-size: 500 weak-keys: on soft-values: on record-stats: on

需要注意, cache.time-to-live 与 inhibit.ttl 如果都配置, 则 inhibit.ttl 优先级更高(生效).

更多配置请查看  JavaFamilyClub/javafamily-cache (地址: https://github.com/JavaFamilyClub/javafamily-cache)

2.3 restTemplate 配置

发送 webhook 请求底层是通过封装的 resttemplate 进行请求, 而 restTemplate 是通过 javafamily-resttemplate-starter 提供组件服务与配置, 因此, 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. 创建 Request, 发送通知

  • Text 通知

   @Test
void testNotifyText() {
final String response = feiShuNotifyHandler.notify(
FeiShuTextNotifyRequest.of("这是一个测试数据!"));

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 = "数据时次: " + dataTime
+ "\n应收收据个数: " + shouldCount
+ "\n实收数据个数: " + actualCount
+ "\n监控状态: **" + status + "**";

final FeiShuCardNotifyRequest request
= FeiShuCardNotifyRequest.of("测试xxx数据监控", content,
"立即前往系统查看 :玫瑰:️ ✅ \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

        如果有任何相关的问题都可以加入 QQ/微信群一起讨论, 学习, 进步. 此外如果有任何对于本公众号的意见和建议也欢迎大家留言积极批评指正, 最后, 愿你我都能成为更好的自己. 


        我是帅帅, 一个集帅气, 幽默与内涵, 并且热爱编程, 拥抱开源, 喜欢烹饪与旅游的暖男, 我们下期再见. 拜了个拜!

        老规矩别忘了哦, 点击原文链接跳转到我们官方的博客平台哦.



悄悄话




每文一句



Don't aim for success if you really want it. Just stick to what you love and believe in, and it will come naturally.

少一些功利主义的追求, 多一些不为什么的坚持.


日常求赞

      你们白漂的力量就是我拖更的史诗级动力, 点赞, 评论, 再看, 赞赏, 看都看到这了, 随便点一个咯.



关注加好友


拉你进大佬交流群





浏览 294
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报