你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
开始使用 Azure IoT 操作 SDK 进行开发。 按照以下步骤设置用于生成和运行示例的开发环境,以及创建和测试自己的 Azure IoT 操作高可用性边缘应用程序。
GitHub repository | .NET sdk | Go SDK | Rust SDK
先决条件
在开始之前,需满足以下先决条件:
- Azure订阅。 如果还没有 Azure 订阅,可以在开始前创建一个免费帐户。
GitHub 帐户。
Azure访问权限。 有关详细信息,请参阅 部署概述 > 所需的权限。
设置
使用 Azure IoT 操作 SDK 进行开发需要一个部署了 Azure IoT 操作 的 Kubernetes 群集。 通过进一步配置,可以直接从开发人员环境访问 MQTT 代理。
重要
以下开发环境设置选项使用 K3s 在 K3d 中运行,以构建轻型 Kubernetes 集群。 使用 测试设置来部署 Azure IoT 操作。 对于生产部署,请选择安全设置。
若要使用安全设置,请按照 准备启用 Azure Arc 的 Kubernetes 群集中的说明在 Ubuntu 上创建 K3s 群集,并使用 在生产群集上部署 Azure IoT 操作来进行安全设置的部署。 然后继续配置 Azure IoT 操作 用于开发。
GitHub Codespaces 提供最简化的体验,可以在几分钟内启动和运行开发环境。
部署Azure IoT 操作
您将对在上一步中创建的开发群集启用 Arc 并使用 test 设置 部署 Azure IoT 操作。
打开新的 Bash 终端并完成以下步骤:
转到存储库根目录:
cd <REPOSITORY ROOT>运行
install-aio-arc.sh脚本使群集启用 Arc 并部署 Azure IoT 操作。 将占位符替换为你的实际值:参数 价值 LOCATION距你较近的 Azure 区域。 有关当前支持区域的列表,请参阅支持的区域。 RESOURCE_GROUP新的 Azure 资源组的名称,该资源组将用于创建您的群集。 CLUSTER_NAMEKubernetes 群集的名称。 STORAGE_ACCOUNT_NAME存储帐户的名称。 存储帐户名的长度必须为 3 到 24 个字符,并且只能包含数字和小写字母。 SCHEMA_REGISTRY_NAME架构注册表的名称。 架构注册表名称只能包含数字、小写字母和连字符。 SCHEMA_REGISTRY_NAMESPACE架构注册表命名空间的名称。 命名空间唯一标识租户内的架构注册表。 架构注册表命名空间名称只能包含数字、小写字母和连字符。 DEVICE_REGISTRY_NAMESPACE设备注册表命名空间的名称。 该名称在您的租户内必须唯一,且长度需在 3 到 24 个字符之间。 只能包含数字、字母、连字符和下划线。 ./tools/deployment/install-aio-arc.sh -l <LOCATION> -g <RESOURCE_GROUP> -c <CLUSTER_NAME> -s <STORAGE_ACCOUNT_NAME> -r <SCHEMA_REGISTRY_NAME> -n <SCHEMA_REGISTRY_NAMESPACE> -d <DEVICE_REGISTRY_NAMESPACE>此脚本运行以下命令:
- 登录到Azure CLI
- 创建资源组
- 注册必需的 Azure 提供程序
- 将 Kubernetes 群集连接到 Azure Arc
- 启用Azure Arc功能
- 创建Azure 存储帐户
- 创建Azure IoT 操作架构注册表
- 创建 Azure 设备注册表命名空间
- 初始化Azure IoT操作
- 创建Azure IoT 操作实例
部署完成后,使用 az iot ops check 来评估 Azure IoT 操作 服务部署的运行状况、配置和可用性。 check 命令可帮助你在部署和配置中发现问题。
az iot ops check
配置用于开发的 Azure IoT 操作
部署 Azure IoT 操作 之后,将其配置为开发环境。 设置 MQTT 中转站和身份验证方法。 请确保为开发环境设置必要的环境变量:
转到存储库根目录:
cd <REPOSITORY ROOT>运行
configure-aio.sh脚本以配置用于开发的Azure IoT 操作:./tools/deployment/configure-aio.sh此脚本运行以下命令:
- 设置证书服务(如果缺失)
- 为 x509 身份验证创建根 CA 和中间 CA
- 为代理创建用于身份验证 x509 客户端的信任包 ConfigMap
- 配置
BrokerListener和BrokerAuthentication资源以用于SAT和x509身份验证
测试安装
若要测试设置,请使用 mosquitto_pub 连接到 MQTT 代理并验证 x509 证书、SAT 和信任捆绑包。
导出
.session目录:export SESSION=$(git rev-parse --show-toplevel)/.session测试无 TLS,无身份验证:
mosquitto_pub -L mqtt://localhost:1883/hello -m world --debug使用 x509 身份验证测试 TLS:
mosquitto_pub -L mqtts://localhost:8883/hello -m world --cafile $SESSION/broker-ca.crt --cert $SESSION/client.crt --key $SESSION/client.key --debug使用 SAT 身份验证测试 TLS:
mosquitto_pub -L mqtts://localhost:8884/hello -m world --cafile $SESSION/broker-ca.crt -D CONNECT authentication-method K8S-SAT -D CONNECT authentication-data $(cat $SESSION/token.txt) --debug
运行示例
此示例使用 Telemetry 和 remote 过程调用 (RPC) 演示客户端和服务器之间的简单通信。 服务器会跟踪一个计数器的值,并接受来自客户端的 RPC 请求,以读取或递增该计数器。
此示例将 v2 协议编译器与 WoT Thing 模型文件配合使用,请参阅 TestThing 文件夹。
安装 .NET 9.0 SDK。
Azure IoT 操作 SDK GitHub存储库中的示例从环境变量读取配置。 存储库根目录提供一个
.env文件,该文件导出样本中用于连接到 MQTT 代理的变量。 编辑.env文件以设置环境的值,或使用文件中提供的默认值。导航到
CounterServer示例目录:cd <REPOSITORY ROOT>/dotnet/samples/Protocol/RPC/CounterServer/生成示例:
dotnet build运行示例:
source `git rev-parse --show-toplevel`/.env; export AIO_MQTT_CLIENT_ID=counter-server; dotnet run打开一个新 shell 并导航到
CounterClient示例目录:cd <REPOSITORY ROOT>/dotnet/samples/Protocol/RPC/CounterClient/生成示例:
dotnet build运行示例:
source `git rev-parse --show-toplevel`/.env; export AIO_MQTT_CLIENT_ID=counter-client; export COUNTER_SERVER_ID=counter-server; dotnet run你会看到客户端和服务器通信,客户端发送读取和递增计数器值的请求,服务器发送遥测数据。 详情请参见 “检查客户端和服务器输出”。
示例
CounterClient在完成时自动退出。 还可以通过在其终端中按CounterServer来停止Ctrl+C示例。
检查客户端和服务器输出
本节展示了客户端和服务器的输出示例。 客户端调用读取并递增计数器值,服务器响应。 服务器还输出遥测数据,因此客户端可以跟踪计数器值。 在这些例子中,消息按逻辑分组,但输出中的消息顺序会根据客户端和服务器的时序变化。 这些示例显示了 MQTT 消息的输出,但你可以通过在客户端和服务器的 appsettings.json 文件中将 mqttDiag 环境变量设置为 false 来禁止显示这些输出。
读取计数器
以下示例展示了读取计数器的输出消息。 客户端和服务器之间的请求和响应通过相关ID匹配,在本例中为 aaaa0000-bb11-2222-33cc-444444dddddd。
客户输出:
# Subscribe once on startup.
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4663363Z] [11]: TX (51 bytes) >>> Subscribe: [PacketIdentifier=2] [TopicFilters=clients/+/rpc/command-samples/+/readCounter@AtLeastOnce]
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4689212Z] [6]: RX (6 bytes) <<< SubAck: [PacketIdentifier=2] [ReasonCode=GrantedQoS1]
CounterClient Information: 0 : Subscribed to topic filter 'clients/+/rpc/command-samples/+/readCounter' for command invoker 'readCounter'
# Invoke readCounter once on startup and once at end -- only one shown.
info: CounterClient.RpcCommandRunner[0] Calling ReadCounter with aaaa0000-bb11-2222-33cc-444444dddddd
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4777793Z] [11]: TX (335 bytes) >>> Publish: [Topic=rpc/command-samples/counter-server/readCounter] [PayloadLength=0] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=3]
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4818287Z] [10]: RX (4 bytes) <<< PubAck: [PacketIdentifier=3] [ReasonCode=Success]
CounterClient Information: 0 : Invoked command 'readCounter' with correlation ID aaaa0000-bb11-2222-33cc-444444dddddd to topic 'rpc/command-samples/counter-server/readCounter'
CounterClient Information: 0 : >> [2026-07-29T17:35:53.5861515Z] [10]: RX (260 bytes) <<< Publish: [Topic=clients/counter-client/rpc/command-samples/counter-server/readCounter] [PayloadLength=21] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=1]
info: CounterClient.RpcCommandRunner[0] called read 0 with id aaaa0000-bb11-2222-33cc-444444dddddd
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6572000Z] [10]: TX (4 bytes) >>> PubAck: [PacketIdentifier=1] [ReasonCode=Success]
服务器输出:
# Subscribe to readCounter topic on startup.
CounterServer Information: 0 : >> [2026-07-29T17:35:12.3456281Z] [7]: TX (54 bytes) >>> Subscribe: [PacketIdentifier=1] [TopicFilters=rpc/command-samples/counter-server/readCounter@AtLeastOnce]
CounterServer Information: 0 : >> [2026-07-29T17:35:12.3553742Z] [11]: RX (6 bytes) <<< SubAck: [PacketIdentifier=1] [ReasonCode=GrantedQoS1]
CounterServer Information: 0 : Command executor for 'readCounter' started.
# Multiple (2) invocations occur - only one shown.
CounterServer Information: 0 : >> [2026-07-29T17:35:53.4842146Z] [11]: RX (334 bytes) <<< Publish: [Topic=rpc/command-samples/counter-server/readCounter] [PayloadLength=0] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=1]
<6>17:35:53CounterServer.CounterService[0] --> Executing Counter.ReadCounter with id aaaa0000-bb11-2222-33cc-444444dddddd for counter-client
<6>17:35:53CounterServer.CounterService[0] --> Executed Counter.ReadCounter with id aaaa0000-bb11-2222-33cc-444444dddddd for counter-client
CounterServer Information: 0 : >> [2026-07-29T17:35:53.5815820Z] [7]: TX (261 bytes) >>> Publish: [Topic=clients/counter-client/rpc/command-samples/counter-server/readCounter] [PayloadLength=21] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=4]
CounterServer Information: 0 : >> [2026-07-29T17:35:53.5854665Z] [11]: RX (4 bytes) <<< PubAck: [PacketIdentifier=4] [ReasonCode=Success]
CounterServer Information: 0 : >> [2026-07-29T17:35:53.5881430Z] [12]: TX (4 bytes) >>> PubAck: [PacketIdentifier=1] [ReasonCode=Success]
增量计数器
以下示例展示了递增计数器的输出消息。 请求和响应通过相关ID在客户端和服务器之间匹配,在本例中为 bbbb1111-cc22-3333-44dd-555555eeeeee。
客户输出:
# Invoke increment multiple times -- only one shown.
info: CounterClient.RpcCommandRunner[0] calling counter.incr with id bbbb1111-cc22-3333-44dd-555555eeeeee
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6375578Z] [11]: TX (49 bytes) >>> Subscribe: [PacketIdentifier=4] [TopicFilters=clients/+/rpc/command-samples/+/increment@AtLeastOnce]
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6399406Z] [10]: RX (6 bytes) <<< SubAck: [PacketIdentifier=4] [ReasonCode=GrantedQoS1]
CounterClient Information: 0 : Subscribed to topic filter 'clients/+/rpc/command-samples/+/increment' for command invoker 'increment'
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6461370Z] [11]: TX (372 bytes) >>> Publish: [Topic=rpc/command-samples/counter-server/increment] [PayloadLength=20] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=21]
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6552803Z] [10]: RX (4 bytes) <<< PubAck: [PacketIdentifier=21] [ReasonCode=Success]
CounterClient Information: 0 : Invoked command 'increment' with correlation ID bbbb1111-cc22-3333-44dd-555555eeeeee to topic 'rpc/command-samples/counter-server/increment'
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6960747Z] [13]: RX (259 bytes) <<< Publish: [Topic=clients/counter-client/rpc/command-samples/counter-server/increment] [PayloadLength=22] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=12]
info: CounterClient.RpcCommandRunner[0] called counter.incr 10 with id bbbb1111-cc22-3333-44dd-555555eeeeee
CounterClient Information: 0 : >> [2026-07-29T17:35:53.7138884Z] [10]: TX (4 bytes) >>> PubAck: [PacketIdentifier=12] [ReasonCode=Success]
服务器输出:
# Subscribe to increment once on startup.
CounterServer Information: 0 : >> [2026-07-29T17:35:12.3548320Z] [7]: TX (52 bytes) >>> Subscribe: [PacketIdentifier=2] [TopicFilters=rpc/command-samples/counter-server/increment@AtLeastOnce]
CounterServer Information: 0 : >> [2026-07-29T17:35:12.3613847Z] [11]: RX (6 bytes) <<< SubAck: [PacketIdentifier=2] [ReasonCode=GrantedQoS1]
CounterServer Information: 0 : Command executor for 'increment' started.
# Multiple invocations are received -- only one shown.
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6502886Z] [13]: RX (371 bytes) <<< Publish: [Topic=rpc/command-samples/counter-server/increment] [PayloadLength=20] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=2]
<6>17:35:53CounterServer.CounterService[0] --> Executing Counter.Increment with id bbbb1111-cc22-3333-44dd-555555eeeeee for counter-client
<6>17:35:53CounterServer.CounterService[0] --> Executed Counter.Increment with id bbbb1111-cc22-3333-44dd-555555eeeeee for counter-client
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6924040Z] [7]: TX (260 bytes) >>> Publish: [Topic=clients/counter-client/rpc/command-samples/counter-server/increment] [PayloadLength=22] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=15]
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6983722Z] [18]: RX (4 bytes) <<< PubAck: [PacketIdentifier=15] [ReasonCode=Success]
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6992783Z] [12]: TX (4 bytes) >>> PubAck: [PacketIdentifier=2] [ReasonCode=Success]
CounterValue 遥测
以下示例展示了CounterValue遥测的输出消息。 服务器输出会先显示,因为它发送遥测数据。
服务器输出:
# Telemetry data is published multiple times.
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6805980Z] [7]: TX (203 bytes) >>> Publish: [Topic=telemetry/telemetry-samples/counterValue] [PayloadLength=18] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=5]
CounterServer Information: 0 : >> [2026-07-29T17:35:53.6843445Z] [11]: RX (4 bytes) <<< PubAck: [PacketIdentifier=5] [ReasonCode=Success]
CounterServer Information: 0 : Telemetry sent successfully to the topic 'telemetry/telemetry-samples/counterValue'
客户输出:
# Subscribe performed once on startup.
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4283758Z] [11]: TX (48 bytes) >>> Subscribe: [PacketIdentifier=1] [TopicFilters=telemetry/telemetry-samples/counterValue@AtLeastOnce]
CounterClient Information: 0 : >> [2026-07-29T17:35:53.4355484Z] [10]: RX (6 bytes) <<< SubAck: [PacketIdentifier=1] [ReasonCode=GrantedQoS1]
CounterClient Information: 0 : Telemetry receiver subscribed for topic telemetry/telemetry-samples/counterValue.
# Multiple telemetry values received - only one shown.
CounterClient Information: 0 : >> [2026-07-29T17:35:53.6820692Z] [6]: RX (202 bytes) <<< Publish: [Topic=telemetry/telemetry-samples/counterValue] [PayloadLength=18] [QoSLevel=AtLeastOnce] [Dup=False] [Retain=False] [PacketIdentifier=2]
CounterClient Information: 0 : Telemetry received from telemetry/telemetry-samples/counterValue
info: CounterClient.CounterClient[0] Telemetry received from counter-server: CounterValue=4
CounterClient Information: 0 : >> [2026-07-29T17:35:53.7131604Z] [10]: TX (4 bytes) >>> PubAck: [PacketIdentifier=2] [ReasonCode=Success]
配置摘要
MQTT 代理配置
安装软件后,群集包含以下 MQTT 中转站定义:
| 组件类型 | Name | Description |
|---|---|---|
Broker |
默认 | MQTT 代理 |
BrokerListener |
默认 | 提供对 MQTT 代理的群集访问权限 |
BrokerListener |
default-external | 提供对 MQTT 代理的群集外访问 |
BrokerAuthentication |
默认 | SAT 身份验证定义 |
BrokerAuthentication |
default-x509 | x509 身份验证定义 |
MQTT 代理访问
可以使用下表中所述的连接信息来访问群集和群集外的 MQTT 中转站。 有关配置应用程序时要使用的环境变量的信息,请参阅 Connection Settings。
注释
根据设置,访问群集外 MQTT 代理时的主机名可能与 localhost 不同。
| Hostname | Authentication | TLS | 群集内端口 | 群集外端口 |
|---|---|---|---|---|
aio-broker |
SAT | ✅ | 18883 |
- |
localhost |
None | ❌ | 1883 |
1883 |
localhost |
x509 | ✅ | 8883 |
8883 |
localhost |
SAT | ✅ | 8884 |
8884 |
开发项目
作为部署脚本的一部分,本地环境会创建以下文件,以方便与 MQTT 中转站的连接和身份验证。 可以在存储库根目录中找到这些文件 .session 。
| 文件 | Description |
|---|---|
broker-ca.crt |
在端口 8883 和 8884 上验证 MQTT 代理所需的 MQTT 代理信任捆绑包 |
token.txt |
用于对 MQTT 代理进行身份验证的服务身份验证令牌 (SAT)8884 |
client.crt |
用于在端口上使用 MQTT 代理进行身份验证的 x509 客户端证书 8883 |
client.key |
用于通过端口 8883 上的 MQTT 代理进行身份验证的 x509 客户端私钥 |
Troubleshooting
请查看故障排除指南,了解 Azure IoT 操作 SDK GitHub 存储库中的常见问题。
后续步骤
在本快速入门中,你将设置Azure IoT 操作 SDK 并运行示例应用程序。 若要了解有关使用 SDK 进行开发的详细信息,请查看以下资源: