【Zinx第四章-全域性配置】Golang輕量級併發伺服器框架
四、Zinx的全域性配置
隨著架構逐步的變大,引數就會越來越多,為了省去我們後續大頻率修改引數的麻煩,接下來Zinx需要做一個載入配置的模組,和一個全域性獲取Zinx引數的物件。
4.1 Zinx-V0.4增添全域性配置程式碼實現
我們先做一個簡單的載入配置模組,要載入的配置檔案的文字格式,就選擇比較通用的json
格式,配置資訊暫時如下:
zinx.json
{ "Name":"demo server", "Host":"127.0.0.1", "TcpPort":7777, "MaxConn":3 }
現在我們需要建立一個全域性配置資訊的物件
A) 建立全域性引數檔案
建立zinx/utils
資料夾,在下面建立globalobj.go
檔案,暫時編寫如下。
zinx/utils/globalobj.go
package utils import ( "encoding/json" "io/ioutil" "zinx/ziface" ) /* 儲存一切有關Zinx框架的全域性引數,供其他模組使用 一些引數也可以通過 使用者根據 zinx.json來配置 */ type GlobalObj struct { TcpServer ziface.IServer //當前Zinx的全域性Server物件 Hoststring//當前伺服器主機IP TcpPortint//當前伺服器主機監聽埠號 Namestring//當前伺服器名稱 Versionstring//當前Zinx版本號 MaxPacketSize uint32 //都需資料包的最大值 MaxConnint//當前伺服器主機允許的最大連結個數 } /* 定義一個全域性的物件 */ var GlobalObject *GlobalObj
我們在全域性定義了一個GlobalObject
物件,目的就是讓其他模組都能訪問到裡面的引數。
B) 提供init初始化方法
然後我們提供一個init()
方法,目的是初始化GlobalObject
物件,和載入服務端應用配置檔案conf/zinx.json
zinx/utils/globalobj.go
//讀取使用者的配置檔案 func (g *GlobalObj) Reload() { data, err := ioutil.ReadFile("conf/zinx.json") if err != nil { panic(err) } //將json資料解析到struct中 //fmt.Printf("json :%s\n", data) err = json.Unmarshal(data, &GlobalObject) if err != nil { panic(err) } } /* 提供init方法,預設載入 */ func init() { //初始化GlobalObject變數,設定一些預設值 GlobalObject = &GlobalObj{ Name:"ZinxServerApp", Version: "V0.4", TcpPort: 7777, Host:"0.0.0.0", MaxConn: 12000, MaxPacketSize:4096, } //從配置檔案中載入一些使用者配置的引數 GlobalObject.Reload() }
C) 硬引數替換與Server初始化引數配置
zinx/znet/server.go
/* 建立一個伺服器控制代碼 */ func NewServer () ziface.IServer { //先初始化全域性配置檔案 utils.GlobalObject.Reload() s:= &Server { Name :utils.GlobalObject.Name,//從全域性引數獲取 IPVersion:"tcp4", IP:utils.GlobalObject.Host,//從全域性引數獲取 Port:utils.GlobalObject.TcpPort,//從全域性引數獲取 Router: nil, } return s }
我們未來方便驗證我們的引數已經成功被價值,在Server.Start()
方法中加入幾行除錯資訊
zinx/znet/server.go
//開啟網路服務 func (s *Server) Start() { fmt.Printf("[START] Server name: %s,listenner at IP: %s, Port %d is starting\n", s.Name, s.IP, s.Port) fmt.Printf("[Zinx] Version: %s, MaxConn: %d,MaxPacketSize: %d\n", utils.GlobalObject.Version, utils.GlobalObject.MaxConn, utils.GlobalObject.MaxPacketSize) //... //... }
當然還有一些其他的之前寫死的數值,均可以在配置檔案配置,用全域性引數替換,這裡不一一列舉。
當前zinx框架目錄結構
├── README.md ├── utils │└── globalobj.go ├── ziface │├── iconnnection.go │├── irequest.go │├── irouter.go │└── iserver.go └── znet ├── connection.go ├── request.go ├── router.go ├── server.go └── server_test.go
4.2 使用Zinx-V0.4完成應用程式
我們這回再基於Zinx完成伺服器就必須要提前先寫好一個conf/zinx.json
配置檔案了。
├── Client.go ├── conf │└── zinx.json └── Server.go
Server.go
package main import ( "fmt" "zinx/ziface" "zinx/znet" ) //ping test 自定義路由 type PingRouter struct { znet.BaseRouter } //Test Handle func (this *PingRouter) Handle(request ziface.IRequest) { fmt.Println("Call PingRouter Handle") _, err := request.GetConnection().GetTCPConnection().Write([]byte("ping...ping...ping\n")) if err != nil { fmt.Println("call back ping ping ping error") } } func main() { //建立一個server控制代碼 s := znet.NewServer() //配置路由 s.AddRouter(&PingRouter{}) //開啟服務 s.Serve() }
$go run Server.go
結果:
$ go run Server.go Add Router succ! [START] Server name: demo server,listenner at IP: 127.0.0.1, Port 7777 is starting [Zinx] Version: V0.4, MaxConn: 3, MaxPacketSize: 4096 start Zinx serverdemo serversucc, now listenning...
現在配置已經載入成功了。
關於作者:
作者:Aceld(劉丹冰)
簡書號:IT無崖子
mail:[email protected]
github:https://github.com/aceld
原創書籍gitbook:http://legacy.gitbook.com/@aceld