rocketmq应用层: eventbridge

209 阅读3分钟

概览

rocketmq-eventbridge是支持Faas + event-driven开发模式的中间件,而该项目目的是提供了一个HTTP APIServer,它向下和rocketmq的链接依赖rocketmq-connect项目,向上则不提供过多的功能。在domain设计上,非常类似于Knaitive的Eventing项目的设计。

project

  • adapter Adapt to different operating environments
    • persistence Implement the repository API in the domain layer and persist the model data.
    • api The open API provided by EventBridge.
    • rpc Implement the rpc API in the domain layer to adapter the run environment.
  • common The common code of all modules.
  • domain The core code of EventBridge.
    • model:The core properties and behaviors of model on EventBridge.
    • service: The domain services which across multiple domains.
  • start

concepts

EventSource

Used to manage events sent to EventBridge, all events sent to EventBridge must be marked with the source name information, corresponding to the source field

public class EventSource {
    private String accountId;
    private String eventBusName;
    private String name;
    private String description;
    private EventSourceTypeEnum type;
    private String className;
    private Map<String, Object> config;
    private String runContext;
    private EventSourceStatusEnum status;
    private Date gmtCreate;
    private Date gmtModify;
}

EventBus

the event bus. Used to store events sent to EventBridge

public class EventBus {
    private String accountId;
    private String name;
    private String description;
    private Date gmtCreate;
    private Date gmtModify;
    private EventBusRepository eventBusRepository;

}

EventRule

When a consumer needs to subscribe to events, they can configure filtering and transformation information through rules to push events to the designated target endpoint.

rule = filter + transform + target

public class EventRule {
    private String accountId;
    private String eventBusName;
    private String name;
    private String description;
    private String filterPattern;
    private String status;
    private Date gmtCreate;
    private Date gmtModify;
}

EventTarget

the target endpoint of the event, which is the actual event consumer.

public class EventTarget {
    private String accountId;
    private String eventBusName;
    private String eventRuleName;
    private String name;
    private String className;
    private Map<String, Object> config;
    private RunOptions runOptions;
    private String runContext;
    private EventTargetStatusEnum status;
    private Date gmtCreate;
    private Date gmtModify;
}

quick start

steps

deploy rocketmq

quick-start

deploy rocketmq-connect

  1. quick-start
  2. install plugins (put them in the directory defined by the "pluginPaths" configuration parameter in rocketmq-connect)
    • 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

deploy eventbridge

  1. wget /rocketmq-eventbridge-xxx-bin-release/
  2. modify config/application.properties
    • mysql address
    • rocketmq namesrv address
    • rocketmq connect address
  3. sh bin/eventbridge.sh start
  4. use http actions to test

actions

POST /bus/createEventBus HTTP/1.1
{
"eventBusName":"demo-bus",
"description":"a demo bus."
}

POST /source/createEventSource HTTP/1.1
{
"eventBusName":"demo-bus",
"eventSourceName":"demo-source",
"description":"A demo source."
}

POST /rule/createEventRule HTTP/1.1
{
  "eventBusName":"demo-bus",
  "eventRuleName":"demo-rule",
  "description":"A demo rule.",
  "filterPattern":"{}"
}

POST /target/createEventTargets HTTP/1.1
{
    "eventBusName":"demo-bus",
    "eventRuleName":"demo-rule",
    "eventTargets":[
            {
            "eventTargetName":"eventbridge-target",
            "className":"acs.eventbridge",
                "config":{
                "RegionId":"cn-hangzhou",
                "AliyunEventBus":"rocketmq-eventbridge"
                }
            },
            {
            "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\\\"}\"}"
                }
            }
        ]
}

POST /putEvents HTTP/1.1
{
  "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"
}

pkgs

main

start/src/main/java/org/apache/rocketmq/eventbridge/Main.java

一个标准的SpringBoot程序

@SpringBootApplication(scanBasePackages = "org.apache.rocketmq.eventbridge.*")
@EnableCaching
public class Main {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Main.class);
        app.addListeners();
        app.run(args);
    }
}

start

下面这些模块不在start里,而是通过@SpringBootApplication(scanBasePackages = "org.apache.rocketmq.eventbridge.*"),也就是说start使用的service不在这个包,而在其他org.apache.rocketmq.eventbridge子包下。

各个包的数量:

  • adapter-api: 109

  • adapter-persistence: 45

  • adapter-rpc: 23

  • common: 65

  • domain: 95

  • start: 5

  • model: 核心业务服务层,用于管理领域内的核心业务模型

  • repository: 定义model持久化的API

  • rpc: 定义来依赖的外部API

  • service: 用于存放跨领域的服务,不直接依赖repository,而是依赖model中的service

domain

  • common
  • model
    • apidestination
    • bus
    • classes
    • connection
    • data
    • rule
    • run
    • source
    • target
  • repository
  • rpc
  • service
  • cache

adpter

  • api: 定义controller,DTO等
  • persistence: 通过MyBatis将domain持久化好DB
  • rpc: 用途未知