从内置终结点读取设备到云的消息Read device-to-cloud messages from the built-in endpoint

默认情况下,消息将路由到与事件中心兼容的内置面向服务的终结点 (messages/events) 中。By default, messages are routed to the built-in service-facing endpoint (messages/events) that is compatible with Event Hubs. 目前仅在端口 5671 上使用 AMQP 协议公开此终结点。This endpoint is currently only exposed using the AMQP protocol on port 5671. IoT 中心公开以下属性,以便用户控制内置的与事件中心兼容的消息传送终结点 messages/eventsAn IoT hub exposes the following properties to enable you to control the built-in Event Hub-compatible messaging endpoint messages/events.

属性Property 说明Description
分区计数Partition count 在创建时设置此属性,以便为设备到云事件引入定义分区数。Set this property at creation to define the number of partitions for device-to-cloud event ingestion.
保留时间Retention time 此属性指定 IoT 中心保留消息的时间(以天为单位)。This property specifies how long in days messages are retained by IoT Hub. 默认值为一天,但可以增加到七天。The default is one day, but it can be increased to seven days.

IoT 中心允许将数据保留在内置的事件中心,最长保留期为 7 天。IoT Hub allows data retention in the built-in Event Hubs for a maximum of 7 days. 可以在创建 IoT 中心时设置保留时间。You can set the retention time during creation of your IoT Hub. IoT 中心的数据保留时间取决于 IoT 中心层和单元类型。Data retention time in IoT Hub depends on your IoT hub tier and unit type. 就大小而言,内置事件中心可以保留最大消息大小至少为 24 小时配额的消息。In terms of size, the built-in Event Hubs can retain messages of the maximum message size up to at least 24 hours of quota. 例如,使用 1 个 S1 单元时,在每条消息的大小为 4k 的情况下,IoT 中心提供的存储足以保留至少 40 万条消息。For example, for 1 S1 unit IoT Hub provides enough storage to retain at least 400K messages of 4k size each. 如果设备发送的消息较小,则这些消息可能会保留更长的时间(最多 7 天),具体取决于使用的存储。If your devices are sending smaller messages, they may be retained for longer (up to 7 days) depending on how much storage is consumed. 我们保证至少在指定的保留期内保留数据。We guarantee retaining the data for the specified retention time as a minimum. 保留时间过后,消息将过期并且将无法访问。Messages will expire and will not be accessible after the retention time has passed.

IoT 中心还支持用户管理内置设备到云接收终结点上的使用者组。IoT Hub also enables you to manage consumer groups on the built-in device-to-cloud receive endpoint. 每个 IoT 中心最多可以有 20 个使用者组。You can have up to 20 consumer groups for each IoT Hub.

如果使用消息路由,并启用了回退路由,则与任何路由上的查询不匹配的所有消息都会写入内置终结点。If you're using message routing and the fallback route is enabled, all messages that don't match a query on any route go to the built-in endpoint. 如果禁用此回退路由,将删除与任何查询都不匹配的消息。If you disable this fallback route, messages that don't match any query are dropped.

可以使用 IoT 中心资源提供程序 REST API 以编程方式修改保留期时间,或通过 Azure 门户进行修改。You can modify the retention time, either programmatically using the IoT Hub resource provider REST APIs, or with the Azure portal.

IoT 中心向后端服务公开 messages/events 内置终结点,让后端服务读取中心收到的设备到云消息。IoT Hub exposes the messages/events built-in endpoint for your back-end services to read the device-to-cloud messages received by your hub. 该终结点与事件中心兼容,因此可以使用事件中心服务支持的任何机制读取消息。This endpoint is Event Hub-compatible, which enables you to use any of the mechanisms the Event Hubs service supports for reading messages.

从内置终结点读取信息Read from the built-in endpoint

某些产品集成和事件中心 SDK 可以感知 IoT 中心,并让你使用 IoT 中心服务连接字符串连接到内置的终结点。Some product integrations and Event Hubs SDKs are aware of IoT Hub and let you use your IoT hub service connection string to connect to the built-in endpoint.

