Event Hub for CloudEvent

概述

Event Hub 是一个简单的 HTTP 服务器,用于接收 CloudEvents 格式的事件,并将它们发送到 Redis Stream。

CloudEvent 格式

CloudEvents 是一种以云为中心的事件标准格式,遵循 CloudEvents 规范

必要字段

一个标准的 CloudEvent JSON 格式至少包含以下字段:

{
  "id": "1234-5678-9101",
  "source": "com.example.service",
  "type": "com.example.event.type",
  "specversion": "1.0",
  "data": {
    "key1": "value1",
    "key2": "value2"
  }
}
字段 描述 类型 必须
id 事件的唯一标识符 String
source 事件发出的来源 URI
type 事件类型 String
specversion CloudEvents 规范版本 String
data 事件数据 Object
datacontenttype 数据内容类型 String
subject 事件主题 String
time 事件发生时间 (ISO 8601) Timestamp

使用方法

发送单个事件

curl -X POST http://localhost:8080/ \
  -H "Content-Type: application/json" \
  -d '{
    "id": "1234-5678-9101",
    "source": "com.example.service",
    "type": "com.example.event.type",
    "specversion": "1.0",
    "time": "2023-04-21T12:00:00Z",
    "data": {
      "message": "Hello, World!",
      "priority": "high"
    }
  }'

发送多个事件(数组格式)

curl -X POST http://localhost:8080/ \
  -H "Content-Type: application/json" \
  -d '[
    {
      "id": "event-001",
      "source": "com.example.service",
      "type": "com.example.event.type",
      "specversion": "1.0",
      "data": {
        "message": "First event"
      }
    },
    {
      "id": "event-002",
      "source": "com.example.service",
      "type": "com.example.event.type",
      "specversion": "1.0",
      "data": {
        "message": "Second event"
      }
    }
  ]'

使用文件发送事件

创建一个 event.json 文件,然后使用以下命令发送:

curl -X POST http://localhost:8080/ \
  -H "Content-Type: application/json" \
  -d @event.json

自定义扩展属性

Event Hub 支持以下自定义扩展属性:

示例:

{
  "id": "1234-5678-9101",
  "source": "com.example.service",
  "type": "com.example.event.type",
  "specversion": "1.0",
  "data": {
    "message": "Hello, World!"
  },
  "eventhub.redis_stream": "custom_stream_name"
}

运行服务

# 默认配置
./eventhub

# 自定义配置
./eventhub --redis-addr=redis:6379 --listen-addr=:9000