利用systemd部署golang項目的實現(xiàn)方法
簡介
- CentOS 7 繼承了 RHEL 7 的新的特性,例如強(qiáng)大的 systemd, 而 systemd 的使用也使得以往系統(tǒng)服務(wù)的 /etc/init.d 的啟動腳本的方式就此改變, 也大幅提高了系統(tǒng)服務(wù)的運(yùn)行效率。但服務(wù)的配置和以往也發(fā)生了極大的不同,同時變的簡單而易用了許多。
- CentOS 7 的服務(wù) systemctl 腳本存放在:/usr/lib/systemd/,有系統(tǒng) system 和用戶 user 之分, 即:/usr/lib/systemd/system 和 /usr/lib/systemd/user
配置文件
- 這里我們先要說明一下 unit 的文件位置,一般主要有三個目錄:
/lib/systemd/system /run/systemd/system /etc/systemd/system
- 這三個目錄的配置文件優(yōu)先級依次從低到高,如果同一選項三個地方都配置了,優(yōu)先級高的會覆蓋優(yōu)先級低的。 系統(tǒng)安裝時,默認(rèn)會將 unit 文件放在 /lib/systemd/system 目錄。如果我們想要修改系統(tǒng)默認(rèn)的配置,比如 nginx.service,一般有兩種方法:
- 在 /etc/systemd/system 目錄下創(chuàng)建 nginx.service 文件,里面寫上我們自己的配置。
- 在 /etc/systemd/system 下面創(chuàng)建 nginx.service.d 目錄,在這個目錄里面新建任何以.conf 結(jié)尾的文件,然后寫入我們自己的配置。推薦這種做法。
- /run/systemd/system 這個目錄一般是進(jìn)程在運(yùn)行時動態(tài)創(chuàng)建 unit 文件的目錄,一般很少修改,除非是修改程序運(yùn)行時的一些參數(shù)時,即 Session 級別的,才在這里做修改。
服務(wù)配置
- 每一個服務(wù)以.service 結(jié)尾,一般會分為 3 部分:[Unit]、[Service] 和 [Install],就以 nginx 為例吧,具體內(nèi)容如下:
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
配置項說明
- 下面分別解釋下著三部分的含義
[Unit]
- Description : 服務(wù)的簡單描述
- Documentation : 服務(wù)文檔
- After= : 依賴,僅當(dāng)依賴的服務(wù)啟動之后再啟動自定義的服務(wù)單元
[Service]
- Type : 啟動類型 simple、forking、oneshot、notify、dbus
- - Type=simple(默認(rèn)值):systemd 認(rèn)為該服務(wù)將立即啟動,服務(wù)進(jìn)程不會 fork。如果該服務(wù)要啟動其他服務(wù),不要使用此類型啟動,除非該服務(wù)是 socket 激活型
- - Type=forking:systemd 認(rèn)為當(dāng)該服務(wù)進(jìn)程 fork,且父進(jìn)程退出后服務(wù)啟動成功。對于常規(guī)的守護(hù)進(jìn)程(daemon),除非你確定此啟動方式無法滿足需求, 使用此類型啟動即可。使用此啟動類型應(yīng)同時指定 PIDFile=,以便 systemd 能夠跟蹤服務(wù)的主進(jìn)程。
- - Type=oneshot:這一選項適用于只執(zhí)行一項任務(wù)、隨后立即退出的服務(wù)??赡苄枰瑫r設(shè)置 RemainAfterExit=yes 使得 systemd 在服務(wù)進(jìn)程退出之后仍然認(rèn)為服務(wù)處于激活狀態(tài)。
- - Type=notify:與 Type=simple 相同,但約定服務(wù)會在就緒后向 systemd 發(fā)送一個信號,這一通知的實現(xiàn)由 libsystemd-daemon.so 提供
- - Type=dbus:若以此方式啟動,當(dāng)指定的 BusName 出現(xiàn)在 DBus 系統(tǒng)總線上時,systemd 認(rèn)為服務(wù)就緒。
- PIDFile : pid 文件路徑
- Environment : 環(huán)境變量(可以添加多個)eg :Environment=REPO_REF=dev
- ExecStartPre :啟動前要做什么,上文中是測試配置文件 -t
- ExecStart:啟動
- ExecReload:重載
- ExecStop:停止
- PrivateTmp:True 表示給服務(wù)分配獨(dú)立的臨時空間
[Install]
- WantedBy:服務(wù)安裝的用戶模式,從字面上看,就是想要使用這個服務(wù)的有是誰?上文中使用的是:multi-user.target ,就是指想要使用這個服務(wù)的目錄是多用戶。
操作示例
- 每一個.target 實際上是鏈接到我們單位文件的集合,當(dāng)我們執(zhí)行
systemctl enable nginx.service
- 就會在 /etc/systemd/system/multi-user.target.wants/ 目錄下新建一個 /usr/lib/systemd/system/nginx.service 文件的鏈接。
常用的 service 操作
# 自啟動 systemctl enable nginx.service # 禁止自啟動 systemctl disable nginx.service # 啟動服務(wù) systemctl start nginx.service # 停止服務(wù) systemctl stop nginx.service # 重啟服務(wù) systemctl restart nginx.service # 查看Unit定義文件 systemctl cat nginx.service # 編輯Unit定義文件 systemctl edit nginx.service # 重新加載Unit定義文件 systemctl reload nginx.service # 列出已啟動的所有unit,就是已經(jīng)被加載到內(nèi)存中 systemctl list-units # 列出系統(tǒng)已經(jīng)安裝的所有unit,包括那些沒有被加載到內(nèi)存中的unit systemctl list-unit-files # 查看服務(wù)的日志 journalctl -u nginx.service # 還可以配合`-b`一起使用,只查看自本次系統(tǒng)啟動以來的日志 # 查看所有target下的unit systemctl list-unit-files --type=target # 查看默認(rèn)target,即默認(rèn)的運(yùn)行級別。對應(yīng)于舊的`runlevel`命令 systemctl get-default # 設(shè)置默認(rèn)的target systemctl set-default multi-user.target # 查看某一target下的unit systemctl list-dependencies multi-user.target # 切換target,不屬于新target的unit都會被停止 systemctl isolate multi-user.target systemctl poweroff # 關(guān)機(jī) systemctl reboot # 重啟 systemctl rescue # 進(jìn)入rescue模式
Systemd + Golang Demo
- 下面例子通過 Systemd 部署一個最簡單的 Golang Web Server
package main
import (
"flag"
"fmt"
"log"
"net/http"
)
func main() {
var address string
flag.StringVar(&address, "address", "5353", "listen address")
flag.Parse()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, This is a test for systemd !\n")
}) // 設(shè)置訪問路由
log.Printf("Starting server on %s\n", address)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", address), nil))
}
- 編譯代碼,并將可執(zhí)行文件同步到遠(yuǎn)程服務(wù)器上
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o systemd-test main.go rsync -zP ./systemd-test root@gp-aliyun:/usr/local/bin/
- 在遠(yuǎn)程服務(wù)器上編寫服務(wù)配置,放在 /etc/systemd/system/ 中
[Unit] Description=Systemd Test After=network.target [Service] User=nobody # Execute `systemctl daemon-reload` after ExecStart= is changed. ExecStart=/usr/local/bin/systemd-test -address "6060" [Install] WantedBy=multi-user.target
- 通過 systemctl 啟動服務(wù)
# 每一次修改ExecStart都需執(zhí)行 systemctl daemon-reload # 啟動 systemctl start systemd-test.service # 查看狀態(tài) systemctl status systemd-test.service
- 狀態(tài)如下

- 可以通過 curl 進(jìn)行測試:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Go并發(fā)原語之SingleFlight請求合并方法實例
本文我們來學(xué)習(xí)一下 Go 語言的擴(kuò)展并發(fā)原語:SingleFlight,SingleFlight 的作用是將并發(fā)請求合并成一個請求,以減少重復(fù)的進(jìn)程來優(yōu)化 Go 代碼2023-12-12
Go?runtime?調(diào)度器之系統(tǒng)調(diào)用引起的搶占
本文解析了在Go語言中,當(dāng)goroutine執(zhí)行的系統(tǒng)調(diào)用時間過長時,系統(tǒng)如何通過監(jiān)控和搶占機(jī)制來處理,以維持運(yùn)行效率和資源分配的平衡,通過具體的示例和流程圖,詳細(xì)展示了系統(tǒng)調(diào)用過程中的搶占操作,感興趣的朋友跟隨小編一起看看吧2024-09-09

