EdgeX Foundry第五弹 导出数据到MQTT和HTTP服务

本文为Edgex系列第五篇文章,本篇文章将介绍如何把之前接入的设备数据导出到我们的MQTT或者HTTP设备进行使用。

一、导出数据到MQTT服务

我们还是进入到edgex-compose项目,并点击compose-builder,拖动滚动条到Pull,可以看到

EdgeX Foundry第五弹 导出数据到MQTT和HTTP服务_第1张图片

我们可以根据选择自定义组合需要的服务,MQTT导出服务这里,我们选择asc-mqtt

接下来跟前面一样进入compose-builder目录执行make asc-mqtt no-secty,我们将asc-mqtt的内容复制到我们之前的docker-compose.yml文件中,最终文件如下:

networks:
  edgex-network:
    driver: bridge
services:
  app-service-mqtt-export:
    container_name: edgex-app-mqtt-export
    depends_on:
    - consul
    - data
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      DATABASE_HOST: edgex-redis
      EDGEX_PROFILE: mqtt-export
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-app-mqtt-export
      TRIGGER_EDGEXMESSAGEBUS_PUBLISHHOST_HOST: edgex-redis
      TRIGGER_EDGEXMESSAGEBUS_SUBSCRIBEHOST_HOST: edgex-redis
      WRITABLE_LOGLEVEL: INFO
      WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_BROKERADDRESS: tcp://broker.mqttdashboard.com:1883
      WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_TOPIC: edgex-events
    hostname: edgex-app-mqtt-export
    image: edgexfoundry/app-service-configurable:2.0.1
    networks:
      edgex-network: null
    ports:
    - 59703:59703/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  app-service-rules:
    container_name: edgex-app-rules-engine
    depends_on:
    - consul
    - data
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      EDGEX_PROFILE: rules-engine
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-app-rules-engine
      TRIGGER_EDGEXMESSAGEBUS_PUBLISHHOST_HOST: edgex-redis
      TRIGGER_EDGEXMESSAGEBUS_SUBSCRIBEHOST_HOST: edgex-redis
    hostname: edgex-app-rules-engine
    image: edgexfoundry/app-service-configurable:2.0.1
    networks:
      edgex-network: null
    ports:
    - 59701:59701/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  command:
    container_name: edgex-core-command
    depends_on:
    - consul
    - database
    - metadata
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-core-command
    hostname: edgex-core-command
    image: edgexfoundry/core-command:2.0.0
    networks:
      edgex-network: null
    ports:
    - 59882:59882/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  consul:
    container_name: edgex-core-consul
    hostname: edgex-core-consul
    image: consul:1.9.5
    networks:
      edgex-network: null
    ports:
    - 8500:8500/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: root:root
    volumes:
    - consul-config:/consul/config:z
    - consul-data:/consul/data:z
  data:
    container_name: edgex-core-data
    depends_on:
    - consul
    - database
    - metadata
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-core-data
    hostname: edgex-core-data
    image: edgexfoundry/core-data:2.0.0
    networks:
      edgex-network: null
    ports:
    - 5563:5563/tcp
    - 59880:59880/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  database:
    container_name: edgex-redis
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      REGISTRY_HOST: edgex-core-consul
    hostname: edgex-redis
    image: redis:6.2.4-alpine
    networks:
      edgex-network: null
    ports:
    - 6379:6379/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: root:root
    volumes:
    - db-data:/data:z
  device-mqtt:
    container_name: edgex-device-mqtt
    depends_on:
    - consul
    - data
    - metadata
    - mqtt-broker
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      MQTTBROKERINFO_HOST: edgex-mqtt-broker
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-device-mqtt
      DEVICE_DEVICESDIR: /custom-config/devices
      DEVICE_PROFILESDIR: /custom-config/profiles
    hostname: edgex-device-mqtt
    image: edgexfoundry/device-mqtt:2.0.0
    networks:
      edgex-network: null
    ports:
    - 59982:59982/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
    volumes:
    - /usr/local/cq/tool/custom-config:/custom-config
  metadata:
    container_name: edgex-core-metadata
    depends_on:
    - consul
    - database
    - notifications
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      NOTIFICATIONS_SENDER: edgex-core-metadata
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-core-metadata
    hostname: edgex-core-metadata
    image: edgexfoundry/core-metadata:2.0.0
    networks:
      edgex-network: null
    ports:
    - 59881:59881/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  mqtt-broker:
    command: /usr/sbin/mosquitto -c /mosquitto-no-auth.conf
    container_name: edgex-mqtt-broker
    hostname: edgex-mqtt-broker
    image: eclipse-mosquitto:2.0.11
    networks:
      edgex-network: null
    ports:
    - published: 1883
      target: 1883
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  notifications:
    container_name: edgex-support-notifications
    depends_on:
    - consul
    - database
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-support-notifications
    hostname: edgex-support-notifications
    image: edgexfoundry/support-notifications:2.0.0
    networks:
      edgex-network: null
    ports:
    - 59860:59860/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  rulesengine:
    container_name: edgex-kuiper
    depends_on:
    - database
    environment:
      EDGEX__DEFAULT__PORT: 6379
      EDGEX__DEFAULT__PROTOCOL: redis
      EDGEX__DEFAULT__SERVER: edgex-redis
      EDGEX__DEFAULT__TOPIC: rules-events
      EDGEX__DEFAULT__TYPE: redis
      KUIPER__BASIC__CONSOLELOG: "true"
      KUIPER__BASIC__RESTPORT: 59720
    hostname: edgex-kuiper
    image: lfedge/ekuiper:1.3.0-alpine
    networks:
      edgex-network: null
    ports:
    - 59720:59720/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: kuiper:kuiper
    volumes:
    - kuiper-data:/kuiper/data:z
  scheduler:
    container_name: edgex-support-scheduler
    depends_on:
    - consul
    - database
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      EDGEX_SECURITY_SECRET_STORE: "false"
      INTERVALACTIONS_SCRUBAGED_HOST: edgex-core-data
      INTERVALACTIONS_SCRUBPUSHED_HOST: edgex-core-data
      MESSAGEQUEUE_HOST: edgex-redis
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-support-scheduler
    hostname: edgex-support-scheduler
    image: edgexfoundry/support-scheduler:2.0.0
    networks:
      edgex-network: null
    ports:
    - 59861:59861/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  system:
    container_name: edgex-sys-mgmt-agent
    depends_on:
    - command
    - consul
    - data
    - metadata
    - notifications
    - scheduler
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      EDGEX_SECURITY_SECRET_STORE: "false"
      EXECUTORPATH: /sys-mgmt-executor
      MESSAGEQUEUE_HOST: edgex-redis
      METRICSMECHANISM: executor
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-sys-mgmt-agent
    hostname: edgex-sys-mgmt-agent
    image: edgexfoundry/sys-mgmt-agent:2.0.0
    networks:
      edgex-network: null
    ports:
    - 58890:58890/tcp
    read_only: true
    security_opt:
    - label:disable
    - no-new-privileges:true
    user: root:root
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock:z
  ui:
    container_name: edgex-ui-go
    hostname: edgex-ui-go
    image: edgexfoundry/edgex-ui:2.0.0
    networks:
      edgex-network: null
    ports:
    - 4000:4000/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  device-virtual:
   container_name: edgex-device-virtual
   depends_on:
   - consul
   - data
   - metadata
   environment:
     CLIENTS_CORE_COMMAND_HOST: edgex-core-command
     CLIENTS_CORE_DATA_HOST: edgex-core-data
     CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
     CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
     CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
     DATABASES_PRIMARY_HOST: edgex-redis
     EDGEX_SECURITY_SECRET_STORE: "false"
     MESSAGEQUEUE_HOST: edgex-redis
     REGISTRY_HOST: edgex-core-consul
     SERVICE_HOST: edgex-device-virtual
   hostname: edgex-device-virtual
   image: edgexfoundry/device-virtual:2.0.0
   networks:
     edgex-network: {}
   ports:
   - 59900:59900/tcp
   read_only: true
   security_opt:
   - no-new-privileges:true
   user: 2002:2001
