操作系统

NAME=”Ubuntu”
VERSION=”20.04 LTS (Focal Fossa)”

虚拟机配置

AWS Lightsail, 2C2G

安装步骤(适用于AWS云平台lightsail提供的Ubuntu操作系统)

操作系统初始化
1
2
3
4
5
6
7
# update os
sudo apt update && sudo apt upgrade -y
# adjust os datetime
sudo mv /etc/localtime /etc/localtime.bak
sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# install basic tools
sudo apt install zip unzip pkg-config build-essential
安装docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Install Common required packages or tools
sudo apt install ca-certificates curl gnupg lsb-release

# Add Docker’s GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# Add the official repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Run the system update again.
sudo apt update
# Installing Docker CE on AWS Ec2 Ubuntu
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# Check the Version & Status
docker -v
# Add your Ubuntu user to the Docker Group
sudo usermod -aG docker $USER
# To check whether your current user is in the Docker group or not, can use:
id $USER
# Reload the Shell Session
newgrp docker
# Test Docker by installing Image
docker run hello-world

refer to: https://linux.how2shout.com/how-to-install-docker-on-aws-ec2-ubuntu-22-04-or-20-04-linux/

创建ssh秘钥(可选)
1
2
# after installation, add public key to github repo
ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa -q
安装golang环境
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# download and unzip
wget -c https://go.dev/dl/go1.20.3.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local
# create go path
mkdir -p ~/goApp/bin && mkdir -p ~/goApp/pkg && mkdir -p ~/goApp/src
echo 'export GOROOT=/usr/local/go' >> ~/.bash_profile
echo 'export GOPATH=$HOME/goApp' >> ~/.bash_profile
echo 'export PATH=$PATH:/home/ubuntu/goApp/bin' >> ~/.bash_profile
# china idc can add
echo 'GOPROXY=https://goproxy.io' >> ~/.bash_profile
# at last, make it effect
source ~/.bash_profile
# add global settings
sudo -i
echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile
exit
source /etc/profile
# check go work
go version

用户环境初始化
1
2
echo 'export EDGEX_SECURITY_SECRET_STORE=false' >> ~/.bash_profile
source ~/.bash_profile
下载源代码
1
2
3
4
cd ~/goApp/src
wget https://download.wisepkg.com/edgex-app-service.zip
unzip edgex-app-service.zip

启动edgex-foundy组件
1
2
3
4
5
6
7
8
9
10
11
cd /home/ubuntu/goApp/src/edgex-app-service
# start edgex-foundry modules
docker-compose -f docker-compose.yml up -d
# when you want to stop, can execute
docker-compose -f docker-compose.yml down -v

# check modules status
docker ps
# visit edgex dashboard on web browser
http://edgex.wisepkg.com:4000/

应用初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
cd /home/ubuntu/goApp/src/edgex-app-service
mkdir logs
mkdir res
mkdir cache
mkdir meta

# init go application
go mod init github.com/ralphdc/maxscale-app-service
# install dependant packages
go mod tidy
# build source code
go build -o gateway main.go

