Azure IoT Edge 客户端模块无法在主机模式下连接



正如初学者 IoT Edge 开发人员所期望的那样,我开始按照有关如何将自定义代码部署到 IoT 边缘服务器的Microsoft教程教程:为 Linux 设备开发 C# IoT Edge 模块。

我能够在工业英特尔PC上安装Ubuntu 18.04服务器,下载和配置edgeHub和edgeAgent模块,创建一个免费的Azure和Docker Hub帐户等。甚至教程中的示例代码也已成功构建并部署到我的目标设备上,一切似乎都很好,所有模块都在相互通信,等等。

运行演示代码后,我开始修改代码以更好地适应我的最终目标,即捕获我们的工业设备生成的网络流量,这些设备使用 UDP 组播/广播。显然,我需要将我的 docker 映像配置为在"主机"网络模式下运行。事实上,一旦我告诉 azure edgeAgent 在主机模式下启动模块容器,UDP 数据包就开始进来。

但是,现在我的示例模块不再能够连接到 IoT 中心,我完全不知所措。我也尝试在主机模式下运行 edgeHub 容器,这似乎没有任何区别。由于ModuleClient.OpenAsync方法,我得到的确切错误是:

Unhandled exception. System.AggregateException: One or more errors occurred. (Transient network error occurred, please retry.)
---> Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException: Transient network error occurred, please retry.
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name or service not known
at System.Net.Dns.InternalGetHostByName(String hostName)
at System.Net.Dns.ResolveCallback(Object context)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
at System.Net.Dns.EndGetHostAddresses(IAsyncResult asyncResult)
at System.Net.Dns.<>c.<GetHostAddressesAsync>b__25_1(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Azure.Devices.Client.Transport.Mqtt.MqttTransportHandler.OpenAsyncInternal(CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Transport.Mqtt.MqttTransportHandler.OpenAsync(CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Transport.ProtocolRoutingDelegatingHandler.OpenAsync(CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<>c__DisplayClass23_0.<<ExecuteWithErrorHandlingAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.ExecuteWithErrorHandlingAsync[T](Func`1 asyncOperation)
--- End of inner exception stack trace ---
at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.ExecuteWithErrorHandlingAsync[T](Func`1 asyncOperation)
at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.<>c__DisplayClass33_0.<<OpenInternalAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.EnsureOpenedAsync(CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.InternalClient.OpenAsync()
at SampleModule.Program.Init() in /app/SampleModule/Program.cs:line 54
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at SampleModule.Program.Main(String[] args) in /app/SampleModule/Program.cs:line 26

用于连接的代码:

ITransportSettings[] settings = { mqttSetting };
ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
await ioTHubModuleClient.OpenAsync();
Console.WriteLine("IoT Hub module client initialized.");

所以显然模块客户端再也找不到edgeHub了(名称或服务未知?我知道"CreateFromEnvironmentAsync"使用edgeAgent设置的环境变量,但是哪些变量呢?有人知道这是为什么吗?即使是指向开始调试此问题的正确方向的指针也非常感谢!

如果我将 docker 容器改回"桥接"网络,OpenAsync 方法可以完美运行,但我的 UDP 广播消息当然不再被接收。

经过一番挖掘,我自己找到了解决方案。

由于暂时性网络错误,模块无法重新启动

这篇文章,虽然我已经读了很多遍,但建议/etc/hosts 文件有问题。果然,我的设备(127.0.1.1(的条目有问题,在此过程中我必须更改了设备的主机名,并且此更改未反映在主机文件中。

自从那次更改以来,我让 AMQP 和 MQTT 都可以工作,但是两者都使用 edgeHub 容器也在主机模式下运行。在桥接模式下,我遇到了一个新问题,即 docker-proxy 没有绑定导出的端口(这导致了新的"连接被拒绝"异常(。为什么...

最新更新