You can use Apache RocketMQ as an event source and integrate it with Function Compute through EventBridge. An Apache RocketMQ trigger can then be used to invoke an associated function, which lets you process messages published to Apache RocketMQ with custom logic. This topic describes how to create an Apache RocketMQ trigger, configure input parameters, and write and test code in the Function Compute console.
Background information
When you create a trigger in the Function Compute console, Function Compute automatically creates event stream resources in EventBridge based on the trigger configuration.
After the trigger is created, you can view its information in the Function Compute console. You can also view the automatically created resources in the EventBridge console. When a message is published to a topic in Apache RocketMQ, the function is triggered. Based on the batching configuration, one or more message events are pushed to the function in batches.
Prerequisites
EventBridge
Function Compute
Apache RocketMQ
Apache RocketMQ 5.0: Quick Start.
Apache RocketMQ 4.x: Quick Start.
A self-managed Apache RocketMQ cluster is created.
A topic is created.
A consumer group is created.
You can quickly deploy an Apache RocketMQ cluster and send and receive messages by referring to the following documents:
Limits
The Apache RocketMQ cluster that serves as the event source must be accessible over the internet or from within an Alibaba Cloud virtual private cloud (VPC).
If the Apache RocketMQ cluster is accessible from within a VPC, the VPC-connected instance and the Function Compute function must be in the same region.
You cannot create Apache RocketMQ triggers if the number of created event streams reaches the upper limit. For more information about the limit on the number of event streams, see Limits.
Step 1: Create an Apache RocketMQ trigger
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Triggers tab and then click Create Trigger.
In the Create Trigger panel, configure the parameters and click OK.
Parameter
Procedure
Example
Trigger Type
Select Self-managed Apache RocketMQ.
Self-managed Apache RocketMQ
Name
Enter a custom name for the trigger.
apache-rocketmq-trigger
Version Or Alias
The default value is LATEST. To create a trigger for a different version or alias, switch to that version or alias in the upper-right corner of the function details page. For more information about versions and aliases, see Manage versions and Manage aliases.
LATEST
Endpoint
Enter the NameServer address of the cluster.
192.168.X.X:9876
Topic
Select a topic of the created Apache RocketMQ instance.
testTopic
Group ID
Select a consumer group ID of the created Apache RocketMQ instance.
testGroup
FilterType
Select the message filtering type. Valid values:
Tag: Filters messages by tag.
SQL: Filters messages by SQL statement. You can match the properties and property values of messages.
Tag
Filter
After you select a FilterType, configure the corresponding filter statement.
TagA
Authentication Mode
Select the authentication mode. The ACL mode is supported.
ACL
Username
If you set Authentication Mode to ACL, configure the Apache RocketMQ username for identity verification.
admin
Password
If you set Authentication Mode to ACL, configure the Apache RocketMQ password for identity verification.
******
Consumer Offset
Select the consumer offset. This specifies the position from which EventBridge starts to pull messages. Valid values:
Latest Offset: Consumes messages from the latest offset.
Earliest Offset: Consumes messages from the earliest offset.
Timestamp: Consumes messages from a specified point in time.
Latest Offset
Network Configuration
Select the network type for message routing. Valid values:
Internet: Accesses the Apache RocketMQ cluster over the internet.
VPC: Accesses the Apache RocketMQ cluster through an Alibaba Cloud virtual private cloud. You must select the corresponding VPC, VSwitch, and Security Group.
Internet
Invocation Mode
Select the function invocation mode.
Valid values:
Synchronous: This is the default invocation mode. An event triggers the function. Function Compute waits for the function to finish and then returns the result. For more information, see Synchronous invocations.
Asynchronous: This mode is suitable for functions with long scheduling delays. After an event triggers the function, Function Compute immediately returns a response and ensures that the function is executed at least once. However, it does not return the execution result. For more information, see Asynchronous invocations.
Synchronous
Trigger State
Specifies whether to enable the trigger immediately after it is created. By default, this option is selected, which means the trigger is enabled immediately after creation.
Enable Trigger
For information about advanced configurations, such as push configurations, retries, and dead-letter queues, see Advanced features of triggers.
After the trigger is created, it appears on the Triggers tab. To modify or delete the trigger, see Manage triggers.
Step 2: Configure function input parameters
A self-managed Apache RocketMQ event source passes an event as an input parameter to the function. You can manually pass the event to the function to simulate a triggering event.
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, select Create New Test Event or Edit Existing Test Event, enter the event name and event content, and then click OK.
The event format is as follows.
[ { "msgId": "7F0000010BDD2A84AEE70DA49B57****", "topic": "testTopic", "systemProperties": { "UNIQ_KEY": "7F0000010BDD2A84AEE70DA49B57****", "CLUSTER": "DefaultCluster", "MIN_OFFSET": "0", "TAGS": "TagA", "MAX_OFFSET": "128" }, "userProperties": {}, "body": "Hello RocketMQ" } ]The following table describes the parameters in the event.
Parameter
Type
Example
Description
msgId
String
7F0000010BDD2A84AEE70DA49B57****
The ID of the Apache RocketMQ message.
topic
String
testTopic
The topic name.
systemProperties
Map
The system properties.
UNIQ_KEY
String
7F0000010BDD2A84AEE70DA49B57****
The unique key of the message.
CLUSTER
String
DefaultCluster
The name of the Apache RocketMQ cluster.
MIN_OFFSET
Int
0
The minimum offset.
MAX_OFFSET
Int
128
The maximum offset.
TAGS
String
TagA
The filter property.
userProperties
Map
None
The user properties.
body
String
Hello RocketMQ
The message content.
Step 3: Write and test the function code
After you create the trigger, you can write and test the function code to verify that it is correct. In a production environment, the trigger automatically invokes the function whenever Apache RocketMQ receives a message.
On the function details page, click the Code tab. In the code editor, write your code and then click Deploy Code.
This topic uses Node.js code as an example. The sample code is as follows.
'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'); }Click Test Function.
More information
In addition to the Function Compute console, you can configure triggers by using one of the following methods:
Use Serverless Devs tool to configure triggers. For more operations, please refer to Common commands of Serverless Devs.
Use SDKs to configure triggers. For more information, see SDKs.
To modify or delete an existing trigger, see Manage triggers.