Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(events): add RocketMQEvent struct #25

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

[OSS 事件格式](https://github.com/aliyun/fc-runtime-go-sdk/blob/master/events/README_OSS.md)


[SLS 事件格式](https://github.com/aliyun/fc-runtime-go-sdk/blob/master/events/README_SLS.md)

[MNS Topic 事件格式](https://github.com/aliyun/fc-runtime-go-sdk/blob/master/events/README_MNS_Topic.md)

[SLS Queue 事件格式](https://github.com/aliyun/fc-runtime-go-sdk/blob/master/events/README_MNS_Queue.md)

[Kafka Eventbridge 事件格式](https://github.com/aliyun/fc-runtime-go-sdk/blob/master/events/README_KAFKA_EVENTBRIDGE.md)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventBridge的readme应该还没有吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventBridge的readme应该还没有吧?

有的,这是别人忘记加了我顺便一并加上的

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,看到了


[RocketMQ 事件格式](https://github.com/aliyun/fc-runtime-go-sdk/blob/master/events/README_RocketMQ.md)
140 changes: 140 additions & 0 deletions events/README_RocketMQ.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# RocketMQ 触发器

### Event 格式

```json
{
"id":"94ebc15f-f0db-4bbe-acce-56fb72fb****",
"source":"RocketMQ-Function-rocketmq-trigger",
"specversion":"1.0",
"type":"mq:Topic:SendMessage",
"datacontenttype":"application/json; charset=utf-8",
"subject":"acs:mq:cn-hangzhou:164901546557****:MQ_INST_164901546557****_BXhFHryi%TopicName",
"time":"2021-04-08T06:01:20.766Z",
"aliyunaccountid":"164901546557****",
"aliyunpublishtime":"2021-10-15T02:05:16.791Z",
"aliyunoriginalaccountid":"164901546557****",
"aliyuneventbusname":"RocketMQ-Function-rocketmq-trigger",
"aliyunregionid":"cn-chengdu",
"aliyunpublishaddr":"42.120.XX.XX",
"data":{
"topic":"TopicName",
"systemProperties":{
"MIN_OFFSET":"0",
"TRACE_ON":"true",
"MAX_OFFSET":"8",
"MSG_REGION":"cn-hangzhou",
"KEYS":"systemProperties.KEYS",
"CONSUME_START_TIME":1628577790396,
"TAGS":"systemProperties.TAGS",
"INSTANCE_ID":"MQ_INST_164901546557****_BXhFHryi"
},
"userProperties":{

},
"body":"TEST"
}
}
```

### 事件流模型的 event 格式如下所示

```json
[
{
"id":"94ebc15f-f0db-4bbe-acce-56fb72fb****",
"source":"RocketMQ-Function-rocketmq-trigger",
"specversion":"1.0",
"type":"mq:Topic:SendMessage",
"datacontenttype":"application/json; charset=utf-8",
"subject":"acs:mq:cn-hangzhou:164901546557****:MQ_INST_164901546557****_BXhFHryi%TopicName",
"time":"2021-04-08T06:01:20.766Z",
"aliyunaccountid":"164901546557****",
"aliyunpublishtime":"2021-10-15T02:05:16.791Z",
"aliyunoriginalaccountid":"164901546557****",
"aliyuneventbusname":"RocketMQ-Function-rocketmq-trigger",
"aliyunregionid":"cn-chengdu",
"aliyunpublishaddr":"42.120.XX.XX",
"data":{
"topic":"TopicName",
"systemProperties":{
"MIN_OFFSET":"0",
"TRACE_ON":"true",
"MAX_OFFSET":"8",
"MSG_REGION":"cn-hangzhou",
"KEYS":"systemProperties.KEYS",
"CONSUME_START_TIME":1628577790396,
"TAGS":"systemProperties.TAGS",
"INSTANCE_ID":"MQ_INST_164901546557****_BXhFHryi"
},
"userProperties":{

},
"body":"TEST"
}
},
{
"id":"94ebc15f-f0db-4bbe-acce-56fb72fb****",
"source":"RocketMQ-Function-rocketmq-trigger",
"specversion":"1.0",
"type":"mq:Topic:SendMessage",
"datacontenttype":"application/json; charset=utf-8",
"subject":"acs:mq:cn-hangzhou:164901546557****:MQ_INST_164901546557****_BXhFHryi%TopicName",
"time":"2021-04-08T06:01:20.766Z",
"aliyunaccountid":"164901546557****",
"aliyunpublishtime":"2021-10-15T02:05:16.791Z",
"aliyunoriginalaccountid":"164901546557****",
"aliyuneventbusname":"RocketMQ-Function-rocketmq-trigger",
"aliyunregionid":"cn-chengdu",
"aliyunpublishaddr":"42.120.XX.XX",
"data":{
"topic":"TopicName",
"systemProperties":{
"MIN_OFFSET":"0",
"TRACE_ON":"true",
"MAX_OFFSET":"8",
"MSG_REGION":"cn-hangzhou",
"KEYS":"systemProperties.KEYS",
"CONSUME_START_TIME":1628577790396,
"TAGS":"systemProperties.TAGS",
"INSTANCE_ID":"MQ_INST_164901546557****_BXhFHryi"
},
"userProperties":{

},
"body":"TEST"
}
}
]
```

### 使用示例

下面展示了一个简单的 FC demo,当你为该函数配置 RocketMQ 触发器之后,每当你向 RocketMQ 发送任意消息之后,就会触发该函数,并返回消息内容。

```go
package main

import (
"context"
"encoding/json"
"fmt"

"github.com/aliyun/fc-runtime-go-sdk/events"
"github.com/aliyun/fc-runtime-go-sdk/fc"
"github.com/aliyun/fc-runtime-go-sdk/fccontext"
)

func HandleRequest(ctx context.Context, event events.RocketMQEvent) (string, error) {
fcCtx, _ := fccontext.FromContext(ctx)
flog := fcCtx.GetLogger()
mes, _ := json.Marshal(event)
flog.Info("event:", string(mes))
return fmt.Sprintf("Body:%s", event.Data.Body), nil
}

func main() {
fc.Start(HandleRequest)
}
```

27 changes: 27 additions & 0 deletions events/rocketmq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package events

import "time"

type RocketMQEvent struct {
ID *string `json:"id"`
Source *string `json:"source"`
SpecVersion *string `json:"specversion"`
Type *string `json:"type"`
DataContentType *string `json:"datacontenttype"`
Subject *string `json:"subject"`
Time *time.Time `json:"time"`
AliyunAccountId *string `json:"aliyunaccountid"`
AliyunPublishTime *time.Time `json:"aliyunpublishtime"`
AliyunOriginalAccountId *string `json:"aliyunoriginalaccountid"`
AliyunEventBusName *string `json:"aliyuneventbusname"`
AliyunRegionId *string `json:"aliyunregionid"`
AliyunPublishAddr *string `json:"aliyunpublishaddr"`
Data *RocketMQData `json:"data"`
}

type RocketMQData struct {
Topic string `json:"topic"`
SystemProperties map[string]string `json:"systemProperties"`
UserProperties map[string]string `json:"userProperties"`
Body string `json:"body"`
}