最近使用Nightingale
需要實現對服務的監測,想要在Windows
系統中使用,看官方文檔中并不直接提供執行程序,原文如下:
準備工作
本地環境
本地已經安裝git
本地安裝git
便于后續下載源碼并進行自動編譯。
Linux
操作系統環境(Centos8
)
由于源碼的執行腳本是使用sh
,為了降低難度,直接使用Linux-amd64
,不管是使用虛擬機還是Wsl
子系統都可以,筆者這里使用的Centos8
本地物理機 。
安裝Go
并配置環境
下載Go
二進制部署包(國內可以使用阿里云等鏡像源進行下載)。
官方下載:
wget https://golang.google.cn/dl/go1.25.0.linux-amd64.tar.gz
國內鏡像:
wget https://mirrors.aliyun.com/golang/go1.25.0.linux-amd64.tar.gz
解壓到/usr/local
目錄。
$sudo tar -C /usr/local -xzf go1.25.0.linux-amd64.tar.gz
執行命令輸出結果如下:
$whereis go
配置環境變量,此處為了方便演示,直接配置/etc/profile
,便于所有用戶均生效。
$sudo vi /etc/profile
在尾部添加如下配置:
export PATH=$PATH:/usr/local/go/bin
國內可以添加環境加速變量:
export GOPROXY=https://goproxy.cn,direct
export GOSUMDB=off
配置后保存并退出,使用source /etc/profile
。
$ source /etc/profile
驗證是否生效:
$go version
go version go1.25.0 linux/amd64
查看環境:
$go env GOOS GOARCH
linux amd64
安裝GoReleaser
參考官方鏈接
直接使用Go install
。
$go install github.com/goreleaser/goreleaser/v2@latest
安裝完成后,實際執行程序在/home/當前賬戶/go/bin/goreleaser
。
由于默認未設置Go
的執行目錄。
為了方便,同樣在當前賬戶環境變量配置.bash_profile
配置文件的尾部添加上環境變量,保存后退出。
export PATH=$PATH:/home/<當前賬戶>/go/bin
執行source
。
source .bash_profile
查看版本。
$goreleaser -v____ ____ _/ ___| ___ | _ \ ___| | ___ __ _ ___ ___ _ __
| | _ / _ \| |_) / _ \ |/ _ \/ _` / __|/ _ \ '__|
| |_| | (_) | _ < __/ | __/ (_| \__ \ __/ |\____|\___/|_| \_\___|_|\___|\__,_|___/\___|_|
goreleaser: Release engineering, simplified.
https://goreleaser.com
GitVersion: v2.11.2
GitCommit: unknown
GitTreeState: unknown
BuildDate: unknown
BuiltBy: unknown
GoVersion: go1.25.0
Compiler: gc
ModuleSum: h1:Od6dcPI5r8IWVPnJYz6wYe3rML1qf80fLzXB1Ix6ZnY=
Platform: linux/amd64
至此,環境準備成功。
下載源碼
把代碼通過master
分支克隆下來,對應tag
版本為8.2.1
。
git clone https://github.com/ccfos/nightingale.git
由于是使用的Go
語言開發,同時注意到項目源碼目錄下中有一個.goreleaser.yml
配置文件。可以考慮直接通過GoReleaser
進行程序構建。
修改配置
實際為了正常使用編譯,需要修改兩個配置文件,一個是fe.sh
,一個是.goreleaser.yml
。
fe.sh
修改該文件主要是在執行自動化打包時,會需要從github
中下載對應版本的源碼包,由于是國內,所以,為了快速下載。所以需要添加一些國內的github
代理站點,這里筆者也是用AI
修改的,完整配置如下:
#!/bin/bash# 復制初始化 SQL
cp -f ./docker/initsql/a-n9e.sql n9e.sql# 如果 pub 目錄已存在,跳過下載
if [ ! -d "./pub" ]; then# 獲取最新版本號TAG=$(curl -sX GET "https://api.github.com/repos/n9e/fe/releases/latest" | awk '/tag_name/{print $4;exit}' FS='[""]')FILENAME="n9e-fe-${TAG}.tar.gz"echo "?? 正在下載前端包: ${FILENAME}"# 定義多個下載源(支持代理)DOWNLOAD_URLS=("https://mirror.ghproxy.com/https://github.com/n9e/fe/releases/download/${TAG}/${FILENAME}""https://gh-proxy.com/https://github.com/n9e/fe/releases/download/${TAG}/${FILENAME}""https://download.fastgit.org/n9e/fe/releases/download/${TAG}/${FILENAME}""https://github.com/n9e/fe/releases/download/${TAG}/${FILENAME}")# 嘗試每個地址,直到成功for url in "${DOWNLOAD_URLS[@]}"; doecho "?? 嘗試下載: $url"if curl -# -f -o "$FILENAME" -L "$url"; thenecho "? 下載成功: $FILENAME"breakelseecho "? 下載失敗: $url"fidone# 檢查是否下載成功if [ ! -f "$FILENAME" ]; thenecho "? 所有下載源均失敗,無法獲取 ${FILENAME}"exit 1fi# 解壓if ! tar zxf "$FILENAME"; thenecho "? 解壓失敗: $FILENAME"exit 2fi# 清理壓縮包(可選)# rm -f "$FILENAME"
fi# 獲取 GOPATH
GOPATH=$(go env GOPATH)
GOPATH=${GOPATH:-$HOME/go}# 確保 statik 已安裝
if ! command -v statik &> /dev/null; thenecho "?? statik 未安裝,正在安裝..."go install github.com/rakyll/statik@latest
fi# 嵌入靜態文件
if ! "$GOPATH/bin/statik" -src=./pub -dest=./front -f; thenecho "? 嵌入靜態文件失敗"exit 4
fiecho "?? 前端資源已成功嵌入"
.goreleaser.yml
這個是goreleaser
進行自動化打包需要的配置文件。原有配置中并未包含windows
執行配置,同時還多了docker
鏡像包的打包,但是并不是筆者需要的,所以在使用AI
對配置進行了合理修改,完整內容如下:
version: 2before:hooks:- go mod tidy- go install github.com/rakyll/statikchangelog:disable: true#
#
#
# #
archives:# Linux amd64- id: linux-amd64name_template: >-{{- if eq .Os "linux" }}n9e-v{{ .Version }}-linux-amd64.tar.gz{{- end }}wrap_in_directory: falsefiles:- docker/**- etc/**- integrations/**- cli/**- n9e.sql# # Linux arm64- id: linux-arm64name_template: >-{{- if eq .Os "linux" }}n9e-v{{ .Version }}-linux-arm64.tar.gz{{- end }}wrap_in_directory: falsefiles:- docker/**- etc/**- integrations/**- cli/**- n9e.sql# Windows amd64- id: windows-amd64name_template: >-{{- if eq .Os "windows" }}n9e-v{{ .Version }}-windows-amd64.zip{{- end }}wrap_in_directory: falsefiles:- docker/**- etc/**- integrations/**- cli/**- n9e.sql# Windows arm64- id: windows-arm64name_template: >-{{- if eq .Os "windows" }}n9e-v{{ .Version }}-windows-arm64.zip{{- end }}wrap_in_directory: falsefiles:- docker/**- etc/**- integrations/**- cli/**- n9e.sql# macOS amd64- id: darwin-amd64name_template: >-{{- if eq .Os "darwin" }}n9e-v{{ .Version }}-darwin-amd64.tar.gz{{- end }}wrap_in_directory: falsefiles:- docker/**- etc/**- integrations/**- cli/**- n9e.sql# macOS arm64- id: darwin-arm64name_template: >-{{- if eq .Os "darwin" }}n9e-v{{ .Version }}-darwin-arm64.tar.gz{{- end }}wrap_in_directory: falsefiles:- docker/**- etc/**- integrations/**- cli/**- n9e.sql#
# builds:- id: buildhooks:pre:- cmd: sh -x ./fe.shoutput: truemain: ./cmd/center/binary: n9eenv:- CGO_ENABLED=0goos:- linux- windows- darwingoarch:- amd64- arm64ldflags:- -s -w- -X github.com/ccfos/nightingale/v8/pkg/version.Version={{ .Tag }}-{{ .ShortCommit }}# skip: false# archives - id: build-climain: ./cmd/cli/binary: n9e-clienv:- CGO_ENABLED=0goos:- linux- windows- darwingoarch:- amd64- arm64ldflags:- -s -w- -X github.com/ccfos/nightingale/v8/pkg/version.Version={{ .Tag }}-{{ .ShortCommit }}- id: build-edgemain: ./cmd/edge/binary: n9e-edgeenv:- CGO_ENABLED=0goos:- linux- windows- darwingoarch:- amd64- arm64ldflags:- -s -w- -X github.com/ccfos/nightingale/v8/pkg/version.Version={{ .Tag }}-{{ .ShortCommit }}#
release:github:owner: ccfosname: nightingalename_template: v{{ .Version }}# Docker
#dockers:
# - image_templates:
# - flashcatcloud/nightingale:{{ .Version }}-amd64
# goos: linux
# goarch: amd64
# ids:
# - build
# dockerfile: docker/Dockerfile.goreleaser
# extra_files:
# - etc
# - integrations
# use: buildx
# build_flag_templates:
# - "--platform=linux/amd64"# - image_templates:
# - flashcatcloud/nightingale:{{ .Version }}-arm64v8
# goos: linux
# goarch: arm64
# ids:
# - build
# dockerfile: docker/Dockerfile.goreleaser.arm64
# extra_files:
# - etc
# - integrations
# use: buildx
# build_flag_templates:
# - "--platform=linux/arm64/v8"# Docker
#docker_manifests:
# - name_template: flashcatcloud/nightingale:{{ .Version }}
# image_templates:
# - flashcatcloud/nightingale:{{ .Version }}-amd64
# - flashcatcloud/nightingale:{{ .Version }}-arm64v8# - name_template: flashcatcloud/nightingale:latest
# image_templates:
# - flashcatcloud/nightingale:{{ .Version }}-amd64
# - flashcatcloud/nightingale:{{ .Version }}-arm64v8
執行配置檢測
為避免配置粘貼復制存在問題,建議在源碼根目錄中,執行配置檢測命令goreleaser checkout
,保證配置的正確性。
$goreleaser checkout? checking path=.goreleaser.yaml? 1 configuration file(s) validated? thanks for using GoReleaser!
進行構建
通過如下指令goreleaser build --snapshot
進行快照構建,構建過程中需要耐心等待,輸出如下內容則表示構建成功build succeeded after
。
$goreleaser build --snapshot/*省略內容*/? building binary=dist/build-edge_darwin_arm64_v8.0/n9e-edge? writing artifacts metadata? build succeeded after 1m2s? thanks for using GoReleaser!
構建過程中會生成dist
目錄,目錄中就是不同平臺對應的執行程序,如果在構建過程中出現異常,可以刪除dist
目錄進行重新構建。
在Windows
上運行
作為獨立運行程序,需要準備兩個依賴服務,也可以直接使用默認sqlite
和miniredis
。
mysql
redis
數據庫初始化腳本在源碼docker/initsql/a-n9e.sql
中,可以直接進行執行,會自動在數據庫服務中創建數據庫n9e_v6
。
配置文件模板在源碼etc
目錄下的config.toml
中,將其連帶etc
文件夾一并拷貝到與執行程序平級。
.
n9e.exe
etc/config.toml
主要修改config.toml
配置文件中[DB]
和[Redis]
。
[DB]
# mysql postgres sqlite
DBType = "mysql"
# postgres: host=%s port=%s user=%s dbname=%s password=%s sslmode=%s
# postgres: DSN="host=127.0.0.1 port=5432 user=root dbname=n9e_v6 password=1234 sslmode=disable"
DSN="root:ggcy@admin@tcp(localhost:3360)/n9e_v6?charset=utf8mb4&parseTime=True&loc=Local"
[Redis]
# address, ip:port or ip1:port,ip2:port for cluster and sentinel(SentinelAddrs)
Address = "127.0.0.1:6739"
RedisType = "standalone"
直接控制臺運行命令。
>n9e.exe
runner.cwd: [workdir]/build_windows_amd64_v1
runner.hostname: [host]
runner.fd_limits: N/A
runner.vm_limits: N/A
rpc.listening: 0.0.0.0:20090
please view n9e at http://172.21.11.6:17000
http server listening on: 0.0.0.0:17000
訪問地址:http://localhost:17000
。
默認賬戶為root
,密碼為root.2020
,進入系統。