RocketMQ EventBridge 快速开始

RocketMQ EventBridge 需要一个消息服务来存储事件,另外需要一个Runtime来订阅并推送事件。这里我们选择 Apache RocketMQ 作为我们的消息服务,选择 Apache RocketMQ Connect 作为我们的Runtime来订阅和推送事件。当然,您也可以选择其他消息服务代替,EventBridge并不对此做限制。未来EventBridge也计划基于OpenMessaging Connect API 实现自己的Runtime,以便更好的提供事件驱动服务。

系统要求:

  • 64位操作系统,推荐 Linux/Unix/macOS
  • 64位 JDK 1.8+

部署Apache RocketMQ

Apache RocketMQ 是一个很棒的消息服务,我们默认选择它作为EventBus的默认存储。这里您可以根据这个手册快速部署: Apache RocketMQ Quick Start

部署Apache RocketMQ Connect

我们使用Apache RocketMQ Connect作为我们的默认Runtime,来连接外部的上下游服务,您可以根据手册完成部署: RocketMQ Connect Quick Start 。在部署 Apache RocketMQ Connect 之前,您应该下载下面的插件,并将其放在rocketmq-connect中配置参数“pluginPaths”所定义的目录下:

部署RocketMQ EventBridge

  • 获取 EventBridge

你可以从这里下载EventBridge的二进制包:rocketmq-eventbridge-xxx-bin-release.zip,下载完毕后进行解压缩,你会得到一个如下目录:

  1. /rocketmq-eventbridge-xxx-bin-release/
  2. |——bin
  3. | |——runserver.sh
  4. | |——eventbridge.sh
  5. |——config
  6. | |——application.properties
  7. |——jar
  8. | |——rocketmq-eventbridge.jar
  • 配置 EventBridge

运行前,我们需要配置EventBridge的运行环境,修改config/application.properties,参考如下:

  1. # Mysql数据库的连接地址
  2. spring.datasource.url=jdbc:mysql://xxxx:3306/xxxx?characterEncoding=utf8
  3. spring.datasource.username=xxx
  4. spring.datasource.password=xxxx
  5. # RocketMQ nameserver的连接地址
  6. rocketmq.namesrvAddr=xxxxx:9876
  7. # RocketMQ的集群名称.
  8. rocketmq.cluster.name=DefaultCluster
  9. # RocketMQ Connect的连接地址
  10. rocketmq.connect.endpoint=xxxxxx:8082
  11. # log默认配置
  12. log.path=~
  13. log.level=INFO
  14. app.name=rocketmq-eventbridge
  • 启动 EventBridge
  1. sh bin/eventbridge.sh start

log默认目录为~/rocketmq-eventbridge/rocketmq-eventbridge.log,可以修改上述log.path和app.name进行修改。可以通过日志来观察服务是否正常启动: img.png

  • 测试 EventBridge

当服务启动后,我们就可以通过下面的Demo用例来测试和验证EventBridge。

Demo

  • 创建事件总线
  1. POST /bus/createEventBus HTTP/1.1
  2. Host: demo.eventbridge.com
  3. Content-Type: application/json; charset=utf-8
  4. {
  5. "eventBusName":"demo-bus",
  6. "description":"a demo bus."
  7. }
  • 创建事件源
  1. POST /source/createEventSource HTTP/1.1
  2. Host: demo.eventbridge.com
  3. Content-Type: application/json; charset=utf-8
  4. {
  5. "eventBusName":"demo-bus",
  6. "eventSourceName":"demo-source",
  7. "description":"A demo source."
  8. }
  • 创建事件规则
  1. POST /rule/createEventRule HTTP/1.1
  2. Host: demo.eventbridge.com
  3. Content-Type: application/json; charset=utf-8
  4. {
  5. "eventBusName":"demo-bus",
  6. "eventRuleName":"demo-rule",
  7. "description":"A demo rule.",
  8. "filterPattern":"{}"
  9. }
  • 创建事件目标

创建一个投递到云上EventBridge的事件目标:

  1. POST /target/createEventTargets HTTP/1.1
  2. Host: demo.eventbridge.com
  3. Content-Type: application/json; charset=utf-8
  4. {
  5. "eventBusName":"demo-bus",
  6. "eventRuleName":"demo-rule",
  7. "eventTargets":[
  8. {
  9. "eventTargetName":"eventbridge-target",
  10. "className":"acs.eventbridge",
  11. "config":{
  12. "RegionId":"cn-hangzhou",
  13. "AliyunEventBus":"rocketmq-eventbridge"
  14. }
  15. }
  16. ]
  17. }

创建一个投递到钉钉机器人推送通知的事件目标:

  1. POST /target/createEventTargets HTTP/1.1
  2. Host: demo.eventbridge.com
  3. Content-Type: application/json; charset=utf-8
  4. {
  5. "eventBusName":"demo-bus",
  6. "eventRuleName":"demo-rule",
  7. "eventTargets":[
  8. {
  9. "eventTargetName":"dingtalk-target",
  10. "className":"acs.dingtalk",
  11. "config":{
  12. "WebHook":"https://oapi.dingtalk.com/robot/send?access_token=b43a54b702314415c2acdae97eda1e092528b7a9dddb31510a5b4430be2ef867",
  13. "SecretKey":"SEC53483bf496b8f9e0b4ab0ab669d422208e6ccfaedfd5120ea6b8426b9ecd47aa",
  14. "Body":"{\"template\":\"{\\\"text\\\":{\\\"content\\\":\\\"${content}\\\"},\\\"msgtype\\\":\\\"text\\\"}\",\"form\":\"TEMPLATE\",\"value\":\"{\\\"content\\\":\\\"$.data.body\\\"}\"}"
  15. }
  16. }
  17. ]
  18. }
  • 发送事件到EventBus

    最后,我们通过API发送一条事件,并验证Target端是否按预期收到对应的事件。

  1. POST /putEvents HTTP/1.1
  2. Host: demo.eventbridge.com
  3. Content-Type:"application/cloudevents+json; charset=UTF-8"
  4. {
  5. "specversion" : "1.0",
  6. "type" : "com.github.pull_request.opened",
  7. "source" : "https://github.com/cloudevents/spec/pull",
  8. "subject" : "123",
  9. "id" : "A234-1234-1234",
  10. "time" : "2018-04-05T17:31:00Z",
  11. "datacontenttype" : "application/json",
  12. "data" : {
  13. "body":"demo"
  14. },
  15. "aliyuneventbusname":"demo-bus"
  16. }