RocketMQ trigger
ApsaraMQ for RocketMQ acts as the event source and, through EventBridge, integrates with Function Compute. The ApsaraMQ for RocketMQ trigger runs the associated function, and you can customize how messages published to ApsaraMQ for RocketMQ are processed. Create a RocketMQ trigger in the Function Compute console, configure function entry point parameters, and write and test code.
Overview
After you submit a trigger creation request in the Function Compute console, Function Compute automatically creates, on the EventBridge side, an event stream resource based on the trigger configuration.
After the trigger is created, you can view the trigger information in the Function Compute console. Information about the automatically created resources is also available in the EventBridge console. When a message is enqueued in the source ApsaraMQ for RocketMQ instance, the trigger causes Function Compute to run. During execution, one or more message events are pushed to the function in batches for processing based on your batching configuration. This approach is suitable for end-to-end streaming data processing.
Considerations
-
The ApsaraMQ for RocketMQ instance that serves as the trigger source must reside in the same region as the Function Compute function.
-
After the number of event streams that you create exceeds the upper limit, you can no longer create RocketMQ triggers.
For the limits on the resource count involved in creating triggers within a single region of a single Alibaba Cloud account, see Limits.
Prerequisites
-
EventBridge
-
Function Compute
-
ApsaraMQ for RocketMQ
Step 1: Create a trigger
-
Log on to Function Compute console and find the target function. On the function details page, click the Trigger tab, and then click Create Trigger.
-
In the Create Trigger panel, for Trigger Type, select ApsaraMQ for RocketMQ, set the following configuration items, and then click OK.
Configuration item
Operation
Example in this topic
Name
Enter a custom trigger name.
rocketmq-trigger
Version or Alias
The default value is LATEST. To create a trigger for another version or alias, first switch to that version or alias in the upper-right corner of the function details page. For an introduction to versions and aliases, see Manage versions and Manage aliases.
LATEST
ApsaraMQ for RocketMQ Instance
Select the RocketMQ instance that you created.
rmq-zky****
Topic
Select the Topic of the RocketMQ instance that you created.
topic1
Tag
Enter the message filter tag. The function is triggered to run only when a message that contains this filter tag is received.
tag
Group ID
Quickly create a Group ID or select an existing Group ID.
Do not share the same Group ID with existing workloads. Otherwise, the sending and receiving of messages for the existing workloads may be affected.
GID_FC_TRIGGER_****
Consumer Offset
Select the consumption offset of messages, that is, the position from which ApsaraMQ for RocketMQ starts to pull messages from the event bus. Valid values:
-
Latest Offset: Consumption starts from the latest offset.
-
Earliest Offset: Consumption starts from the earliest offset.
-
Timestamp: Consumption starts from the specified timestamp.
Latest Offset
Invocation Method
Select the function invocation method.
Valid values:
-
Sync Invocation: This option is suitable for sequential invocation. A single event (or batch of events) triggers a function invocation. After the function finishes running and returns the result, the next event (or batch of events) triggers another function invocation. The maximum payload of a synchronous call request body is 32 MB. For more information, see Synchronous calls.
-
Async Invocation: This option allows events to be consumed quickly. A single event (or batch of events) triggers a function invocation. Function Compute immediately returns a response, and then the next event (or batch of events) triggers another function invocation. During this process, the function runs asynchronously. The maximum payload of an asynchronous call request body is 128 KB. For more information, see Asynchronous invocation.
Sync Invocation
For more information about advanced configuration items such as push configuration, retry, and dead letter, see Advanced Trigger Features.
-
Step 2: (Optional) Configure function entry point parameters
ApsaraMQ for RocketMQ, as the event source, is passed to the function as an input parameter in the form of event. Use code to parse the event parameter and process the event. To simulate a trigger event and test whether the function code is correct, manually pass an event to the function.
On the Code tab of the function details page, click the
icon next Test Function and select Configure Test Parameters from the drop-down list. -
In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, enter the event name and event content, and then click OK.
eventis in the following format:[ { "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" } } ]data field parameters are described in the following table. For more information about the parameters defined in the CloudEvents specification, see Event overview.
Parameter
Type
Example
Description
topic
String
TopicName
The topic name.
systemProperties
Map
System properties.
MIN_OFFSET
Int
0
The earliest offset.
TRACE_ON
Boolean
true
Indicates whether a message trace exists. Valid values:
true: A message trace is available.
false: No message trace.
MAX_OFFSET
Int
8
The latest offset.
MSG_REGION
String
cn-hangzhou
The region from which the message was sent.
KEYS
String
systemProperties.KEYS
The keys used for message filtering.
CONSUME_START_TIME
Long
1628577790396
The message consumption start time. Unit: milliseconds.
UNIQ_KEY
String
AC14C305069E1B28CDFA3181CDA2****
The unique key of the message.
TAGS
String
systemProperties.TAGS
The tags used for message filtering.
INSTANCE_ID
String
MQ_INST_123456789098****_BXhFHryi
The instance ID.
userProperties
Map
User-defined properties.
body
String
TEST
The message body.
Step 3: Write and test the function code
After the trigger is created, you can write and test the function code to verify that it is correct. In practice, when a ApsaraMQ for RocketMQ event passes through EventBridge and is delivered to Function Compute, the trigger automatically causes the function to run.
-
On the Code tab of the function details page, write code in the code editor and click Deploy Code.
This topic uses Node.js function code as an example.
'use strict'; /* To enable the initializer feature please implement the initializer function as below: exports.initializer = (context, callback) => { console.log('initializing'); callback(null, ''); }; */ exports.handler = (event, context, callback) => { console.log("event: %s", event); // Parse the event parameters and process the event. callback(null, 'return result'); } -
Test the function.
Method 1: If you configure the function entry point parameter
eventto simulate the event source, Click Test Function.Method 2: Log on to the ApsaraMQ for RocketMQ console. Find the target Topic of the target RocketMQ instance. Click One-Click Message Sending and Receiving Experience, and then send a message by following the on-screen instructions.
-
After the execution is complete, on the Code tab of the target function details page, click Real-time Logs to view the result.