RocketMQ EventBridge 快速入门
RocketMQ EventBridge 需要一个消息服务来存储事件,以及一个运行时来订阅和推送事件。在本例中,我们选择 Apache RocketMQ 作为我们的消息服务,并选择 Apache RocketMQ Connect 作为我们的运行时来订阅和推送事件。当然,您也可以选择其他消息服务,EventBridge 对此没有限制。未来,EventBridge 也计划基于 OpenMessaging Connect API 实现自己的运行时,以便更好地提供事件驱动服务。
系统要求
- 64 位操作系统,推荐使用 Linux/Unix/macOS
- 64 位 JDK 1.8+
部署 Apache RocketMQ
Apache RocketMQ 是一款出色的消息服务,我们选择它作为 EventBus 的默认存储。您可以根据此手册快速部署它:Apache RocketMQ 快速入门
部署 Apache RocketMQ Connect
我们使用 Apache RocketMQ Connect 作为我们的默认运行时,连接到外部上游和下游服务。您可以根据手册完成部署:RocketMQ Connect 快速入门。在部署 Apache RocketMQ Connect 之前,您应该下载以下插件并将它们放在 rocketmq-connect 中 "pluginPaths" 配置参数定义的目录中。
- rocketmq-connect-eventbridge-jar-with-dependencies.jar
- rocketmq-connect-dingtalk-jar-with-dependencies.jar
- connect-cloudevent-transform-jar-with-dependencies.jar
- connect-filter-transform-jar-with-dependencies.jar
- connect-eventbridge-transform-jar-with-dependencies.jar
部署 RocketMQ EventBridge
下载 EventBridge
您可以从 这里 下载 EventBridge 的二进制包:rocketmq-eventbridge-xxx-bin-release.zip。下载后解压缩,您将得到如下目录
/rocketmq-eventbridge-xxx-bin-release/
|——bin
| |——runserver.sh
| |——eventbridge.sh
|——config
| |——application.properties
|——jar
| |——rocketmq-eventbridge.jar
配置 EventBridge
在运行之前,我们需要通过修改 config/application.properties 文件来配置 EventBridge 的运行时环境,如下:
# Mysql database address
spring.datasource.url=jdbc:mysql://xxxx:3306/xxxx?characterEncoding=utf8
spring.datasource.username=xxx
spring.datasource.password=xxxx
# RocketMQ nameserver address
rocketmq.namesrvAddr=xxxxx:9876
# RocketMQ cluster name
rocketmq.cluster.name=DefaultCluster
# RocketMQ Connect address
rocketmq.connect.endpoint=xxxxxx:8082
# log default configuration
log.path=~
log.level=INFO
app.name=rocketmq-eventbridge启动 EventBridge
sh bin/eventbridge.sh start
默认情况下,日志目录位于 ~ /rocketmq-eventbridge/rocketmq-eventbridge.log,可以通过更改 log.path 和 app.name 来修改。日志可用于检查服务是否已正确启动。:
测试 EventBridge
服务启动后,我们可以使用以下演示案例来测试和验证 EventBridge。
演示
创建事件总线
POST /bus/createEventBus HTTP/1.1
Host: demo.eventbridge.com
Content-Type: application/json; charset=utf-8
{
"eventBusName":"demo-bus",
"description":"a demo bus."
}创建源事件
POST /source/createEventSource HTTP/1.1
Host: demo.eventbridge.com
Content-Type: application/json; charset=utf-8
{
"eventBusName":"demo-bus",
"eventSourceName":"demo-source",
"description":"A demo source."
}创建事件规则
POST /rule/createEventRule HTTP/1.1
Host: demo.eventbridge.com
Content-Type: application/json; charset=utf-8
{
"eventBusName":"demo-bus",
"eventRuleName":"demo-rule",
"description":"A demo rule.",
"filterPattern":"{}"
}创建事件目标
创建一个将事件传递到云端 EventBridge 的事件目标。
POST /target/createEventTargets HTTP/1.1
Host: demo.eventbridge.com
Content-Type: application/json; charset=utf-8
{
"eventBusName":"demo-bus",
"eventRuleName":"demo-rule",
"eventTargets":[
{
"eventTargetName":"eventbridge-target",
"className":"acs.eventbridge",
"config":{
"RegionId":"cn-hangzhou",
"AliyunEventBus":"rocketmq-eventbridge"
}
}
]
}创建一个将通知传递到钉钉机器人的事件目标:
POST /target/createEventTargets HTTP/1.1
Host: demo.eventbridge.com
Content-Type: application/json; charset=utf-8
{
"eventBusName":"demo-bus",
"eventRuleName":"demo-rule",
"eventTargets":[
{
"eventTargetName":"dingtalk-target",
"className":"acs.dingtalk",
"config":{
"WebHook":"https://oapi.dingtalk.com/robot/send?access_token=b43a54b702314415c2acdae97eda1e092528b7a9dddb31510a5b4430be2ef867",
"SecretKey":"SEC53483bf496b8f9e0b4ab0ab669d422208e6ccfaedfd5120ea6b8426b9ecd47aa",
"Body":"{\"template\":\"{\\\"text\\\":{\\\"content\\\":\\\"${content}\\\"},\\\"msgtype\\\":\\\"text\\\"}\",\"form\":\"TEMPLATE\",\"value\":\"{\\\"content\\\":\\\"$.data.body\\\"}\"}"
}
}
]
}将事件发送到事件总线
最后,我们将通过 API 发送一个事件,并验证目标端点是否按预期接收了相应的事件。
POST /putEvents HTTP/1.1
Host: demo.eventbridge.com
Content-Type:"application/cloudevents+json; charset=UTF-8"
{
"specversion" : "1.0",
"type" : "com.github.pull_request.opened",
"source" : "https://github.com/cloudevents/spec/pull",
"subject" : "123",
"id" : "A234-1234-1234",
"time" : "2018-04-05T17:31:00Z",
"datacontenttype" : "application/json",
"data" : {
"body":"demo"
},
"aliyuneventbusname":"demo-bus"
}