-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
55 lines (52 loc) · 1.1 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
package main
import (
"github.com/longpi1/consul-tool/internal/backends"
"log"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
// 初始化consul配置信息
cli := backends.NewConfig(backends.WithPrefix("kvTest"))
if err := cli.Init(); err != nil {
log.Fatalln(err)
}
//监听consul中的key: test
err := cli.Watch("test", func(r *backends.KV) {
log.Printf("该key: %s 已经更新", r.Key())
})
if err != nil {
log.Fatalln(err)
}
//插入key
if err := cli.Put("test", "value"); err != nil {
log.Fatalln(err)
}
//读取key
if ret := cli.Get("test"); ret.Err() != nil {
log.Fatalln(ret.Err())
} else {
println(ret.Value())
}
c := make(chan os.Signal, 1)
// 监听退出相关的syscall
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-c
log.Printf("exit with signal %s", s.String())
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
//停止监听对应的路径
cli.StopWatch("test")
time.Sleep(time.Second * 2)
close(c)
return
case syscall.SIGHUP:
default:
close(c)
return
}
}
}