EdgeX Foundry使用教程

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tianhuanqingyun/article/details/90454329


根据官网的操作指导,下面记录下在Ubuntu上运行EdgeX(Ubuntu)的操作流程(官网指导可能会有更新,下面的仅供参考)。

1. 运行前提

1.1 安装Docker

可参考ubuntu环境在线安装Docker

1.2 安装Docker Compose

根据官网指导,安装Docker Compose

1.2.1 下载Docker Compose

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

1.2.2 修改文件的可执行权限

$ sudo chmod +x /usr/local/bin/docker-compose

1.2.3 通过查看Docker Compose的版本验证是否成功安装

$ docker-compose --version

2. 运行EdgeX

2.1 下载yml文件

$ wget https://raw.githubusercontent.com/edgexfoundry/developer-scripts/master/compose-files/docker-compose.yml

2.2 下载镜像

使用原来的镜像源可能速度比较慢,建议使用国内镜像源:
Docker 官方中国区:https://registry.docker-cn.com
网易:http://hub-mirror.c.163.com
中国科学技术大学:https://docker.mirrors.ustc.edu.cn
先修改/etc/docker/daemon.json:

$ vi /etc/docker/daemon.json

添加如下内容:

{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}

重新加载守护进程,然后重启docker:

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

可查看Registry Mirrors修改是否生效:

$ sudo docker info

切换完镜像源后下载镜像:

$ sudo docker-compose pull

下载的时候经常会在某个状态卡住,多次尝试未果后选择科学上网瞬间搞定了>o<:

Pulling volume         ... done
Pulling consul         ... done
Pulling config-seed    ... done
Pulling mongo          ... done
Pulling logging        ... done
Pulling notifications  ... done
Pulling metadata       ... done
Pulling data           ... done
Pulling command        ... done
Pulling scheduler      ... done
Pulling export-client  ... done
Pulling export-distro  ... done
Pulling rulesengine    ... done
Pulling device-virtual ... done
Pulling portainer      ... done

镜像安装可参考github上提供的其他方法

2.3 启动微服务

$ sudo docker-compose up -d

因为网络问题反复执行了好几次才没报错:

edgex-files is up-to-date
edgex-support-rulesengine is up-to-date
Starting edgex_portainer_1 ... 
edgex-core-consul is up-to-date
Starting edgex-config-seed ... 
Starting edgex-config-seed ... done
edgex-support-logging is up-to-date
edgex-core-data is up-to-date
edgex-core-metadata is up-to-date
edgex-support-notifications is up-to-date
edgex-export-client is up-to-date
edgex-core-command is up-to-date
edgex-support-scheduler is up-to-date
Starting edgex_portainer_1 ... done
edgex-device-virtual is up-to-date

2.4 验证容器已启动

$ sudo docker-compose ps
返回如下结果:

     Name                          Command               State                                                                  Ports                                                               
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
edgex-config-seed             /bin/sh -c /edgex/cmd/conf ...   Exit 0                                                                                                                                     
edgex-core-command            /core-command --consul --p ...   Up       0.0.0.0:48082->48082/tcp                                                                                                          
edgex-core-consul             docker-entrypoint.sh agent ...   Up       8300/tcp, 8301/tcp, 8301/udp, 8302/tcp, 8302/udp, 0.0.0.0:8400->8400/tcp, 0.0.0.0:8500->8500/tcp, 0.0.0.0:8600->8600/tcp, 8600/udp
edgex-core-data               /core-data --consul --prof ...   Up       0.0.0.0:48080->48080/tcp, 0.0.0.0:5563->5563/tcp                                                                                  
edgex-core-metadata           /core-metadata --consul -- ...   Up       0.0.0.0:48081->48081/tcp, 48082/tcp                                                                                               
edgex-device-virtual          /bin/sh -c java -jar -Djav ...   Up       0.0.0.0:49990->49990/tcp                                                                                                          
edgex-export-client           /export-client --consul -- ...   Up       0.0.0.0:48071->48071/tcp                                                                                                          
edgex-export-distro           /export-distro --consul -- ...   Up       0.0.0.0:48070->48070/tcp, 0.0.0.0:5566->5566/tcp                                                                                  
edgex-files                   /bin/sh -c /usr/bin/tail - ...   Up                                                                                                                                         
edgex-mongo                   docker-entrypoint.sh /bin/ ...   Up       0.0.0.0:27017->27017/tcp                                                                                                          
edgex-support-logging         /support-logging --consul  ...   Up       0.0.0.0:48061->48061/tcp                                                                                                          
edgex-support-notifications   /support-notifications --c ...   Up       0.0.0.0:48060->48060/tcp                                                                                                          
edgex-support-rulesengine     /bin/sh -c java -jar -Djav ...   Up       0.0.0.0:48075->48075/tcp                                                                                                          
edgex-support-scheduler       /support-scheduler --consu ...   Up       0.0.0.0:48085->48085/tcp                                                                                                          
edgex_portainer_1             /portainer -H unix:///var/ ...   Up       0.0.0.0:9000->9000/tcp   

