Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: viper use dymanic update config
Browse files Browse the repository at this point in the history
  • Loading branch information
ozline committed Sep 3, 2023
1 parent 6912a39 commit 3e603cd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
25 changes: 17 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"

"github.com/cloudwego/kitex/pkg/klog"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)

Expand All @@ -17,18 +18,20 @@ var (
Redis *redis
OSS *oss
Elasticsearch *elasticsearch

runtime_viper = viper.New()
)

func Init(path string, service string) {
viper.SetConfigType("yaml")
viper.AddConfigPath(path)
runtime_viper.SetConfigType("yaml")
runtime_viper.AddConfigPath(path)

// use etcd for config save
// viper.AddRemoteProvider("etcd", "http://127.0.0.1:2379", "/config/config.yaml")

klog.Infof("config path: %v\n", path)

if err := viper.ReadInConfig(); err != nil {
if err := runtime_viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
klog.Fatal("could not find config files")
} else {
Expand All @@ -38,17 +41,23 @@ func Init(path string, service string) {
}

configMapping(service)

// 持续监听配置
runtime_viper.OnConfigChange(func(e fsnotify.Event) {
klog.Infof("config file changed: %v\n", e.String())
})
runtime_viper.WatchConfig()
}

func configMapping(srv string) {
c := new(config)
if err := viper.Unmarshal(&c); err != nil {
if err := runtime_viper.Unmarshal(&c); err != nil {
log.Fatal(err)
}
Snowflake = &c.Snowflake

Server = &c.Server
Server.Secret = []byte(viper.GetString("server.jwt-secret"))
Server.Secret = []byte(runtime_viper.GetString("server.jwt-secret"))

Etcd = &c.Etcd
Mysql = &c.MySQL
Expand All @@ -60,12 +69,12 @@ func configMapping(srv string) {
}

func GetService(srvname string) *service {
addrlist := viper.GetStringSlice("services." + srvname + ".addr")
addrlist := runtime_viper.GetStringSlice("services." + srvname + ".addr")

return &service{
Name: viper.GetString("services." + srvname + ".name"),
Name: runtime_viper.GetString("services." + srvname + ".name"),
AddrList: addrlist,
LB: viper.GetBool("services." + srvname + ".load-balance"),
LB: runtime_viper.GetBool("services." + srvname + ".load-balance"),
}
}

Expand Down
2 changes: 1 addition & 1 deletion config/config_exmple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ oss:

elasticsearch:
addr: 127.0.0.1:9200
host: localhost
host: 127.0.0.1

etcd:
addr: 127.0.0.1:2379
Expand Down
2 changes: 1 addition & 1 deletion deploy/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ oss:

elasticsearch:
addr: 127.0.0.1:9200
host: localhost
host: 127.0.0.1

etcd:
addr: 127.0.0.1:2379
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,27 @@ services:
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_ADVERTISE_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_API=3
- ALLOW_NONE_AUTHENTICATION=yes
# command: ["etcd", "--config-file=/config/config.yaml"]
# volumes:
# - ./config:/config
networks:
- tiktok

etcd-keeper:
hostname: etcdkeeper
image: evildecay/etcdkeeper
environment:
- ETCD_HOSTS=http://etcd:2379
- SW_STORAGE_ETCD_CLUSTER_NODES=etcd:2379
- HOST=0.0.0.0
- HOSTNAME=etcd-keeper
links:
- etcd
ports:
- 8099:8080
depends_on:
- etcd
networks:
- tiktok

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down
3 changes: 3 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const (
JWTValue = "MTAxNTkwMTg1Mw=="
StartID = 10000

// Viper config
// etcdAddress = "http://127.0.0.1:2379"

// redis
ReidsDB_Chat = 1
RedisDBFollow = 2
Expand Down

0 comments on commit 3e603cd

Please sign in to comment.