After the ApsaraMQ forRocketMQ event source is integrated with Function Compute by using EventBridge, the ApsaraMQ forRocketMQ trigger can trigger associated functions. The functions can be used to perform custom operations on the messages that are published to ApsaraMQ forRocketMQ. This topic describes how to create an ApsaraMQ for RocketMQ trigger, configure function input parameters, and write and test code in the Function Compute console.
Overview
- Event mode
- Event stream mode
- Event mode: Each message is passed to the function as an event parameter. The event follows the CloudEvents specification. For information about the relationship between the message content and CloudEvents, see Step 2: Configure the input parameters of the function.
- Event stream mode: One or more messages are pushed to Function Compute for batch processing based on your batch configurations. This model is suitable for scenarios in which end-to-end streaming data is processed.
Precautions
- The ApsaraMQ forRocketMQ instance that is used as the trigger source must reside in the same region as the function in Function Compute.
- When the number of created custom event buses and the number of created event rules reach the upper limits, you can no longer create an ApsaraMQ for RocketMQ trigger of the event model.
- When the number of created event streams reaches the upper limit, you cannot create an ApsaraMQ for RocketMQ trigger of the event stream mode.
For information about the limits on the number of resources that can be created by using an Alibaba Cloud account in each region, see Limits.
Before you begin
- EventBridge
- Function Compute
- ApsaraMQ forRocketMQ
Step 1: Create a trigger
- Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
- In the top navigation bar, select a region. On the Services page, click the desired service.
- On the Functions page, click the function that you want to manage.
- On the function details page, click the Triggers tab, select the version or alias from the Version or Alias drop-down list, and then click Create Trigger.
- In the Create Trigger panel, specify related parameters. After you specify the parameters, click OK.The following table describes the basic parameters.
Parameter Description Example Trigger Type Select ApsaraMQ for RocketMQ from the drop-down list. ApsaraMQ for RocketMQ Name Enter a trigger name. rocketmq-trigger Version or Alias The default value is LATEST. If you want to create a trigger for another version or alias, switch to the specified version or alias in the upper-right corner of the function details page. For more information about versions and aliases of a service, see Manage versions and Manage aliases. LATEST ApsaraMQ for RocketMQ Instance Select an ApsaraMQ forRocketMQ instance from the drop-down list. MQ_INST_164901546557****_BX7**** Topic Select a topic of the ApsaraMQ forRocketMQ instance from the drop-down list. topic1 Tag Specify a tag for message filtering. The execution of a function is triggered only when a message that contains the specified filtering tag is received.
tag Group ID Select the group ID of the created ApsaraMQ forRocketMQ instance. GID_group1 Consumer Offset Select a consumer offset of the message. A consumer offset specifies the point at which ApsaraMQ forRocketMQ starts to pull messages from the event bus. Valid values: - Latest Offset: consumes messages from the latest offset.
- Earliest Offset: consumes messages from the earliest offset.
- Timestamp: consumes messages from the specified timestamp.
Latest Offset Invocation Method Select a method to invoke the function. Valid values:- Sync Invocation: This mode is suitable for sequential invocations. When a single event or a batch of events trigger the function, Function Compute runs the function and waits for a response before it processes the next event or batch of events. The upper limit of the payload for a synchronous invocation request is 32 MB. For more information, see Synchronous invocations.
- Async Invocation: This mode allows you to quickly consume events. When a single event or a batch of events trigger the function, Function Compute immediately returns a response and continue to process the next event or batch of events During this process, the function is run in the asynchronous mode. The upper limit of the payload for an asynchronous invocation request is 128 KB. For more information, see Overview.
Sync Invocation Message Push Mode The underlying application mode that is used for pushing message data to Function Compute. Valid values:- Event Stream Mode: One or more messages are pushed to Function Compute for batch processing based on your batch configurations. This model is suitable for scenarios in which end-to-end streaming data is processed.
- Event Mode: Each message is passed to the function as an event parameter. The event follows the CloudEvents specification. For information about the relationship between message content and CloudEvents, see Step 2: Configure the input parameters of the function.
Event Mode Trigger State Specify whether to enable the trigger after it is created. By default, Enable Trigger is selected and the trigger is enabled after it is created. N/A For more information about advanced configurations such as message pushing, retry, and dead letter configurations, see Advanced features of triggers.
After the trigger is created, it is displayed on the Triggers tab. To modify or delete an existing trigger, see Trigger management.
Step 2: Configure the input parameters of the function
An ApsaraMQ forRocketMQ event source is passed to a function in the form of event
that acts as an input parameter. You can manually pass event
to a function to simulate a trigger event and test whether the code of the function is correct.
- On the function details page, click the Code tab and click the icon. From the drop-down list that appears, select Configure Test Parameters.
- In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, and specify Event Name and the event content. After you specify the parameters, click OK.Sample code of
event
in the event mode:{ "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" } }
Sample code ofevent
in the event stream mode:[ { "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" } } ]
The following table describes the parameters in data. For information about the parameters that are defined in the CloudEvents specification, see Overview.Parameter Type Example Description topic String TopicName The name of the topic. systemProperties Map The 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 exists.
- false: No message trace exists.
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 for filtering. CONSUME_START_TIME Long 1628577790396 The start time of consumption. Unit: milliseconds. UNIQ_KEY String AC14C305069E1B28CDFA3181CDA2**** The unique key of the message. TAGS String systemProperties.TAGS The tags for filtering. INSTANCE_ID String MQ_INST_123456789098****_BXhFHryi The ID of the instance. userProperties Map N/A The user properties. body String TEST The content of the message.
Step 3: Write the function code and test the function
After you create the trigger, you can write function code and test the function to verify that the code is correct. When ApsaraMQ forRocketMQ events are delivered to Function Compute by using EventBridge, the trigger triggers function execution.
- On the function details page, click the Code tab, edit the function code in the code editor, and then click Deploy.In this topic, Node.js is used as function code.
'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 the Code tab and click Test Function. After the function is executed, you can view the result on the Code tab.
References
To modify or delete an existing trigger, see Trigger management.