12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package main
- import (
- "context"
- "fmt"
- "time"
- "git.wecise.com/wecise/common/etcd"
- )
- func main() {
- etcdPath := "47.92.151.165:2379"
- etcdUser := ""
- etcdPass := ""
- etcdFile := "/matrix/etc/omdb"
- etcdURL := "etcd://" + etcdUser + "@" + etcdPath + "" + etcdFile
- fmt.Println(etcdURL)
- cli, err := etcd.NewClient(etcdPath, etcdUser, etcdPass)
- if err != nil {
- fmt.Println(err)
- return
- }
- val, err := cli.Get(etcdFile)
- if err != nil {
- fmt.Println(err)
- return
- }
- fmt.Println(fmt.Sprintf("load config from %s", etcdURL))
- cache_value := []string{""}
- on_change := func(value string) {
- if cache_value[0] != value {
- fmt.Println("config changed:\r\n", value)
- }
- }
- on_change(val)
- ch := cli.Watch(context.Background(), etcdFile, false)
- go func() {
- fmt.Println(fmt.Sprintf("watching %s", etcdURL))
- for {
- select {
- case evt := <-ch:
- on_change(evt.Node.Value)
- }
- }
- }()
- for {
- time.Sleep(1 * time.Minute)
- }
- }
|