Go項目實戰:打造高並發日志采集系統(十)


前情回顧

前文我們完成了日志管理系統后台開發。

本節目標

這次為日志管理搭建一個web管理平台,可以通過web端錄入項目和配置信息,以及項目對應的日志路徑和采集信息,並且寫入etcd,
這樣通過之前編寫的日志采集系統可以根據etcd采集對應的日志。

選擇beego作為web后台開發

web端采用beego框架進行開發,beego是一個采用mvc三層架構設計的web框架。這里闡述下web管理平台的架構和功能。
1.png
components里包含web平台用到的組件,包括beego日志插件,etcd插件,mysql插件。
conf里包含了web的配置信息,配置在app.conf這個文件中。
logs存儲了web平台生成的日志
controllers為mvc架構中的c,也就是控制層,將models數據映射到view中。
models為mvc架構中的m,也就是數據層,負責從mysql中讀取數據以及寫入數據
views為mvc架構中的v,也就是視圖層,這里為web端展示的前端界面。
routers保存了路由回應的回調函數,這樣根據對應的路由調用不同的函數,從而調用不同的controller。
statics存儲了css和js文件,這個主要是網頁端用到的。

代碼流程

main函數中初始化插件,並且啟動beego

func init_components() bool {
	err := components.InitLogger() //調用logger初始化
	if err != nil {
		logs.Warn("initDb failed, err :%v", err)
		return false
	}

	err = components.InitDb()
	if err != nil {
		logs.Warn("initDb failed, err:%v", err)
		return false
	}

	err = components.InitEtcd()
	if err != nil {
		logs.Warn("init etcd failed, err:%v", err)
		return false
	}

	return true
}

func main() {
	if init_components() == false {
		return
	}
	beego.Run()
}

插件初始化具體可以查看我的源碼,之后在最下方我給出源碼鏈接。
接下來我們看看routers中路由規則的注冊

func init() {
	beego.Router("/index", &AppController.AppController{}, "*:AppList")
	beego.Router("/app/list", &AppController.AppController{}, "*:AppList")
	beego.Router("/app/apply", &AppController.AppController{}, "*:AppApply")
	beego.Router("/app/create", &AppController.AppController{}, "*:AppCreate")
	beego.Router("/log/apply", &LogController.LogController{}, "*:LogApply")
	beego.Router("/log/list", &LogController.LogController{}, "*:LogList")
	beego.Router("/log/create", &LogController.LogController{}, "*:LogCreate")
}

/index為首頁展示
/app/list為項目列表
/app/apply為創建項目
/app/create為創建項目后跳轉的路由
/log/apply為創建日志
/log/list為為日志列表展示
/log/create為日志創建成功后跳轉的路由
接下來通過一個路由說說邏輯流程。當用戶在瀏覽器輸入http://localhost:8080/index,
web后端通過路由調用Controller中的AppList函數

func (p *AppController) AppList() {

	logs.Debug("enter index controller")

	p.Layout = "layout/layout.html"
	appList, err := model.GetAllAppInfo()
	if err != nil {
		p.Data["Error"] = fmt.Sprintf("服務器繁忙")
		p.TplName = "app/error.html"

		logs.Warn("get app list failed, err:%v", err)
		return
	}

	logs.Debug("get app list succ, data:%v", appList)
	p.Data["applist"] = appList

	p.TplName = "app/index.html"
}

可以看到我們設置了布局文件和模板文件,並且調用models獲取所有項目的信息,然后設置到data中,通過模板返回。
前端可以通過網頁展示項目列表。GetAllAppInfo獲取項目信息的實現放在model層。

func GetAllAppInfo() (appList []AppInfo, err error) {
    err = Db.Select(&appList, "select app_id, app_name, app_type, 
    create_time, develop_path from tbl_app_info")
	if err != nil {
		logs.Warn("Get All App Info failed, err:%v", err)
		return
	}
	return
}

model通過查詢數據庫將項目信息返回。這些數據存儲在mysql表中。這里其實是通過orm映射,
將數據庫表的數據存儲在我們定義的結構體

type AppInfo struct {
	AppId       int    `db:"app_id"`
	AppName     string `db:"app_name"`
	AppType     string `db:"app_type"`
	CreateTime  string `db:"create_time"`
	DevelopPath string `db:"develop_path"`
	IP          []string
}

數據庫表如下
2.png

測試web管理平台

我們點擊項目申請,填寫項目信息
3.png
提交后可以看到項目列表
4.png
同樣我們點擊日志申請,填寫信息
5.png
查看日志信息
6.png
數據庫表也存儲成功了
7.png

源碼下載

https://github.com/secondtonone1/golang-/tree/master/logcatchsys
感謝關注我的公眾號
wxgzh.jpg
個人微信號1017234088, 添加請注明來源。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM