You can use a custom event bus of EventBridge to receive events that your applications produce and route the events to an Alibaba Cloud service. This topic describes how to publish marketing-related events from a customer relationship management (CRM) system to EventBridge, use event rules to filter the marketing-related events to obtain user registration and logon events, and then route the filtered events to Simple Message Queue (formerly MNS) (SMQ) queues.
Before you start
Make sure that the following operations are performed:
Background information
This topic describes how to publish custom application events to EventBridge and build an event-driven architecture in a CRM system.
CRM system: a system that is used to collect, manage, analyze, and utilize customer information. The system records various interactions between enterprises and customers in the marketing process and the status of various related activities. To publish marketing-related events to EventBridge, you must describe the attributes and data of the events in the CRM system based on the CloudEvents 1.0 specification. The following table describes the parameters that are configured to define a marketing-related event in the CRM system.
Parameter
Example
Description
source
crmabc.newsletter
The event source. The value of this parameter can be up to 128 bytes in length.
type
UserSignUp, UserLogin
The event type. The value of this parameter can be up to 64 bytes in length. Valid values:
UserPayOff: The user pays for the order.
UserLogin: The user logs on to the system.
UserSignUp: The user creates an account.
subject
crmabc/users/1234345
The event subject. The value of this parameter can be up to 128 bytes in length.
data
{ \"E-Mail\": \"${email}\" }
The event content. The additional data of custom application events.
Event-driven architecture: used to design and manage services. You can use the architecture to decouple service modules and improve service stability and flexibility. In an event-driven architecture, all business data can be mapped to events, and business events are classified into multiple event types based on business fields. For more information, see the "EDA" section of the Terms topic.
Step 1: Create a custom event bus
Log on to the EventBridge console.
In the left-side navigation pane, click Event Buses.
In the top navigation bar, select a region.
On the Event Bus page, click Quickly Create in the Custom Event Buses section.
In the Create Custom Event Bus wizard, perform the following steps:
In the Event Bus step, enter marketing in the Name field and Route marketing-related events in the CRM system in the Description field. Then, click Next Step.
In the Event Source step, enter MyCustomSource in the Event Source Name field and Marketing-related events produced in the CRM system in the Description field. Then, select Custom Application from the Event Provider drop-down list and click Next Step.
In the Event Rule step, enter MyCustomRule in the Event Rule Name field and Route marketing-related events from the CRM system to SMQ in the Description field. In the Pattern Content code editor, specify the event pattern and click Next Step.
The following sample code shows the event pattern that is used to filter the marketing-related events to obtain user registration or logon events:
{ "source": [ "crmabc.newsletter" ], "type": [ "UserSignUp", "UserLogin" ] }In the Event Target step, follow the on-screen instructions to configure the parameters and click Create. The following items describe the parameters:
Service Type: Select SMQ.
Queue: Select MyQueue.
Message Body: Select Complete Event.
Step 2: Use an SDK to publish events
Use an SDK to publish marketing-related events from the CRM system to EventBridge.
Add Maven dependencies.
Sample code:
<dependency> <groupId>com.aliyun</groupId> <artifactId>eventbridge-client</artifactId> <version>1.2.6</version> </dependency>Publish events.
Sample code:
import java.net.URI; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.aliyun.eventbridge.EventBridge; import com.aliyun.eventbridge.EventBridgeClient; import com.aliyun.eventbridge.models.CloudEvent; import com.aliyun.eventbridge.models.Config; import com.aliyun.eventbridge.models.PutEventsResponse; import com.aliyun.eventbridge.util.EventBuilder; import com.google.gson.Gson; public class PutEventsSample { private final EventBridge eventBridgeClient; public PutEventsSample() { Config authConfig = new Config(); authConfig.accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); authConfig.accessKeySecret =System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); authConfig.endpoint = "{endpoint}"; eventBridgeClient = new EventBridgeClient(authConfig); } public void putEventsSample() { List<CloudEvent> cloudEventList = new ArrayList<CloudEvent>(); cloudEventList.add(EventBuilder.builder() .withId("9e9b433c-a89b-4918-896b-7e1b7221****") .withSource(URI.create("crmabc.newsletter")) .withType("UserSignUp") .withSubject("crmabc/users/1234345") .withTime(new Date()) .withJsonStringData("{ \"E-Mail\": \"${email}\" }") .withAliyunEventBus("marketing") .build()); PutEventsResponse putEventsResponse = eventBridgeClient.putEvents(cloudEventList); System.out.println(new Gson().toJson(putEventsResponse)); } public static void main(String[] args){ PutEventsSample sample = new PutEventsSample(); try { sample.putEventsSample(); } catch (Throwable e) { e.printStackTrace(); } } }Parameter
Description
accessKeyId
The AccessKey ID of your Alibaba Cloud account.
accessKeySecret
The AccessKey secret of your Alibaba Cloud account.
endpoint
The endpoint of EventBridge. To obtain the endpoint, log on to the EventBridge console and click Event Buses in the left-side navigation pane. On the page that appears, find the custom event bus that you created and click Details in the Actions column. Then, view the endpoint information in the Endpoints section of the Overview page.
Verify the result
You can check whether the marketing-related events are received in the SMQ console.
Log on to the SMQ console.
In the left-side navigation pane, choose Queue Model > Queues.
In the top navigation bar, select a region.
On the Queues page, find the queue to which you routed the events and choose in the Actions column.
In the Receive Message section of the Quick Experience page, click Receive Message.
The following sample code provides an example of received events:
{ "eventId":"9e9b433c-a89b-4918-896b-7e1b7221****", "publishTime":1591272433527, "Message":{ "data":"{ \"E-Mail\": \"${email}\" }", "id":"9e9b433c-a89b-4918-896b-7e1b7221****", "source":"crmabc.newsletter", "specversion":"1.0", "type":"UserSignUp", "subject":"crmabc/users/1234345", "time":"2020-06-04T12:07:11.851Z" }, "eventBusName":"marketing", "eventBusOwner":"<yourAccountId>", "ruleName":"MyCustomRule", "eventBusInvoker":"<yourAccountId>", "MessageMD5":"D0256972C35F85409E38D176B7E7****" }