Event Hub 是一个简单的 HTTP 服务器,用于接收 CloudEvents 格式的事件,并将它们发送到 Redis Stream。
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 支持以下自定义扩展属性:
eventhub.redis_stream - 用于指定 Redis Stream 名称,默认为 “eventhub”示例:
{
"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