对设备到云的消息使用消息路由和自定义终结点Use message routes and custom endpoints for device-to-cloud messages

Note

本文中提到的某些功能(例如云到设备消息传递、设备孪生、设备管理)仅在 IoT 中心的标准层中提供。Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only available in the standard tier of IoT hub. 有关基本和标准 IoT 中心层的详细信息,请参阅如何选择合适的 IoT 中心层For more information about the basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.

借助 IoT 中心消息路由,用户可将设备到云的消息路由到面向服务的终结点。IoT Hub Message Routing enables users to route device-to-cloud messages to service-facing endpoints. 路由还提供了一种查询功能,让你先筛选数据,再将其路由到终结点。Routing also provides a querying capability to filter the data before routing it to the endpoints. 所配置的每个路由查询包含以下属性:Each routing query you configure has the following properties:

属性Property 说明Description
名称Name 用于标识查询的唯一名称。The unique name that identifies the query.
Source Source 要处理的数据流的来源。The origin of the data stream to be acted upon. 例如,设备遥测。For example, device telemetry.
条件Condition 针对消息应用程序属性、系统属性、消息正文、设备孪生标记和设备孪生属性运行的路由查询的查询表达式,用于确定该查询是否是终结点的匹配项。The query expression for the routing query that is run against the message application properties, system properties, message body, device twin tags, and device twin properties to determine if it is a match for the endpoint. 要详细了解如何构造查询,请参阅消息路由查询语法For more information about constructing a query, see the see message routing query syntax
终结点Endpoint IoT 中心将匹配查询的消息发送到的终结点的名称。The name of the endpoint where IoT Hub sends messages that match the query. 建议所选终结点与 IoT 中心位于同一区域。We recommend that you choose an endpoint in the same region as your IoT hub.

一条消息可能与多个路由查询中的条件匹配,在这种情况下,IoT 中心会将该消息传递到与每个匹配查询关联的终结点。A single message may match the condition on multiple routing queries, in which case IoT Hub delivers the message to the endpoint associated with each matched query. IoT 中心还会自动删除重复的消息传递,因此如果消息与具有相同目标的多个查询匹配,仅将其写入该目标位置一次。IoT Hub also automatically deduplicates message delivery, so if a message matches multiple queries that have the same destination, it is only written once to that destination.

终结点和路由Endpoints and routing

IoT 中心有一个默认的内置终结点An IoT hub has a default built-in endpoint. 通过将订阅中的其他服务链接到中心,可以创建要将消息路由到的自定义终结点。You can create custom endpoints to route messages to by linking other services in your subscription to the hub. IoT 中心目前支持将 Azure 存储容器、事件中心、服务总线队列和服务总线主题用作自定义终结点。IoT Hub currently supports Azure Storage containers, Event Hubs, Service Bus queues, and Service Bus topics as custom endpoints.

使用路由和自定义终结点时,如果消息不与任何查询匹配,则只将其传送到内置终结点。When you use routing and custom endpoints, messages are only delivered to the built-in endpoint if they don't match any query. 若要将消息传递到内置终结点以及自定义终结点,请添加用于将消息发送到内置事件终结点的路由 。To deliver messages to the built-in endpoint as well as to a custom endpoint, add a route that sends messages to the built-in events endpoint.

Note

  • IoT 中心仅支持将数据作为 blob 写入 Azure 存储容器。IoT Hub only supports writing data to Azure Storage containers as blobs.
  • 不支持将已启用会话重复项检测的服务总线队列和主题用作自定义终结点。Service Bus queues and topics with Sessions or Duplicate Detection enabled are not supported as custom endpoints.

有关在 IoT 中心创建自定义终结点的详细信息,请参阅 IoT 中心终结点For more information about creating custom endpoints in IoT Hub, see IoT Hub endpoints.

有关从自定义终结点读取数据的详细信息,请参阅:For more information about reading from custom endpoints, see:

后续步骤Next steps

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