version: '3.7'
volumes:
  consul-config: {}
  consul-data: {}
  db-data: {}
  kuiper-data: {}

复制代码

我们把MQTT导出服务的配置单独拿出来,如下:

app-service-mqtt-export:
    container_name: edgex-app-mqtt-export
    depends_on:
    - consul
    - data
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      DATABASE_HOST: edgex-redis
      EDGEX_PROFILE: mqtt-export
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-app-mqtt-export
      TRIGGER_EDGEXMESSAGEBUS_PUBLISHHOST_HOST: edgex-redis
      TRIGGER_EDGEXMESSAGEBUS_SUBSCRIBEHOST_HOST: edgex-redis
      WRITABLE_LOGLEVEL: INFO
      WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_BROKERADDRESS: tcp://broker.mqttdashboard.com:1883
      WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_TOPIC: edgex-events
    hostname: edgex-app-mqtt-export
    image: edgexfoundry/app-service-configurable:2.0.1
    networks:
      edgex-network: null
    ports:
    - 59703:59703/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
复制代码

WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_BROKERADDRESS导出地址及WRITABLE_PIPELINE_FUNCTIONS_MQTTEXPORT_PARAMETERS_TOPIC导出主题可以自定义以方便服务接受。

接下来我们进入MQTT的web client进行测试,输出Host和Port后进行连接

