使用 Azure 事件中心和 Azure 逻辑应用监视、接收和发送事件Monitor, receive, and send events with Azure Event Hubs and Azure Logic Apps

本文演示如何使用 Azure 事件中心连接器监视和管理从逻辑应用发送到 Azure 事件中心的事件。This article shows how you can monitor and manage events sent to Azure Event Hubs from inside a logic app with the Azure Event Hubs connector. 通过这样的方式可以创建逻辑应用,从事件中心自动执行检查、发送和接收事件的任务和工作流。That way, you can create logic apps that automate tasks and workflows for checking, sending, and receiving events from your Event Hub. 如需特定于连接器的技术信息,请参阅 Azure 事件中心连接器参考For connector-specific technical information, see the Azure Event Hubs connector reference.

先决条件Prerequisites

检查权限并获取连接字符串Check permissions and get connection string

若要确保逻辑应用可以访问事件中心,请检查权限并获取事件中心命名空间的连接字符串。To make sure that your logic app can access your Event Hub, check your permissions and get the connection string for your Event Hubs namespace.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 转到事件中心命名空间,而不是特定的事件中心。 Go to your Event Hubs namespace, not a specific Event Hub.

  3. 在命名空间菜单上的“设置”下,选择“共享访问策略”。 On the namespace menu, under Settings, select Shared access policies. 在“声明”下,检查你是否有该命名空间的“管理”权限。 Under Claims, check that you have Manage permissions for that namespace.

    管理事件中心命名空间的权限

  4. 如果想要以后手动输入连接信息,请获取事件中心命名空间的连接字符串。If you want to later manually enter your connection information, get the connection string for your Event Hubs namespace.

    1. 在“策略”下,选择“RootManageSharedAccessKey” 。Under Policy, choose RootManageSharedAccessKey.

    2. 查找主键的连接字符串。Find your primary key's connection string. 选择复制按钮,并保存连接字符串供以后使用。Choose the copy button, and save the connection string for later use.

      复制事件中心命名空间连接字符串

      提示

      若要确认连接字符串是与事件中心命名空间关联,还是与特定的事件中心关联,请确保该连接字符串不具有 EntityPath 参数。To confirm whether your connection string is associated with your Event Hubs namespace or with a specific event hub, make sure the connection string doesn't have the EntityPath parameter. 如果找到了该参数,则说明连接字符串适用于特定的事件中心“实体”,不是适用于逻辑应用的正确字符串。If you find this parameter, the connection string is for a specific Event Hub "entity" and is not the correct string to use with your logic app.

  5. 现在继续添加事件中心触发器添加事件中心操作Now continue with Add an Event Hubs trigger or Add an Event Hubs action.

添加事件中心触发器Add Event Hubs trigger

在 Azure 逻辑应用中,每个逻辑应用都必须从触发器开始,该触发器在发生特定事件或特定条件得到满足的情况下触发。In Azure Logic Apps, every logic app must start with a trigger, which fires when a specific event happens or when a specific condition is met. 每当触发器触发时,逻辑应用引擎就会创建一个逻辑应用实例并开始运行应用的工作流。Each time the trigger fires, the Logic Apps engine creates a logic app instance and starts running your app's workflow.

