• 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.javafamilygroupId>
       <artifactId>feishu-notification-spring-boot-starterartifactId>
       <version>2.3.2-beta.8version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    所有的源码和配置都可以在 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 只推送一次
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    通过指定 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    需要注意, 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    更多 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;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4. 创建 Request, 发送通知

    • Text 通知
       @Test
       void testNotifyText() {
          final String response = feiShuNotifyHandler.notify(
             FeiShuTextNotifyRequest.of("这是一个测试数据!"));
    
          log.info(response);
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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);
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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);
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

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

    5. 示例代码

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

  • 相关阅读:
    从乘法求导法则到BPTT算法
    体验不尽,进化不止,看视频云技术六大创新
    2024/3/5打卡最长上升子序列**----线性DP,贪心,单调栈
    平面点云,边界提取
    【人工智能】第三部分:ChatGPT的应用场景和挑战
    Mock.js在Vue项目中的使用
    【QT】Ubuntu 搭建 QT 环境(图形化界面安装)
    Python flask项目修改访问ip及端口号无效的解决办法
    低代码:API 开发的启动板
    使用Github Copilot生成单元测试并执行
  • 原文地址:https://blog.csdn.net/DreamLi1314/article/details/126590330