EdgeX Foundry第五弹 导出数据到MQTT和HTTP服务_第2张图片

接下来我们订阅刚才配置的主题edgex-events

有数据进来,导出成功。

二、导出数据到HTTP服务

进入compose-builder目录执行make asc-http no-secty,我们将asc-http的内容复制到我们之前的docker-compose.yml文件中,在刚才的配置文件中加

...
app-service-http-export:
    container_name: edgex-app-http-export
    depends_on:
    - consul
    - data
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      DATABASE_HOST: edgex-redis
      EDGEX_PROFILE: http-export
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-app-http-export
      TRIGGER_EDGEXMESSAGEBUS_PUBLISHHOST_HOST: edgex-redis
      TRIGGER_EDGEXMESSAGEBUS_SUBSCRIBEHOST_HOST: edgex-redis
      WRITABLE_LOGLEVEL: INFO
      WRITABLE_PIPELINE_FUNCTIONS_HTTPEXPORT_PARAMETERS_URL: http://192.168.10.131:8080/income
    hostname: edgex-app-http-export
    image: edgexfoundry/app-service-configurable:2.0.1
    networks:
      edgex-network: null
    ports:
    - 59704:59704/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  device-rest:
    container_name: edgex-device-rest
    depends_on:
    - consul
    - data
    - metadata
    environment:
      CLIENTS_CORE_COMMAND_HOST: edgex-core-command
      CLIENTS_CORE_DATA_HOST: edgex-core-data
      CLIENTS_CORE_METADATA_HOST: edgex-core-metadata
      CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications
      CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler
      DATABASES_PRIMARY_HOST: edgex-redis
      EDGEX_SECURITY_SECRET_STORE: "false"
      MESSAGEQUEUE_HOST: edgex-redis
      REGISTRY_HOST: edgex-core-consul
      SERVICE_HOST: edgex-device-rest
    hostname: edgex-device-rest
    image: edgexfoundry/device-rest:2.0.0
    networks:
      edgex-network: null
    ports:
    - 127.0.0.1:59986:59986/tcp
    read_only: true
    security_opt:
    - no-new-privileges:true
    user: 2002:2001
  ...
复制代码

注意WRITABLE_PIPELINE_FUNCTIONS_HTTPEXPORT_PARAMETERS_URL: http://192.168.10.131:8080/income这里配置了我们的导出端点,即http://192.168.10.131:8080/income,这里是我自定义的一个springboot应用

EdgeX Foundry第五弹 导出数据到MQTT和HTTP服务_第3张图片

接下来我们执行docker-compose up -d,使用docker ps检查服务是否启动,服务没问题后,启动我们的springboot应用,可以看到数据不断推送过来

EdgeX Foundry第五弹 导出数据到MQTT和HTTP服务_第4张图片

至此我们http数据导出成功。

三、数据导出的其他方式

  1. 使用app-function-go自定义导出应用服务,数据转换层放这里是个好选择?欢迎大家讨论。
  2. 使用eKuiper数据过滤导出功能

四、推荐视频

官方meetup

五、参考资料

docs.edgexfoundry.org/2.0/walk-th… github.com/edgexfoundr…

你可能感兴趣的:(边缘计算)