此示例演示在将新事件发送到事件中心时,如何启动逻辑应用工作流。This example shows how you can start a logic app workflow when new events are sent to your Event Hub.

  1. 在 Azure 门户或 Visual Studio 中创建一个空白的逻辑应用,以便打开逻辑应用设计器。In the Azure portal or Visual Studio, create a blank logic app, which opens Logic Apps Designer. 此示例使用 Azure 门户。This example uses the Azure portal.

  2. 在搜索框中,输入“事件中心”作为筛选器。In the search box, enter "event hubs" as your filter. 从触发器列表中选择此触发器:当事件在事件中心可用时 - 事件中心From the triggers list, select this trigger: When events are available in Event Hub - Event Hubs

    选择触发器

  3. 如果系统提示输入连接详细信息,请立即创建事件中心连接If you're prompted for connection details, create your Event Hubs connection now.

  4. 在触发器中,提供要监视的事件中心的相关信息。In the trigger, provide information about the Event Hub that you want to monitor. 如需更多属性,请打开“添加新参数”列表。 For more properties, open the Add new parameter list. 选择一个参数时,会将该属性添加到触发器卡。Selecting a parameter adds that property to the trigger card.

    触发器属性

    属性Property 必须Required 说明Description
    事件中心名称Event Hub name Yes 要监视的事件中心的名称The name for the Event Hub that you want to monitor
    内容类型Content type No 事件的内容类型。The event's content type. 默认为 application/octet-streamThe default is application/octet-stream.
    使用者组名称Consumer group name No 要用于读取事件的事件中心使用者组名称The name for the Event Hub consumer group to use for reading events. 如果未指定,则使用默认使用者组。If not specified, the default consumer group is used.
    最大事件计数Maximum events count No 最大事件数。The maximum number of events. 触发器返回的事件数至少为 1,至多为此属性指定的事件数。The trigger returns between one and the number of events specified by this property.
    时间间隔Interval Yes 一个正整数,描述工作流基于频率运行的频繁度A positive integer that describes how often the workflow runs based on the frequency
    频率Frequency Yes 定期计划的时间单位The unit of time for the recurrence

    其他属性Additional properties

    属性Property 必须Required 说明Description
    内容架构Content schema No 要从事件中心读取的事件的 JSON 内容架构。The JSON content schema for the events to read from the Event Hub. 例如,如果指定内容架构,则只有在这些事件符合架构时,才能触发逻辑应用。For example, if you specify the content schema, you can trigger the logic app for only those events that match the schema.
    最小分区键Minimum partition key No 输入要读取的最小分区 ID。Enter the minimum partition ID to read. 默认读取所有分区。By default, all partitions are read.
    最大分区键Maximum partition key No 输入要读取的最大分区 ID。Enter the maximum partition ID to read. 默认读取所有分区。By default, all partitions are read.
    时区Time zone No 仅当指定启动时间时才适用,因为此触发器不接受 UTC 时差。Applies only when you specify a start time because this trigger doesn't accept UTC offset. 选择要应用的时区。Select the time zone that you want to apply.

    有关详细信息,请参阅使用 Azure 逻辑应用创建和运行重复任务和工作流For more information, see Create and run recurring tasks and workflows with Azure Logic Apps.

    开始时间Start time No 按以下格式提供开始时间:Provide a start time in this format:

    如果选择了时区,则格式为 YYYY-MM-DDThh:mm:ssYYYY-MM-DDThh:mm:ss if you select a time zone

    -或--or-

    如果未选择时区,则格式为 YYYY-MM-DDThh:mm:ssZYYYY-MM-DDThh:mm:ssZ if you don't select a time zone

    有关详细信息,请参阅使用 Azure 逻辑应用创建和运行重复任务和工作流For more information, see Create and run recurring tasks and workflows with Azure Logic Apps.

  5. 完成后,请在设计器工具栏上选择“保存” 。When you're done, on the designer toolbar, choose Save.

  6. 现在请继续向逻辑应用添加一个或多个操作,以便完成需对触发器结果执行的任务。Now continue adding one or more actions to your logic app for the tasks you want to perform with the trigger results.

    例如,若要根据特定值(例如类别)来筛选事件,可以添加一个条件,这样“发送事件”操作就只发送符合条件的事件。 For example, to filter events based on a specific value, such as a category, you can add a condition so that the Send event action sends only the events that meet your condition.

备注

所有事件中心触发器都是长轮询触发器,这意味着当触发器触发时,触发器将处理所有事件,然后等待 30 秒,让更多事件出现在事件中心 。All Event Hub triggers are long-polling triggers, which means that when a trigger fires, the trigger processes all the events and then waits for 30 seconds for more events to appear in your Event Hub. 如果在 30 秒内未收到事件,则会跳过触发器运行。If no events are received in 30 seconds, the trigger run is skipped. 否则,该触发器将继续读取事件,直到事件中心为空。Otherwise, the trigger continues reading events until your Event Hub is empty. 下一次触发器轮询的发生将基于触发器的属性中指定的重复周期间隔。The next trigger poll happens based on the recurrence interval that you specify in the trigger's properties.

添加事件中心操作Add Event Hubs action

