首页 > 解决方案 > 无法访问 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)

标签: pythonazure-iot-hubazure-iot-edge

解决方案


对于任何努力解决这种情况的人,您应该通过在 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"
                  }
                ]
              }
            }
          }
        }
      }
    }

推荐阅读