#頭條創作挑戰賽#
目錄
- 前言
- 1,關于EdgeX的例子
- 2,使用debian進行建構
- 3,建立Dockerfile
- 4,然後将docker鏡像放到EdgeX服務中,修改res的configuration.toml 配置
- 5,測試接口,使用post man 測試接口
- 6,使用總結
1,關于EdgeX的例子
最新的例子放在github的
https://github.com/edgexfoundry/edgex-examples/tree/main/application-services/custom/send-command
項目使用的SDK是:
require github.com/edgexfoundry/app-functions-sdk-go/v2 v2.2.0
這個代碼做的是對裝置進行注冊,同時注冊兩個接口,一個get 接口,一個set 接口。
然後可以通過 EdgeX 進行通路相關資料。
2,使用debian進行建構
其實本來計劃使用 alpine 進行建構的,但是相關的環境和工具不太友好,沒有類似 pkgconfig
這樣的工具。
還需要一點點地折騰,但是換成現在 debian 之後,很多工具就有了,非常的友善。
直接上首先要編寫make file 建構腳本:
需要使用 代理:https://goproxy.io/zh/ 去下載下傳相關的依賴包。
Makefile 檔案内容:
.PHONY: build clean
GO=GOPROXY=https://proxy.golang.com.cn,direct CGO_ENABLED=1 GO111MODULE=on go
build:
$(GO) mod tidy
$(GO) build -o edgex-app-demo.bin
clean:
rm -f edgex-app-demo.bin
3,建立Dockerfile
建構拆分了編譯和運作,使用golang版本的debian bullseye 進行編譯,然後把二進制代碼和資源檔案拷貝到debian bullseye-slim 系統上。
建構的系統占資源量:
建構的時候鏡像 本地有 992MB,運作的時候少很多了隻需要 85MB的基礎鏡像起。
$ docker images | grep bullseye
golang 1.19-bullseye dce494d5814b 6 days ago 992MB
debian bullseye-slim acdab49503b5 7 days ago 80.5MB
# 使用debian 系統進行源代碼建構:
FROM golang:1.19-bullseye AS builder
# 更新debian 的源,使用 http的位址。有證書問題。
RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n"\
"deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n"\
"deb http://mirrors.aliyun.com/debian-security/ bullseye-security main\n"\
"deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main\n"\
"deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib\n"\
"deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib\n"\
"deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib\n"\
"deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" > /etc/apt/sources.list
# 按照zeroMQ 開發庫。
RUN apt-get update && apt-get install -y libzmq3-dev && apt-get clean
WORKDIR /go/src/edgex-app-demo
COPY . .
# 使用make 進行建構。修改make參數即可。
RUN cd /go/src/edgex-app-demo && make
# 實際運作的鏡像
FROM debian:bullseye-slim
# 更新debian 的源,使用 http的位址。有證書問題。
RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n"\
"deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n"\
"deb http://mirrors.aliyun.com/debian-security/ bullseye-security main\n"\
"deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main\n"\
"deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib\n"\
"deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib\n"\
"deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib\n"\
"deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" > /etc/apt/sources.list
EXPOSE 59780
RUN apt-get update && apt-get install -y libzmq5 && apt-get clean
WORKDIR /opt
COPY --from=builder /go/src/edgex-app-demo/edgex-app-demo.bin \
/opt/edgex-app-demo.bin
COPY --from=builder /go/src/edgex-app-demo/res /opt/res
ENTRYPOINT ["/opt/edgex-app-demo.bin"]
開始建構Docker鏡像:
docker build . -t edgex2022/edgex-app-demo:v1.0
複雜麻煩的地方在于為了節省磁盤空間,是以折騰得比較複雜。
有的時候這個鏡像要上傳到 私服上面,再下載下傳,是以鏡像越小越好。
最終鏡像大小在:
docker images | grep edgex
edgex2022/edgex-app-demo v1.0 225f61ad3d5c 5 minutes ago 145MB
4,然後将docker鏡像放到EdgeX服務中,修改res的configuration.toml 配置
需要 EdgeX的環境才可以啟動成功,然後設定啟動點,這個時候特别注意一定要增加參數
app-demo:
container_name: edgex-app-demo
image: edgex2022/edgex-app-demo:v1.0
networks:
edgex-network: {}
ports:
- 59780:59780/tcp
entrypoint: ["/opt/edgex-app-demo.bin", "--registry","--confdir=/opt/res"]
然後再配置 res 當中的 configuration.toml
[Writable]
LogLevel = "DEBUG"
[Writable.StoreAndForward]
Enabled = false
RetryInterval = "5m"
MaxRetryCount = 10
[Writable.InsecureSecrets]
[Writable.InsecureSecrets.DB]
path = "redisdb"
[Writable.InsecureSecrets.DB.Secrets]
username = ""
password = ""
[Service]
HealthCheckInterval = "10s"
Host = "localhost"
Port = 59780 # 設對外端口
ServerBindAddr = "0.0.0.0" # 需要設定成0.0.0.0 否則不對外提供服務 if blank, uses default Go behavior https://golang.org/pkg/net/#Listen
StartupMsg = "This is a edgex App sample Application Service to receive HTTP request as trigger"
RequestTimeout = "30s"
MaxRequestSize = 0
MaxResultCount = 0
[Registry]
Host = "edgex-core-consul"
Port = 8500
Type = "consul"
[Database]
Type = "redisdb"
Host = "edgex-redis"
Port = 6379
Timeout = "30s"
[Clients]
[Clients.core-metadata]
Protocol = "http"
Host = "edgex-core-metadata"
Port = 59881
[Clients.core-command]
Protocol = "http"
Host = "edgex-core-command"
Port = 59882
# This example expect custom type via HTTP Trigger
[Trigger]
Type="http"
[ApplicationSettings]
也可以設定資料源:配置Trigger的觸發器。
[Trigger]
Type="edgex-messagebus"
[Trigger.EdgexMessageBus]
Type = "redis"
[Trigger.EdgexMessageBus.SubscribeHost]
Host = "edgex-redis"
Port = 6379
Protocol = "redis"
SubscribeTopics="edgex/events/#"
[Trigger.EdgexMessageBus.PublishHost]
Host = "edgex-redis"
Port = 6379
Protocol = "redis"
PublishTopic="rules-events"
# 接受到了相關的日志:
level=DEBUG ts=2022-11-02T13:27:55.532706951Z app=edgex-app-demo source=runtime.go:239 msg="Expecting a custom type of edgex-robot-service/functions.ActionRequest"
level=DEBUG ts=2022-11-02T13:27:55.532770833Z app=edgex-app-demo source=send_cmd.go:45 msg="Sending Command"
level=ERROR ts=2022-11-02T13:27:55.532829953Z app=edgex-app-demo source=send_cmd.go:87 msg="Invalid action requested: "
level=DEBUG ts=2022-11-02T13:27:55.532861117Z app=edgex-app-demo source=triggermessageprocessor.go:145
msg="trigger successfully processed message 'default-pipeline' in pipeline 3e001c04-5197-4a40-9627-118ad23a7056"
level=DEBUG ts=2022-11-02T13:27:55.533387539Z app=edgex-app-demo source=messaging.go:189
msg="MessageBus Trigger: Received message with 529 bytes on topic 'edgex/events/device/Random-UnsignedInteger-Device/Random-UnsignedInteger-Device/Uint32'. Content-Type=application/json"
level=DEBUG ts=2022-11-02T13:27:55.5334934Z app=edgex-app-demo source=triggermessageprocessor.go:100
msg="trigger attempting to find pipeline(s) for topic edgex/events/device/Random-UnsignedInteger-Device/Random-UnsignedInteger-Device/Uint32"
特别需要注意的時候在 service 當中的配置設定:
特别需要注意的是在 service 當中的配置設定:
Port = 59780 # 設對外端口
ServerBindAddr = “0.0.0.0” # 需要設定成0.0.0.0 否則不能對外提供服務 i
然後在服務中就可以看到 app-send-command 了:
可以進行配置修改檢視:
5,測試接口,使用post man 測試接口
配置觸發器:
http://127.0.0.1:59780/api/v2/trigger
POST 内容:
{
"action" : "get",
"deviceName" : "Random-Integer-Device",
"commandName" : "Int8"
}
可以進行 get set 設定edgex的裝置值:
set 傳回資料:
{
"apiVersion": "v2",
"statusCode": 200
}
get 傳回資料:裝置和 profile 都是 Random-Integer-Device
{
"apiVersion": "v2",
"statusCode": 200,
"event": {
"apiVersion": "v2",
"id": "2369f3f1-df20-4522-bd2a-1fbc20e7049e",
"deviceName": "Random-Integer-Device",
"profileName": "Random-Integer-Device",
"sourceName": "Int8",
"origin": 1625868240254928800,
"readings": [
{
"id": "b37c67c6-1678-4d18-b07c-a6f148aeaefd",
"origin": 1625868240254928800,
"deviceName": "Random-Integer-Device",
"resourceName": "Int8",
"profileName": "Random-Integer-Device",
"valueType": "Int8",
"binaryValue": null,
"mediaType": "",
"value": "101"
}
]
}
}
6,使用總結
在使用的過程中,主要是環境搭建,通過docker快速建構,使用golang的代理,下載下傳成功依賴之後。
再進行相關的建構開發,就非常的友善了。剩下的就是使用docker-compose,把服務啟動。
并且增加到edgex網絡中,修改core,command等微服務的位址的配置檔案,修改對外暴露的端口。
通過service.SetFunctionsPipeline方法建構一個消息總線,然後在總線内,放入消息處理的方法就行了。
這個例子是通過trigger 中配置的觸發,也可以通過 messagebus 獲得資料源。