在 Azure 逻辑应用中,操作是指工作流中紧跟在某个触发器或另一操作后面执行的一个步骤。In Azure Logic Apps, an action is a step in your workflow that follows a trigger or another action. 就此示例来说,逻辑应用一开始使用事件中心触发器来检查事件中心的新事件。For this example, the logic app starts with an Event Hubs trigger that checks for new events in your Event Hub.

  1. 在 Azure 门户或 Visual Studio 的逻辑应用设计器中打开逻辑应用。In the Azure portal or Visual Studio, open your logic app in Logic Apps Designer. 此示例使用 Azure 门户。This example uses the Azure portal.

  2. 在该触发器或操作下,选择“新建步骤” 。Under the trigger or action, choose New step.

    若要在现有步骤之间添加操作,请将鼠标移到连接箭头上方。To add an action between existing steps, move your mouse over the connecting arrow. 选择出现的加号 ( + ),然后选择“添加操作”。 Choose the plus sign (+) that appears, and then select Add an action.

  3. 在搜索框中,输入“事件中心”作为筛选器。In the search box, enter "event hubs" as your filter. 在操作列表中选择此操作:发送事件 - 事件中心From the actions list, select this action: Send event - Event Hubs

    选择“发送事件”操作

  4. 如果系统提示输入连接详细信息,请立即创建事件中心连接If you're prompted for connection details, create your Event Hubs connection now.

  5. 在操作中,提供要发送的事件的相关信息。In the action, provide information about the events that you want to send. 如需更多属性,请打开“添加新参数”列表。 For more properties, open the Add new parameter list. 选择一个参数时,会将该属性添加到操作卡。Selecting a parameter adds that property to the action card.

    选择事件中心名称并提供事件内容

    属性Property 必须Required 说明Description
    事件中心名称Event Hub name Yes 要在其中发送事件的事件中心The Event Hub where you want to send the event
    内容Content No 要发送的事件内容The content for the event you want to send
    属性Properties No 要发送的应用属性和值The app properties and values to send
    分区键Partition key No 分区 ID,表明应该向何处发送事件The partition ID for where to send the event

    例如,可以将输出从事件中心触发器发送到另一事件中心:For example, you can send the output from your Event Hubs trigger to another Event Hub:

    发送事件示例

  6. 完成后,请在设计器工具栏上选择“保存” 。When you're done, on the designer toolbar, choose Save.

连接到事件中心Connect to your Event Hub

要使逻辑应用能够访问某个服务,必须在逻辑应用与该服务之间创建连接。Before your logic app can access any service, you must create a connection between your logic app and that service. 如果以前未创建此连接,则在向逻辑应用添加该服务的触发器或操作时,系统会提示输入连接信息。If you didn't previously create this connection, you're prompted for connection information when you add a trigger or action for that service to your logic app. 可以使用逻辑应用设计器轻松地直接从逻辑应用创建此连接。The Logic Apps Designer provides an easy way for you to create this connection directly from your logic app.

  1. 系统提示输入连接信息时,请提供以下详细信息:When you're prompted for connection information, provide these details:

    属性Property 必须Required ValueValue 说明Description
    连接名称Connection Name Yes <connection-name><connection-name> 将要为连接创建的名称The name to create for your connection
    事件中心命名空间Event Hubs Namespace Yes <event-hubs-namespace><event-hubs-namespace> 选择要使用的事件中心命名空间。Select the Event Hubs namespace you want to use.

    例如:For example:

    创建事件中心连接

    若要手动输入连接字符串,请选择“手动输入连接信息” 。To manually enter the connection string, select Manually enter connection information. 了解如何查找连接字符串Learn how to find your connection string.

  2. 选择要使用的事件中心策略(如果尚未选择)。Select the Event Hubs policy to use, if not already selected. 选择“创建”。 Choose Create.

    创建事件中心连接(第 2 部分)

  3. 创建连接后,继续添加事件中心触发器添加事件中心操作After you create your connection, continue with Add Event Hubs trigger or Add Event Hubs action.

连接器参考Connector reference

如需技术详细信息(例如触发器、操作和限制,如连接器的 Swagger 文件所述),请查看连接器的参考页For technical details, such as triggers, actions, and limits, as described by the connector's Swagger file, see the connector's reference page.

后续步骤Next steps