etcd.go 943 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "git.wecise.com/wecise/common/etcd"
  7. )
  8. func main() {
  9. etcdPath := "47.92.151.165:2379"
  10. etcdUser := ""
  11. etcdPass := ""
  12. etcdFile := "/matrix/etc/omdb"
  13. etcdURL := "etcd://" + etcdUser + "@" + etcdPath + "" + etcdFile
  14. fmt.Println(etcdURL)
  15. cli, err := etcd.NewClient(etcdPath, etcdUser, etcdPass)
  16. if err != nil {
  17. fmt.Println(err)
  18. return
  19. }
  20. val, err := cli.Get(etcdFile)
  21. if err != nil {
  22. fmt.Println(err)
  23. return
  24. }
  25. fmt.Println(fmt.Sprintf("load config from %s", etcdURL))
  26. cache_value := []string{""}
  27. on_change := func(value string) {
  28. if cache_value[0] != value {
  29. fmt.Println("config changed:\r\n", value)
  30. }
  31. }
  32. on_change(val)
  33. ch := cli.Watch(context.Background(), etcdFile, false)
  34. go func() {
  35. fmt.Println(fmt.Sprintf("watching %s", etcdURL))
  36. for {
  37. select {
  38. case evt := <-ch:
  39. on_change(evt.Node.Value)
  40. }
  41. }
  42. }()
  43. for {
  44. time.Sleep(1 * time.Minute)
  45. }
  46. }