# 抛出异常:
go build github.com/pebbe/zmq4:
# pkg-config --cflags -- libzmq
Package libzmq was not found in the pkg-config search path.
Perhaps you should add the directory containing `libzmq.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libzmq' found
pkg-config: exit status 1

# 解决方法
sudo apt install libzmq5 libczmq-dev

# 编译完成后,可以看到生成了可执行文件
gateway

创建gateway应用的配置文件(res/configuration.yml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# vi ./res/configure.yml
# 写入以下内容:

ctrl_config:
gate_name: gateway
gate_cloud_ip: *.*.*.* # 写入当前gateway主机IP
ctrl_host: 124.71.209.129 # 写入控制中心IP
ctrl_web_port: 8980
ctrl_udp_port: 50000

gateway_config:
frate: 0
treceivedReportPeriod: 2000
treceivedReportSwitch: Y
tforwardingReportSwitch: Y
tforwardingReportPeriod: 2000
cacheReportPeriod: 60
sdrsamplingp: 2000
sdfsamplingPeriod: 2000
cacheMinSensor: 200
cacheRatio: 0.5

log_act:
file_path: ./logs/app
level: info
max_age: 1400
rotation_time: 5
rotation_count: 7
include: true

influx:
addr: 117.62.218.227
port: 8086
username: gateway
password: Gateway996
database: gateway
measurement: device
timeout: 5

http_service:
host: 0.0.0.0
port: 6000
protocol: http
read_timeout: 30
write_timeout: 30

cloud_service:
host: 127.0.0.1
port: 6001
protocol: http

edgex_foundy:
host: 127.0.0.1
port: 59881

runtime:
state: true
local_storage_path: ./cache

创建适用于edgex-foundry配置文件(res/configuration.toml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# TODO: Go here for detailed information on Application Service configuation:
# https://docs.edgexfoundry.org/2.2/microservices/application/GeneralAppServiceConfig/
[Writable]
LogLevel = "INFO"

[Writable.StoreAndForward]
Enabled = false
RetryInterval = "5m"
MaxRetryCount = 10

[Writable.InsecureSecrets]
[Writable.InsecureSecrets.DB]
path = "redisdb"
[Writable.InsecureSecrets.DB.Secrets]
username = ""
password = ""
# TODO: Remove if not using HTTPS Webserver
[Writable.InsecureSecrets.HTTPS]
path = "https"
[Writable.InsecureSecrets.HTTPS.Secrets]
cert = ""
key = ""

[Writable.Telemetry]
Interval = "30s"
PublishTopicPrefix = "edgex/telemetry" # /<service-name>/<metric-name> will be added to this Publish Topic prefix
[Writable.Telemetry.Metrics] # All service's metric names must be present in this list.
MessagesReceived = true
PipelineMessagesProcessed = true # Your pipeline IDs are added to this name for the actual metric name reported
PipelineMessageProcessingTime = true # Your pipeline IDs are added to this name for the actual metric name reported
# TODO: Remove sample custom metric and implement meaningful custom metrics if any needed.
EventsConvertedToXML = true
[Writable.Telemetry.Tags] # Contains the service level tags to be attached to all the service's metrics
# Gateway="my-iot-gateway" # Tag must be added here or via Consul Env Override can only chnage existing value, not added new ones.

[Service]
HealthCheckInterval = "10s"
Host = "localhost"
# TODO: set this port appropriately,
# App service use port assigments 597xx with lower range 00-39 reserved for
# app-service-configurable profiles/instances, Rules Engine and examples
Port = 59740
ServerBindAddr = "" # Leave blank so default to Host value unless different value is needed.
StartupMsg = "maxscale-app-service Application Service has started"
MaxResultCount = 0 # Not curently used by App Services.
MaxRequestSize = 0 # Not curently used by App Services.
RequestTimeout = "5s"

# TODO: Remove section if not using HTTPS Webserver. Default protocol is HTTP if section is empty
[HttpServer]
Protocol = "http"
SecretName = "https"
HTTPSCertName = "cert"
HTTPSKeyName = "key"

[Registry]
Host = "localhost"
Port = 8500
Type = "consul"

[Database]
Type = "redisdb"
Host = "localhost"
Port = 6379
Timeout = "30s"

# TODO: Determine if your service will use secrets in secure mode, i.e. Vault.
# if not this secion can be removed, but you must make sure EDGEX_SECURITY_SECRET_STORE is set to false
# Note is database is running in secure more and you have Store and Forward enable you will need to run this
# service in secure mode.
# For more deatils about SecretStore: https://docs.edgexfoundry.org/2.2/microservices/security/Ch-SecretStore/
[SecretStore]
Type = "vault"
Host = "localhost"
Port = 8200
Path = "appservice/"
Protocol = "http"
RootCaCertPath = ""
ServerName = ""
TokenFile = "/tmp/edgex/secrets/maxscale-app-service/secrets-token.json"
[SecretStore.Authentication]
AuthType = "X-Vault-Token"
[SecretStore.RuntimeTokenProvider]
Enabled = false
Protocol = "https"
Host = "localhost"
Port = 59841
TrustDomain = "edgexfoundry.org"
EndpointSocket = "/tmp/edgex/secrets/spiffe/public/api.sock"
RequiredSecrets = "redisdb"

[Clients]
[Clients.core-data]
Protocol = "http"
Host = "localhost"
Port = 59880

[Clients.core-metadata]
Protocol = "http"
Host = "localhost"
Port = 59881

[Clients.core-command]
Protocol = "http"
Host = "localhost"
Port = 59882

[Clients.support-notifications]
Protocol = "http"
Host = "localhost"
Port = 59860

[Trigger]
Type="edgex-messagebus"
[Trigger.EdgexMessageBus]
Type = "redis"
[Trigger.EdgexMessageBus.SubscribeHost]
Host = "localhost"
Port = 6379
Protocol = "redis"
SubscribeTopics="edgex/events/#"
[Trigger.EdgexMessageBus.PublishHost] # TODO: Remove if service is NOT publishing back to the message bus
Host = "localhost"
Port = 6379
Protocol = "redis"
PublishTopic="event-xml"
[Trigger.EdgexMessageBus.Optional]
authmode = "usernamepassword" # requied for redis messagebus (secure or insecure).
secretname = "redisdb"

# TODO: If using mqtt messagebus, Uncomment this section and remove above [Trigger] section,
# Otherwise remove this commented out block
#[Trigger]
#Type="edgex-messagebus"
# [Trigger.EdgexMessageBus]
# Type = "mqtt"
# [Trigger.EdgexMessageBus.SubscribeHost]
# Host = "localhost"
# Port = 1883
# Protocol = "tcp"
# SubscribeTopics="events, edgex/events/#"
# [Trigger.EdgexMessageBus.PublishHost] # TODO: Remove if service is NOT publishing back to the message bus
# Host = "localhost"
# Port = 1883
# Protocol = "tcp"
# PublishTopic="event-xml"
# [Trigger.EdgexMessageBus.Optional]
# ClientId ="maxscale-app-service"
# Qos = "0" # Quality of Sevice values are 0 (At most once), 1 (At least once) or 2 (Exactly once)
# KeepAlive = "10" # Seconds (must be 2 or greater)
# Retained = "false"
# AutoReconnect = "true"
# ConnectTimeout = "30" # Seconds
# SkipCertVerify = "false"
# authmode = "none" # change to "usernamepassword", "clientcert", or "cacert" for secure MQTT messagebus.
# secretname = "mqtt-bus"

# TODO: Add custom settings needed by your app service or remove if you don't have any settings.
# This can be any Key/Value pair you need.
# For more details see: https://docs.edgexfoundry.org/2.2/microservices/application/GeneralAppServiceConfig/#application-settings
# Example that works with devices from the Virtual Device service:
[ApplicationSettings]
DeviceNames = "Random-Boolean-Device, Random-Integer-Device, Random-UnsignedInteger-Device, Random-Float-Device, Random-Binary-Device"

# TODO: Replace this section with your actual structured custom configuration section
# or remove if you don't have a need for structured custom configuration
# This can be any structure you need, but it can not contain slices. Use a maps instead of slices.
# For more details see: https://docs.edgexfoundry.org/2.2/microservices/application/GeneralAppServiceConfig/#custom-configuration
[AppCustom]
ResourceNames = "Boolean, Int32, Uint32, Float32, Binary"
SomeValue = 123
[AppCustom.SomeService]
Host = "localhost"
Port = 9080
Protocol = "http"

使用docker安装influxdb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 # 拉取镜像
docker pull influxdb:1.8
# 创建宿主机目录
sudo mkdir /etc/influxdb
sudo mkdir /wls/data/influxdb && sudo chmod 777 /wls/data/influxdb

# 生成配置文件
docker run --rm influxdb:1.8 influxd config | sudo tee /etc/influxdb/influxdb.conf > /dev/null
# 启动容器
docker run -p 8086:8086 \
--name influxdb \
--restart unless-stopped \
-e DOCKER_INFLUXDB_INIT_USERNAME=admin \
-e DOCKER_INFLUXDB_INIT_PASSWORD=admin@123 \
-v /wls/data/influxdb:/var/lib/influxdb \
-v /etc/influxdb/influxdb.conf:/etc/influxdb/influxdb.conf \
-v /etc/localtime:/etc/localtime \
-d influxdb:1.8

# 进入容器内部
docker exec -it influxdb /bin/bash
# 进入命令行
influx
# 设置管理员
create user "root" with password 'root@123' with all privileges
# 发现root权限为true了,那么root的用户就创建好了。
> show users
user admin
---- -----
root true
# 创建数据库
create database gateway
# 创建普通用户并赋权限
# influxd 权限有 read/write/all
create user "gateway" with password 'Gateway996';
use gateway;
grant all on gateway to "gateway";
# 用户设置完成后,退出容器
# 打开用户认证
# /etc/influxdb/influxdb.conf
auth-enabled = true
# 设置后重启容器
docker restart influxdb
# 保存对容器的修改
docker commit ecdad4f1e7dc influxdb:1.8-gateway

编译云端数据中心应用,接收gateway上报的数据(和gateway部署在同一台主机)
1
2
3
4
5
6
7
8
9
10

# 下载源码
wget https://download.wisepkg.com/edgex-cloud-collector.zip
# 解压
unzip edgex-cloud-collector.zip
cd edgex-cloud-collector
go mod init
go mod tidy
go build -o data_center main.go

在项目目录下,创建配置文件(configuration.yml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

log_act:
file_path: ./logs/app
level: info
max_age: 1400
rotation_time: 5
rotation_count: 7
include: true

influx:
addr: 127.0.0.1
port: 8086
username: gateway
password: Gateway996
database: gateway
measurement: device
timeout: 5

http_service:
host: 0.0.0.0
port: 6001
protocol: http

启动datacenter进程
1
2
3
4
5
6

# 调试输出
./data_center
# 后台运行
nohup ./data_center > /dev/null 2>&1 &

启动应用,观察输出(stdout和日志等)
1
2
3
4
5
6

# 试运行
./edgex-app-service
# 后台运行
nohup ./gateway > /dev/null 2>&1 &

配置项说明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

res/configure.yml - 配置项说明

ctrl_config:
gate_name: gateway
gate_cloud_ip: 117.62.218.227 # 运行边缘网关的IP,因安装有docker多个网桥,必须手动设置。
ctrl_host: 124.71.209.129 # 控制中心IP
ctrl_web_port: 8980 # 上报至控制中心的端口
ctrl_udp_port: 50000 # upd测距的端口

gateway_config:
frate: 0 #转发因子,值越小,转发速率越快,整数,取值范围: [0-10]
treceivedReportPeriod: 2000 #边缘网关向控制中心上报接收速率的周期,单位:毫秒
treceivedReportSwitch: Y #边缘网卡向控制中心上报接收速率的开关
tforwardingReportSwitch: Y #边缘网关向控制中心上报转发速率的开关
tforwardingReportPeriod: 2000 #边缘网关向控制中心上报转发速率的周期,单位:毫秒
cacheReportPeriod: 60 #缓存情况汇报周期
sdrsamplingPeriod: 2000 #边缘网关对虚拟设备接收速率的采样周期,单位:毫秒
sdfsamplingPeriod: 2000 #边缘网关对虚拟设备转发速率的采样周期,单位:毫秒
cacheMinSensor: 200 #每个传感器分配的内存空间
cacheRatio: 0.5 #所有传感器对应到主机内存空间使用上限的一个比率

log_act:
file_path: ./logs/app # 日志存储目录
level: info # 日志级别
max_age: 1400 # 最长保存期限
rotation_time: 5 # 归档间隔
rotation_count: 7 # 归档次数
include: true

influx:
addr: 117.62.218.227 # 当要把数据保存在influxdb数据库时,需要设置。
port: 8086
username: gateway
password: Gateway996
database: gateway
measurement: device
timeout: 5

http_service: # 边缘网关启动的http受控地址
host: 0.0.0.0
port: 6000
protocol: http
read_timeout: 30
write_timeout: 30

cloud_service: # 边缘网关将数据上报至云端的地址
host: 127.0.0.1
port: 6001
protocol: http

edgex_foundy: # edgex_foundy项目组件的通信地址
host: 127.0.0.1
port: 59881

runtime: #用于控制运行时状态,比如远端不能接收数据时,将保存真本地路径
state: true
local_storage_path: ./cache