diff --git a/config/config.go b/config/config.go index 9e31b9bd..09c74bdd 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "log" "github.com/cloudwego/kitex/pkg/klog" + "github.com/fsnotify/fsnotify" "github.com/spf13/viper" ) @@ -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 { @@ -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 @@ -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"), } } diff --git a/config/config_exmple.yaml b/config/config_exmple.yaml index b9aead5a..91ee3fdd 100644 --- a/config/config_exmple.yaml +++ b/config/config_exmple.yaml @@ -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 diff --git a/deploy/config/config.yaml b/deploy/config/config.yaml index 8066c857..ae041bc3 100644 --- a/deploy/config/config.yaml +++ b/deploy/config/config.yaml @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index 9078dbb1..5fca3e40 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/go.mod b/go.mod index 3aea6a1c..b90e6e60 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 01fb21ff..1bc7742b 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -7,6 +7,9 @@ const ( JWTValue = "MTAxNTkwMTg1Mw==" StartID = 10000 + // Viper config + // etcdAddress = "http://127.0.0.1:2379" + // redis ReidsDB_Chat = 1 RedisDBFollow = 2