騰訊 Tars 框架中,有兩種能夠稱之爲 「配置」 的地方:其中一個是能夠自定義的,在 Tars 管理頁面中稱爲 「服務配置」。在這裏,能夠按照開發者喜歡的格式上傳文件,而且支持配置的熱更新。git
而另外一個地方,則是本文要講的。這個在 Tars 中稱爲 「模版」。模版能夠在 Tars 管理平臺頂部的 「運維管理」 進去後,再進入 「模版管理」 能夠看到。github
查看下面那些已有的模版能夠看到,通常而言,模版是用於對整個服務的參數進行配置,其配置項更加偏向運維,而不是偏業務。shell
TarsGo 服務通常使用 tars.default
模板。查看這個模板的內容,能夠看到以下:segmentfault
<tars> <application> enableset=${enableset} #是否啓用SET分組 setdivision=${setdivision} #SET分組的全名.(mtt.s.1) <client> locator =${locator} #地址 sync-invoke-timeout = 3000 #同步調用超時時間,缺省3s(毫秒) async-invoke-timeout =5000 #異步超時時間,缺省5s(毫秒) refresh-endpoint-interval = 60000 #從新獲取服務列表時間間隔(毫秒) stat = tars.tarsstat.StatObj #模塊間調用服務[可選] property = tars.tarsproperty.PropertyObj #屬性上報服務[可選] report-interval = 60000 #上報間隔時間,默認60s(毫秒) sample-rate = 100000 #stat採樣比1:n 例如sample-rate爲1000時 採樣比爲千分之一 max-sample-count = 50 #1分鐘內stat最大采樣條數 asyncthread = ${asyncthread} #網絡異步回調線程個數 modulename = ${modulename} #模塊名稱 </client> <server> app = ${app} #應用名稱 server = ${server} #服務名稱 localip = ${localip} #本地ip local = ${local} #本地管理套接字[可選] basepath = ${basepath} #服務的數據目錄,可執行文件,配置文件等 datapath = ${datapath} logpath = ${logpath} #日誌路徑 logsize = 10M #日誌大小 lognum = 10 #日誌數量 config = tars.tarsconfig.ConfigObj #配置中心的地址[可選] notify = tars.tarsnotify.NotifyObj #信息中心的地址[可選] log = tars.tarslog.LogObj #遠程LogServer[可選] deactivating-timeout = 3000 #關閉服務時等待時間 logLevel=DEBUG #滾動日誌等級默認值 </server> </application> </tars>
上面這些就是 Tars 平臺中固定配置的一些參數。在 TarsGo 中,咱們能夠以以下參數來讀取:服務器
import ( "github.com/TarsCloud/TarsGo/tars" ) func main() { cfg := tars.GetServerConfig() // 1 server := cfg.App + "." + cfg.Server // 2 ... }
說明以下:網絡
<server>
下的配置總項,也就是對應着配置的 「域」 爲 "tars/application/server"
<server>
下的 app
和 server
值可是上面代碼的 app
和 server
都是預約義值,若是咱們額外添加了一個鍵值對,那麼用這個方法就無法讀到了。app
這裏須要提一下如何自定義模版配置。咱們點擊 Tars 管理平臺的 「服務管理」,再點擊具體的服務,好比這裏我拿以前的例子中用到的 GoWebServer
爲例,點擊具體服務,而後在 「服務管理」 tab 中,在指定的服務點 「編輯」:框架
在談出來的對話框中,拉到最下方,在 「私有模板」 中能夠配置本身的值。若是配置了與默認模版相同的 key,則 tars 會將值替換爲這裏所配置的私有模板值。能夠看到在圖中我覆蓋修改了遠程日誌的服務器。運維
這裏其實咱們只要看一下 TarsGo 的代碼,從 GetServerConfig()
函數(文件是 config.go)往裏看。函數調用了 Init()
,而這個函數又調用了 initConfig()
函數(文件是 application.go)。該函數的邏輯很清晰,就是利用 github.com/TarsCloud/TarsGo/tars/util/conf
包來解析模版文件的內容。dom
解析模版條件的基本邏輯是:
<server>...</server>
,那麼這個屬性下自己會被存儲爲一個 Go map[string]string
類型,下面保存以 key = value
格式存儲的鍵值對信息所以,咱們就能夠利用這個 util 包,來獲取解析到的原始配置值了
爲了便於使用,我基於 github.com/TarsCloud/TarsGo/tars/util/conf
封裝了一個簡易的包來讀取自定義的模版,安裝以下:
$ go get github.com/Andrew-M-C/tarsgo-tools/config
好比我自定義瞭如下私有模版:
<tars> <application> <server> myStr=This is a string myInt=54321 myLong=12345 myErrorInt=abcde </server> </application> </tars>
那麼在服務啓動時我就能夠這樣去讀取:
import ( "github.com/Andrew-M-C/tarsgo-tools/config" ) func main() { tarsconf, err := config.NewConfig() if err != nil { fmt.Println("Failed to get config: " + err.Error()) } else { myStr, exist := tarsconf.GetString("/tars/application/server", "myStr", "WHAT?") fmt.Printf("%t, myStr: %s\n", exist, myStr) myInt, exist := tarsconf.GetInt("/tars/application/server", "myInt") fmt.Printf("%t, myInt: %d\n", exist, myInt) myInt2, exist := tarsconf.GetInt("/tars/application/server", "myInt2", -2) fmt.Printf("%t, myInt2: %d\n", exist, myInt2) myLong, exist := tarsconf.GetLong("/tars/application/server", "myLong", -3) fmt.Printf("%t, myLong: %d\n", exist, myLong) myErrorInt, exist := tarsconf.GetInt("/tars/application/server", "myInt", -4) fmt.Printf("%t, myErrorInt: %d\n", exist, myErrorInt) } return }
命令行輸出:
true, myStr: This is a string true, myInt: 54321 false, myInt2: -2 true, myLong: 12345 false, myErrorInt: -4
這就成功讀到自定義的配置值啦。其中對於每個 GetXxx()
函數,均可以傳入三個參數:
domain
,也就是前文提到的 「域」 名key
,指的是指定域下鍵值對的鍵名exist
爲 false
的時候,接口會將該值做爲默認值,賦給返回值,開發者能夠用來節省一些代碼操做。須要注意的是,其中 myErrorInt
的值儘管是存在的,可是因爲沒法解析爲數字,所以 GetInt()
函數返回的 exist
結果爲 false
。
本文章採用 知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議 進行許可。
原做者: amc,歡迎轉載,但請按上述協議註明出處。
本文連接:http://www.javashuo.com/article/p-zvoemcrb-et.html
原文連接:https://cloud.tencent.com/developer/article/1394093,也是本人的博客
原文標題:《騰訊 Tars-Go 服務獲取自定義模版(配置)值》
發佈日期:2019 年 2 月 25 日
發佈平臺:SegmentFault