webhook-java实现反向API接口

webhook是什么?

webhook是一种web回调或者http的push API,是向APP或者其他应用提供实时信息的一种方式。Webhook在数据产生时立即发送数据,也就是你能实时收到数据。这一种不同于典型的API,需要用了实时性需要足够快的轮询。这无论是对生产还是对消费者都是高效的。

Webhook有时也被称为反向API,因为他提供了API规则,你需要设计要使用的API。Webhook将向你的应用发起http请求,典型的是post请求,应用程序由请求驱动。

Webhook模式的主要优点是,当你的应用在等待变化时,无需周期性地调用APIs。

为什么是webhook

实际生产应用中采用了prometheus + grafana来部署监控。
在prometheus官网上介绍监控系统通过alertmanager来进行告警配置。
其中alertmanager可以配置webhook来反向代理 公司内部 告警中心。

如何用java实现webhook

这里采用的是springboot构建的一个简单的http接口,如下图是给ceph集群部署监控后配置告警的架构概览图示:

prometheus会收集ceph集群的各种指标,并将告警信息发送给alert manager进行管理;
alert manager配置了告警的目标地址——webhook http接口;
alert manager整理告警信息后,通过http请求调用webhook的接口,webhook处理告警信息后调用告警中心的http接口;
接下来的告警分发,就是告警中心要做的事了。

以上就是webhook承担的整个功能,明白流程后一切都好办了!
在这里插入图片描述

项目结构如下图:
项目结构
关键代码如下:(关于Http请求部分,可以谷歌一下通过retrofit来实现)

URIBuilder uri = null;
    try {
      uri = new URIBuilder(url);
    } catch (URISyntaxException e) {
      logger.error("", e);
    }

    uri.setParameters(qparams);

    try (
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault()
        ) {
      HttpPost httpPost = new HttpPost(uri.build());
      httpPost.addHeader("Content-type", "application/json;charset=utf-8");

      StringEntity alertStringEntity = new StringEntity(mapper.writeValueAsString(alertCenterStructs), "UTF-8");
      httpPost.setEntity(alertStringEntity);

      try (
          CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpPost)
          ) {
        HttpEntity entity = httpResponse.getEntity();
        System.out.println(entity.getContent());
      } catch (Exception e) {
        logger.error("send msg to alert center failed", e);
      }
    } catch (Exception e) {
      logger.error("send msg to alert center failed", e);
    }

参考文章:
通过git的webhook实现通过钉钉进行代码及时review(Java版本)
运行 Spring Boot 应用的 3 种方式
Spring boot 发布jar包 ,传参数的3种方式
SpringBoot之Spring@Value属性注入使用详解

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
prometheus-webhook-dingtalk 是一个用于将 Prometheus 监控告警消息发送到钉钉的 Webhook 工具。它可以帮助你将 Prometheus 监控告警通过钉钉机器人发送到指定的群组或用户。 你可以通过以下步骤来配置和使用 prometheus-webhook-dingtalk: 1. 安装 prometheus-webhook-dingtalk:你可以使用 Go 工具链来安装 prometheus-webhook-dingtalk,运行以下命令: ``` go get github.com/timonwong/prometheus-webhook-dingtalk/cmd/dingtalk ``` 2. 创建钉钉机器人:在钉钉中创建一个自定义机器人,并获取到它的 Webhook 地址,用于将告警消息发送到指定的群组或用户。 3. 创建配置文件:在 prometheus-webhook-dingtalk 的配置文件中,你需要指定钉钉机器人的 Webhook 地址以及其他相关参数。你可以创建一个名为 config.yml 的配置文件,并将以下示例内容填入: ```yaml listen: 0.0.0.0:8060 dingtalk: webhook: https://oapi.dingtalk.com/robot/send?access_token=your_webhook_token ``` 4. 启动 prometheus-webhook-dingtalk:运行以下命令来启动 prometheus-webhook-dingtalk: ``` dingtalk -config.file=config.yml ``` 5. 配置 Prometheus:在 Prometheus 的配置文件中,添加以下内容来指定告警消息的接收端: ```yaml receivers: - name: 'dingtalk' webhook_configs: - url: 'http://prometheus-webhook-dingtalk:8060/dingtalk/webhook' ``` 6. 重新启动 Prometheus:确保 Prometheus 已经重新加载了配置文件,并重启 Prometheus 服务。 现在,当 Prometheus 监控触发告警时,prometheus-webhook-dingtalk 将会将告警消息发送到钉钉机器人的 Webhook 地址,从而通知到指定的群组或用户。 请注意,以上步骤仅为一般示例,实际操作可能会因环境和需求而有所不

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值