-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (84 loc) · 1.81 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"github.com/fsnotify/fsnotify"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/pflag"
"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
"go.uber.org/zap"
"log"
"net/http"
)
func main() {
InitViperWatch()
initLogger()
initPrometheus()
server := InitWebServer()
server.GET("/hello", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "hello")
})
server.Run(":8080")
}
func initPrometheus() {
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8081", nil)
}()
}
func initLogger() {
logger, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
zap.ReplaceGlobals(logger)
defer logger.Sync()
}
func InitViper() {
viper.SetConfigName("dev")
viper.SetConfigType("yaml")
viper.AddConfigPath("config")
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
log.Println(viper.Get("test.key"))
}
func InitViperRemote() {
err := viper.AddRemoteProvider("etcd3", "http://127.0.0.1:12379", "/webook/config")
if err != nil {
panic(err)
}
viper.SetConfigType("yaml")
viper.OnConfigChange(func(in fsnotify.Event) {
log.Println("远程配置中心发生变更:", in.Name)
})
err = viper.ReadRemoteConfig()
if err != nil {
panic(err)
}
go func() {
for {
err := viper.WatchRemoteConfig()
if err != nil {
panic(err)
}
log.Println("远程配置中心监听中...")
}
}()
}
func InitViperWatch() {
cfile := pflag.String("config", "config/dev.yaml", "config file path")
pflag.Parse()
viper.SetConfigType("yaml")
viper.SetConfigFile(*cfile)
//viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) {
log.Println("本地配置文件发生变更:", in.Name)
})
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
log.Println(viper.Get("test.key"))
}