增加watch方法
This commit is contained in:
30
middleware/etcd/watch.go
Normal file
30
middleware/etcd/watch.go
Normal file
@ -0,0 +1,30 @@
|
||||
// Package etcd...
|
||||
//
|
||||
// Description : 监听key的变化
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-11-23 2:58 下午
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// WatchKey 监听key的变化
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2:58 下午 2021/11/23
|
||||
func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) {
|
||||
if nil == callbackFunc {
|
||||
// 变化之后,没有任何逻辑处理,视为不需要监听变化
|
||||
return
|
||||
}
|
||||
rch := Client.Watch(context.Background(), watchKey) // <-chan WatchResponse
|
||||
for watchResp := range rch {
|
||||
for _, ev := range watchResp.Events {
|
||||
callbackFunc(ev)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user