SpringBoot 飞书通知处理器

上一篇介绍 基于 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 找到.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E1S3B1Wi-1661769105250)(http://image.openwrite.cn/19081_03F454FBEADF4E4A9DB275A086B612AC)]

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:
  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-liveinhibit.ttl 如果都配置, 则 inhibit.ttl 优先级更高(生效).

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-i3frDRhn-1661769105252)(http://image.openwrite.cn/19081_3B3528F8150F41F79E4B212151FB30DC)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TDoX69yw-1661769105253)(http://image.openwrite.cn/19081_A7D19F5DB68449F2B9C6DEAB957CF837)]

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))

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mAbDI8FN-1661769105253)(http://image.openwrite.cn/19081_C2166A3A25064B25B0C3C50CB2C3C571)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ouzSB30A-1661769105254)(http://image.openwrite.cn/19081_A02A8FBA152746F1AA2F0C83E930BEF5)]

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);
   }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VFQqU6AA-1661769105255)(http://image.openwrite.cn/19081_EA1942C061854E56890EF7493643DFE4)]

  • 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);
   }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wR8LcY6w-1661769105256)(http://image.openwrite.cn/19081_300678B818634C11BEB38E90651E838B)]

  • 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);
   }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nPEdDcgy-1661769105256)(http://image.openwrite.cn/19081_B69F0A8BABB84C89B4540E6E98521D3D)]

5. 示例代码

所有的示例代码都在 https://github.com/JavaFamilyClub/notification-manager/tree/main/examples

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值