Route events to a database

更新时间:
复制 MD 格式

You can use event rules to filter events and route them to an ApsaraDB RDS for MySQL instance or a self-managed MySQL database. This topic uses a custom event as an example to describe the prerequisites, procedure, and verification steps.

Region limitations

Routing events to a database is supported only in the China (Zhangjiakou), Germany (Frankfurt), and Singapore regions.

Prerequisites

Step 1: Add a custom event source

  1. Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.

  2. In the top navigation bar, select a region. On the Event Buses page, click the name of the target custom event bus.

  3. In the left-side navigation pane, click Event Sources and then click Add Event Source.

  4. In the Add Custom Event Source panel, enter a Name and Description, select Custom Application for Event Provider, and then click OK.

Step 2: Create an event rule

Important

The event target and the event rule must be in the same region.

  1. Log on to the EventBridge console.

  2. In the left-side navigation pane, click Event Buses.

  3. In the top navigation bar, select a region.

  4. On the Event Buses page, click the name of the target event bus.

  5. In the left-side navigation pane, click Event Rules.

  6. On the Event Rules page, click Create Rule.

  7. On the Create Rule page, complete the following steps.

    1. In the Configure Basic Info step, enter a rule name in the Name field and a rule description in the Description field, and then click Next.

    2. In the Configure Event Pattern step, set Event Source Type to Custom Event Sources and select the custom Event Sources you added in Step 1. Then, enter an event pattern in the Event Pattern Content code editor and click Next.

      For more information, see Event patterns.

    3. In the Configure Targets step, select Database for Service Type, configure the following parameters, and then click Create.

      Note

      You can add up to five targets to a single event rule.

      Category

      Parameter

      Description

      Database Configuration

      Instance ID

      The ID of the target ApsaraDB RDS for MySQL instance. This parameter is required only when Database Configuration is set to RDS for MySQL.

      The endpoint of the database.

      The endpoint of the target self-managed MySQL database. The endpoint consists of the host's IP address or domain name and the port number, which is 3306. This parameter is required only when Database Configuration is set to Self-managed MySQL Database.

      The name of the database.

      The name of the target database.

      • If Database Configuration is set to RDS for MySQL, select a database name from the drop-down list.

      • If Database Configuration is set to Self-managed MySQL Database, enter the database name.

      The username used to log on to the database.

      The account used to access the target database.

      The password used to log on to the database.

      The password for the specified database account.

      Concurrency Configuration (Database Connection Pool)

      The maximum number of concurrent database connections. Default: 10.

      Network Configuration

      Internet

      Route events to the database over the public network. This option is available only when Database Configuration is set to Self-managed MySQL Database.

      VPC

      The VPC information used to route events.

      • VPC: The VPC used to route events.

      • vSwitches: The vSwitch used to route events.

      • Security Group: The security group that allows EventBridge to access the database.

      Database Import Configuration

      Quick Configuration

      Provides a simple way to configure data operations on the database table.

      • Table Name: The name of an existing data table in the database. The routed event content is written to this table.

      • Operation Method: The Data Manipulation Language (DML) operation to be performed on the database table. The following operations are supported:

        • INSERT: Inserts event content into the database table.

        • UPDATE: Updates content in the database table based on the specified key-value pair.

        • DELETE: Deletes content from the database table based on the specified key-value pair.

      • Column, Value: EventBridge routes the event content to the key-value pairs of the database table.

        • Column: A field in the database table. You can enter a constant or a variable from the event.

        • Value: The value for a table field (key). You can enter a constant or a variable from the event. Maximum length: 1,024 characters.

        You can add more key-value pairs as needed by clicking Add .

        Note

        Quick configuration generates the SQL statements that are executed.

      Custom SQL Statement

      The SQL statement to write data to the database table.

      • Variables: Defines variables that can be used in the SQL statement.

      • SQL Statement: The SQL statement to execute. Maximum length: 10,240 characters.

      For more information, see Event transformation.

Step 3: Publish an event

  1. Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.

  2. In the top navigation bar, select a region.
  3. On the Event Buses page, find the target bus and click Publish Event in the Operations column.

    Note

    You can publish events from the console only for custom event buses.

  4. In the Publish Event to Custom Event Bus {name} panel, select a custom event source from the Custom Event Source drop-down list, enter the event content in the Event Body code editor, and then click OK.

    For information about event parameters, see Event structure.

Verify the results

You can view the routed event content in the database. This example uses an ApsaraDB RDS for MySQL instance as the event target and inserts data with an id of 123 to verify successful delivery.

  1. Go to the RDS Instances page. In the top navigation bar, select a region and then click the ID of the target instance.

  2. On the Basic Information page of the instance, click Log On to Database in the upper-right corner.

  3. In the Log On to Instance dialog box, enter the database account and password, and then click Login.

  4. View the data in the database table.

    The inserted data is visible in the user table. The row with id=123 has NULL values in the name and AAA columns. The other rows (id=33, id=22, and id=11) contain non-NULL values.

    You can view the inserted content in the destination database table.