3. 连接设备

3.1 开启虚拟设备微服务

EdgeX Foundry提供了虚拟设备以便调试,默认关闭,可修改下载的docker-compose.yml,将如下内容的注释去掉:

device-random:
image: edgexfoundry/docker-device-random-go:0.7.1
ports:
- “49988:49988”
container_name: edgex-device-random
hostname: edgex-device-random
networks:
- edgex-network
volumes:
- db-data:/data/db
- log-data:/edgex/logs
- consul-config:/consul/config
- consul-data:/consul/data
depends_on:
- data
- command

3.2 启动虚拟设备微服务

$ sudo docker-compose up -d device-random

由于之前没有下载过虚拟设备的镜像,执行命令后会重新拉取该镜像:

Pulling device-random (edgexfoundry/docker-device-random-go:0.7.1)...
0.7.1: Pulling from edgexfoundry/docker-device-random-go
76ae2d597c16: Pull complete
Digest: sha256:fa3196f3075f579e105f313f0f9e1a3c6c40b31ce83967cdfa597a1208df41a3
Status: Downloaded newer image for edgexfoundry/docker-device-random-go:0.7.1
edgex-files is up-to-date
edgex-core-consul is up-to-date
edgex-mongo is up-to-date
Starting edgex-config-seed ... done
edgex-support-logging is up-to-date
Starting edgex-core-metadata ... 
Starting edgex-core-metadata ... done
edgex-core-command is up-to-date
Creating edgex-device-random ... done

3.3 查看设备发送的数据

通过EdgeX提供的日志服务查看启动的Random-Integer-Generator01发送的数据:

$ curl http://localhost:48080/api/v1/event/device/Random-Integer-Generator01/10

启动虚拟设备微服务前,返回为[];启动后,能得到类似如下的发送信息:

