加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
xmx0632 提交于 2020-07-02 18:09 . batch fetch config files
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"nacos-prometheus-discovery/model"
"nacos-prometheus-discovery/service"
"os"
"os/signal"
"time"
)
const (
DefaultConfigPath = "conf/config.json"
MODE_CONFIG = "config"
MODE_SERVICE = "service"
)
func init() {
log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
}
func main() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, os.Kill)
log.Println("Nacos Prometheus Discovery Starting ...")
filename := DefaultConfigPath
if len(os.Args) > 1 {
argsWithoutProg := os.Args[1:2]
log.Println("config file path:", argsWithoutProg)
filename = argsWithoutProg[0]
}
// read config file
configJson, configErr := ioutil.ReadFile(filename)
if configErr != nil {
log.Fatal("read config file error.", configErr)
}
log.Println("configJson:", string(configJson))
config := model.Config{}
json.Unmarshal(configJson, &config)
log.Println("config.IntervalInSecond:", config.IntervalInSecond)
// start timer
ticker := time.NewTicker(time.Second * time.Duration(config.IntervalInSecond))
defer ticker.Stop()
done := make(chan bool)
go func() {
// listen terminate sig and stop
s := <-c
fmt.Println("terminated.", s)
done <- true
}()
for {
select {
case <-done:
fmt.Println("Exit!")
return
case t := <-ticker.C:
log.Printf("Current time: %s", t)
service.FetchPrometheusConfig(config)
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化