使用无法感知 IoT 中心的事件中心 SDK 或产品集成时,需要一个与事件中心兼容的终结点以及与事件中心兼容的名称。When you use Event Hubs SDKs or product integrations that are unaware of IoT Hub, you need an Event Hub-compatible endpoint and Event Hub-compatible name. 可以从门户检索这些值,如下所示:You can retrieve these values from the portal as follows:

  1. 登录 [Azure 门户][lnk-management-portal]并导航到 IoT 中心。Sign in to the [Azure portal][lnk-management-portal] and navigate to your IoT hub.

  2. 单击“内置终结点”。Click Built-in endpoints.

  3. “事件”部分包含以下值:“分区”、“与事件中心兼容的名称”、“与事件中心兼容的终结点”、“保留时间”、“使用者组”。 The Events section contains the following values: Partitions, Event Hub-compatible name, Event Hub-compatible endpoint, Retention time, and Consumer groups.

    设备到云的设置

在门户中,“与事件中心兼容的终结点”字段包含完整的事件中心连接字符串,如下所示:Endpoint=sb://abcd1234namespace.servicebus.chinacloudapi.cn/;SharedAccessKeyName=iothubowner;SharedAccessKey=keykeykeykeykeykey=;EntityPath=iothub-ehub-abcd-1234-123456In the portal, the Event Hub-compatible endpoint field contains a complete Event Hubs connection string that looks like: Endpoint=sb://abcd1234namespace.servicebus.chinacloudapi.cn/;SharedAccessKeyName=iothubowner;SharedAccessKey=keykeykeykeykeykey=;EntityPath=iothub-ehub-abcd-1234-123456. 如果所用 SDK 需求其他值,则这些值将会是:If the SDK you're using requires other values, then they would be:

名称Name ValueValue
终结点Endpoint sb://abcd1234namespace.servicebus.chinacloudapi.cn/sb://abcd1234namespace.servicebus.chinacloudapi.cn/
主机名Hostname abcd1234namespace.servicebus.chinacloudapi.cnabcd1234namespace.servicebus.chinacloudapi.cn
命名空间Namespace abcd1234namespaceabcd1234namespace

然后,可以从下拉菜单中选择任何共享访问策略,如上面的屏幕截图中所示。You can then choose any shared access policy from the drop-down as shown in the screenshot above. 它仅显示可用于连接到指定事件中心的具有“ServiceConnect”权限的策略。It only shows policies that have the ServiceConnect permissions to connect to the specified Event Hub.

可以用来连接到内置的、与事件中心兼容的且由 IoT 中心公开的终结点的 SDK 包括:The SDKs you can use to connect to the built-in Event Hub-compatible endpoint that IoT Hub exposes include:

语言Language SDKSDK 示例Example
.NET.NET https://www.nuget.org/packages/Azure.Messaging.EventHubs 快速入门Quickstart
JavaJava https://mvnrepository.com/artifact/com.azure/azure-messaging-eventhubs 快速入门Quickstart
Node.jsNode.js https://www.npmjs.com/package/@azure/event-hubs 快速入门Quickstart
PythonPython https://pypi.org/project/azure-eventhub/ 快速入门Quickstart

可以与内置的、与事件中心兼容的且由 IoT 中心公开的终结点配合使用的产品集成包括:The product integrations you can use with the built-in Event Hub-compatible endpoint that IoT Hub exposes include:

后续步骤Next steps

  • 有关 IoT 中心终结点的详细信息,请参阅 IoT 中心终结点For more information about IoT Hub endpoints, see IoT Hub endpoints.

  • 快速入门介绍如何从模拟设备发送设备到云的消息,以及如何从内置终结点读取消息。The Quickstarts show you how to send device-to-cloud messages from simulated devices and read the messages from the built-in endpoint.

有关更多详细信息,请参阅使用路由处理 IoT 中心设备到云的消息教程。For more detail, see the Process IoT Hub device-to-cloud messages using routes tutorial.