[{"id":"5ce6b2020e360800014fbb98","pushed":0,"device":"Random-Integer-Generator01","created":1558622722001,"modified":0,"origin":1558622722001,"schedule":null,"event":null,"readings":[{"id":"5ce6b2020e360800014fbb99","pushed":0,"created":1558622722001,"origin":1558622722000,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int16","value":"9162"}]}

4. 连接应用

EdgeX的北向可连接不同的云服务与应用,可通过MQTT broker验证EdgeX的北向数据。

4.1 注册客户端服务

$ curl -X POST -d '{
"name":"QuickStartExport",
"addressable":{
"name":"HiveMQBroker",
"protocol":"tcp",
"address":"broker.hivemq.com",
"port":1883,
"publisher":"EdgeXExportPublisher",
"topic":"EdgeXQuickStartGuide"
},
"format":"JSON",
"filter":{
"deviceIdentifiers":["Random-Integer-Generator01"]
},
"enable":true,
"destination":"MQTT_TOPIC"
}' http://localhost:48071/api/v1/registration

4.2 注册客户端服务

安装了MQTT client的话,可以直接通过命令行订阅设备发布的消息:

$ mosquitto_sub -h broker.hivemq.com -p 1883 -t EdgeXQuickStartGuide

可周期性接收到如下类似的信息:

{"id":"5ce6ba050e36080001cfa68a","pushed":0,"device":"Random-Integer-Generator01","created":1558624773002,"modified":0,"origin":1558624773001,"schedule":null,"event":null,"readings":[{"id":"5ce6ba050e36080001cfa68b","pushed":0,"created":1558624773002,"origin":1558624773001,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int8","value":"104"}]}
{"id":"5ce6ba050e36080001cfa68c","pushed":0,"device":"Random-Integer-Generator01","created":1558624773002,"modified":0,"origin":1558624773001,"schedule":null,"event":null,"readings":[{"id":"5ce6ba050e36080001cfa68d","pushed":0,"created":1558624773002,"origin":1558624773001,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int32","value":"411637861"}]}
{"id":"5ce6ba050e36080001cfa68e","pushed":0,"device":"Random-Integer-Generator01","created":1558624773003,"modified":0,"origin":1558624773001,"schedule":null,"event":null,"readings":[{"id":"5ce6ba050e36080001cfa68f","pushed":0,"created":1558624773003,"origin":1558624773001,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int16","value":"2984"}]}

5. 设备控制

使用EdgeX不能直接给设备下发指令,需要统一通过指令微服务(Command Service)下发

5.1 查看可用指令

$ curl http://localhost:48082/api/v1/device/name/Random-Integer-Generator01

执行后会返回如下的JSON数据:

{
	"id": "5ce6b1fd9f8fc20001eef84e",
	"name": "Random-Integer-Generator01",
	"adminState": "UNLOCKED",
	"operatingState": "ENABLED",
	"lastConnected": 0,
	"lastReported": 0,
	"labels": ["device-random-example"],
	"location": null,
	"commands": [{
		"created": 1558622717068,
		"modified": 0,
		"origin": 0,
		"id": "5ce6b1fd9f8fc20001eef849",
		"name": "GenerateRandomValue_Int8",
		"get": {
			"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int8",
			"responses": [{
				"code": "200",
				"description": null,
				"expectedValues": ["RandomValue_Int8"]
			}, {
				"code": "503",
				"description": "service unavailable",
				"expectedValues": []
			}],
			"url": "http://edgex-core-command:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef849"
		},
		"put": {
			"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int8",
			"responses": [{
				"code": "200",
				"description": null,
				"expectedValues": []
			}, {
				"code": "503",
				"description": "service unavailable",
				"expectedValues": []
			}],
			"parameterNames": ["Min_Int8", "Max_Int8"],
			"url": "http://edgex-core-command:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef849"
		}
	}, {
		"created": 1558622717072,
		"modified": 0,
		"origin": 0,
		"id": "5ce6b1fd9f8fc20001eef84a",
		"name": "GenerateRandomValue_Int16",
		"get": {
			"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int16",
			"responses": [{
				"code": "200",
				"description": null,
				"expectedValues": ["RandomValue_Int16"]
			}, {
				"code": "503",
				"description": "service unavailable",
				"expectedValues": []
			}],
			"url": "http://edgex-core-command:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84a"
		},
		"put": {
			"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int16",
			"responses": [{
				"code": "200",
				"description": null,
				"expectedValues": []
			}, {
				"code": "503",
				"description": "service unavailable",
				"expectedValues": []
			}],
			"parameterNames": ["Min_Int8", "Max_Int8"],
			"url": "http://edgex-core-command:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84a"
		}
	}, {
		"created": 1558622717073,
		"modified": 0,
		"origin": 0,
		"id": "5ce6b1fd9f8fc20001eef84b",
		"name": "GenerateRandomValue_Int32",
		"get": {
			"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int32",
			"responses": [{
				"code": "200",
				"description": null,
				"expectedValues": ["RandomValue_Int32"]
			}, {
				"code": "503",
				"description": "service unavailable",
				"expectedValues": []
			}],
			"url": "http://edgex-core-command:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84b"
		},
		"put": {
			"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int32",
			"responses": [{
				"code": "200",
				"description": null,
				"expectedValues": []
			}, {
				"code": "503",
				"description": "service unavailable",
				"expectedValues": []
			}],
			"parameterNames": ["Min_Int32", "Max_Int32"],
			"url": "http://edgex-core-command:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84b"
		}
	}]
}

5.2 手动获取设备发送数据

容器外,需要使用宿主机地址去访问URL,URL后半部分内容与之前的json内容保持一致:

$ curl http://localhost:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84b

执行后会返回如下的JSON数据:

{
	"id": "",
	"pushed": 0,
	"device": "Random-Integer-Generator01",
	"created": 0,
	"modified": 0,
	"origin": 1558626326372,
	"schedule": null,
	"event": null,
	"readings": [{
		"id": "",
		"pushed": 0,
		"created": 0,
		"origin": 1558626326372,
		"modified": 0,
		"device": "Random-Integer-Generator01",
		"name": "RandomValue_Int32",
		"value": "1225254283"
	}]
}

5.3 手动控制设备产生的数据范围

虚拟设备默认数据范围是 Min_Int32 ~ Max_Int32,现在使用指令微服务中的put方法修改为0 ~ 100:

$ curl -X PUT -d '[ {"Min_Int32": "0", "Max_Int32": "100"} ]' http://localhost:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84b

手动查看设备上报的结果

$ curl http://localhost:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84b

可见随机值在0 ~ 100之间:

{"id":"","pushed":0,"device":"Random-Integer-Generator01","created":0,"modified":0,"origin":1558627626973,"schedule":null,"event":null,"readings":[{"id":"","pushed":0,"created":0,"origin":1558627626973,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int32","value":"82"}]}

至此,利用EdgeX提供的虚拟设备将设备数据发送到云端以及使用指令服务控制设备的操作就完成了。

展开阅读全文

Cloud Foundry 实例安装配置

09-03

第一周把Cloud Foundry基础架构学习了一下,对于Cloud Foundry的几个核心模块有了大致的了解,为了以后深入学习,必须在自己机器上搭建一个CF实例,不过一直对于那种很复杂的配置安装非常感冒,特别是那种繁琐的配置文件,各种离奇的错误,非常令人奔溃。一开始以为CF也是多模块系统,肯定非常繁琐,没想到过程还是比较顺利。rnrn首先说一下安装环境,我是在Mac机下装了一个Ubuntu 10.04的虚拟机,64位,注意,这里必须是64位镜像!因为CF是构建在64位架构上。一个Ubuntu环境就够了,接下来就是按照文档来单节点部署一个实例,事实上在生产环境就是应该多节点安装,每个模块可以分别安装在不同的VM上面,但是开发和实验环境为了方便,就安装在一台VM上就足够了,文档区对于单节点和多接点安装的不同步骤非常详细的进行了说明。安装文档链接:[url=https://github.com/cloudfoundry/oss-docs/tree/master/vcap/single_and_multi_node_deployments_with_dev_setup]https://github.com/cloudfoundry/oss-docs/tree/master/vcap/single_and_multi_node_deployments_with_dev_setup[/url] rn 安装前最好更新一下源sudo apt-get updaternrn 接下来就是单节点安装步骤,信不信由你,就下面2行命令:rn [color=#0000FF] [plain] view plaincopyrnrn sudo apt-get install curl rn bash < <(curl -s -k -B https://raw.github.com/cloudfoundry/vcap/master/dev_setup/bin/vcap_dev_setup) [/color]rnrn 然后就进入漫长的等待,我是一般用户安装,中间会要求输入几次密码,猜测root用户安装应该会省却这些步骤,刚开始发生错误,RubyGems.org 没法加进源, Google之,是代理问题,由于在公司使用代理上网,于是呼换了一个网线插口,取消代理,重新输上面两条命令,一切就正常了,安装文档中对于各种常见的错误都有非常详细的说明,不过我相信现在的安装脚本还是写的比较可靠的,基本上是一键安装,不会遇到什么问题。rnrn 最后,出现这个截图,安装成功rn[img=http://events.csdn.net//xhy/images/201209031519.jpg][/img]rn 接下来就是启动CF,输入命令:~/cloudfoundry/vcap/dev_setup/bin/vcap_dev start rnrn从上面可以看到CF各个模块都已经跑起来了,我的VM内存分配了1G,保险起见,最好配个2G内存。rnrn 接下来一步是可选择的,对于Mac/Linux用户来说,创建一个本地SSH通道rn[img=http://events.csdn.net//xhy/images/201209031518.jpg][/img]rn ssh @rnrn sudo ssh -L ::80 @ -Nrnrn 然后在浏览器里面访问api.vcap.me,出现这个页面,就表示成功,至此,Cloud Foundry 的一个实例开发环境就搭建好了,非常简单rnrnrn[img=http://events.csdn.net//xhy/images/20120903160159341.jpg][/img] 论坛

没有更多推荐了,返回首页