python - 无法访问 Azure IoT Edge API 模块。请求超时
问题描述
我正在安装在 Ubuntu hyper-v VM 上的 Azure IoT Edge 运行时实现 API。我在 Ubuntu vm 的边缘运行时中实现并运行了一个烧瓶 API 模块。所以我需要从主机访问这个 API,这是我在 Windows 10 上运行的本地机器。运行的 API 模块的 IP 地址是http://172.20.36.197:5000/predict。
这适用于来宾(Ubuntu hyper-v VM)机器,但每次我尝试在主机中调用此调用时请求都会超时。我什至禁用了主机 VM 的防火墙。但没有运气。似乎我必须从我的主机访问在 Ubuntu VM 上运行的物联网边缘运行时。
我只是在 docker 容器中运行相同的烧瓶 API,而不是在 IoT 边缘运行时作为边缘模块,而是在 Ubuntu Hyper-v VM 内的 docker 中运行,我可以从主机访问该 API,没有任何问题。以下是我在物联网边缘运行时运行的烧瓶 API 边缘模块的代码
import os
from azure.iot.device.aio import IoTHubModuleClient
from flask import Flask
port =int(os.environ.get("port",5000))
print("port is ",port)
# Initialize flask app
app = Flask(__name__)
# Create an endpoint
@app.route('/predict')
def predict_chiller_condition():
print("api func called")
predictedVal=3
return 'predicted value is '+str(predictedVal)
if __name__ == "__main__":
app.run(host='0.0.0.0',port=port)
解决方案
对于任何努力解决这种情况的人,您应该通过在 deployment.template.json 中添加如下部分来公开在来宾 VM 上运行的边缘模块ExposedPorts
容器createOptions
。并且虚拟机的 IPv4 地址也应该与相关端口一起使用。
"modules": {
"test_flask_api": {
"version": "1.0",
"type": "docker",
"status": "running",
"restartPolicy": "always",
"settings": {
"image": "${MODULES.test_flask_api}",
"createOptions": {
"ExposedPorts": {
"5000/tcp": {}
},
"HostConfig": {
"PortBindings": {
"5000/tcp": [
{
"HostPort": "5000"
}
]
}
}
}
}
}
}
推荐阅读
- android - Android Studio 序列化
- google-analytics - 是否有一种解决方法可以在 Google Analytics(分析)中为同一目标创建多个渠道?
- redis - Dovecot 无法初始化 dict: dict redis: Invalid IP
- mysql - Wordpress Docker“建立数据库连接时出错”
- imagemagick - 在 MacOS Catalina 上安装 Imagick 时出错
- c# - 如何将 Unity 中的屏幕截图保存到 Azure 存储?
- jquery - 在 iOS 下的 Fancybox 中播放 mp3 的问题
- python - 如何将多个整数作为一个输入的列表附加到列表中?
- java - 为什么这段代码会给我一个 NullPointerException?
- c# - Unity3D - 光线投射没有击中正确的位置