DataEase v2 Webhook 功能的使用和实践


飞致云 发布于 2025-01-22 / 177 阅读 / 0 评论 /
1 Webhook 功能介绍 Webhook‌ 是一种事件驱动的轻量级通信机制,通过 HTTP 在应用之间自动发送数据。Webhook 由特定事件触发,可以自动实现应用编程接口(API)之间的通信,即当某个事件发生时,系统会自动向指定的 URL 发送数据。与传统的 API 调用方式不同,API 采用

1 Webhook 功能介绍

Webhook‌ 是一种事件驱动的轻量级通信机制,通过 HTTP 在应用之间自动发送数据。Webhook 由特定事件触发,可以自动实现应用编程接口(API)之间的通信,即当某个事件发生时,系统会自动向指定的 URL 发送数据。与传统的 API 调用方式不同,API 采用的是“拉”数据的方式,即客户端主动向服务器请求数据;而 Webhook 则是“推”数据的方式,服务器在事件发生时主动向客户端推送数据‌,Webhook 是组织级别的内容,以便于统一管理和扩展消息推送,在 DataEase 中主要用于图表的阈值告警推送;

注:目前 DataEase 的 Webhook 功能只能对接第三方系统,无法对接企业微信、钉钉、飞书的 Webhook 机器人;

2 DataEase Webhook 的请求参数说明

请求类型 :POST 

URL :第三方系统 URL

JSON 请求格式:

title :告警标题

content :告警内容

messageId :消息 ID

注:JSON 请求格式为固定格式,不能改动;

示例:

curl -X POST http://192.168.8.38:8080/testWebhook \
-H "Content-Type: application/json" \
-d '{"title":"Test Title","content":"This is a test content.","messageId":"12345"}'

3 DataEase Webhook 的使用

1.DataEase 页面配置如下:

2.图表开启阈值告警功能并配置 Webhook ;

3.第三方系统需要实现接收 Webhook 对应的方法

 java 代码示例:

package com.dai.demo.controller;
 
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import org.json.JSONObject;
import java.io.IOException;
import java.io.OutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
 
public class TestWebhook {
 
 
    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new java.net.InetSocketAddress(8080), 0);
        server.createContext("/testWebhook", new TestWebhookHandler());
        server.setExecutor(null); // 创建一个默认的执行器
        server.start();
        System.out.println("服务器已启动,端口号:8080");
    }
 
    static class TestWebhookHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            try {
                if ("POST".equals(exchange.getRequestMethod())) {
                    Map<String, String> body = readRequestBody(exchange);
 
                    String title = body.get("title");
                    String content = body.get("content");
                    String messageId = body.get("messageId");
                    System.out.println("标题: " + title);
                    System.out.println("内容: " + content);
                    System.out.println("消息 ID: " + messageId);
 
                    sendResponse(exchange, "OK", 200);
                } else {
                    sendResponse(exchange, "方法不允许", 405);
                }
            } catch (Exception e) {
                e.printStackTrace();
                sendResponse(exchange, "服务器内部错误", 500);
            }
        }
    }
 
 
    private static Map<String, String> readRequestBody(HttpExchange exchange) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(exchange.getRequestBody()));
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        String json = jsonBuilder.toString();
        return JsonUtil.fromJsonString(json);
    }
 
    private static void sendResponse(HttpExchange exchange, String response, int responseCode) throws IOException {
        exchange.sendResponseHeaders(responseCode, response.getBytes().length);
        try (OutputStream os = exchange.getResponseBody()) {
            os.write(response.getBytes());
            os.flush();
        }
    }
 
}
 
 
class JsonUtil {
 
    public static Map<String, String> fromJsonString(String json) {
        JSONObject jsonObject = new JSONObject(json);
        Map<String, String> map = new HashMap<>();
        for (String key : jsonObject.keySet()) {
            map.put(key, jsonObject.getString(key));
        }
        return map;
    }
}

pom.xml 添加对应的依赖

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20210307</version> <!-- 使用合适的版本 -->
</dependency>



是否对你有帮助?