3 Commits

Author SHA1 Message Date
34c9f035ee update 2021-11-26 15:56:30 +08:00
628eac60b4 add watch key with prefix 2021-11-26 14:31:25 +08:00
840ca01f87 更新gin 2021-11-26 12:29:51 +08:00
2 changed files with 25 additions and 7 deletions

4
go.mod
View File

@ -4,8 +4,6 @@ go 1.17
replace github.com/coreos/bbolt v1.3.4 => go.etcd.io/bbolt v1.3.4
replace github.com/gin-gonic/gin v1.7.5 => github.com/gin-gonic/gin v1.7.6
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
require (
@ -13,7 +11,7 @@ require (
github.com/apolloconfig/agollo/v4 v4.0.9
github.com/coreos/etcd v3.3.27+incompatible
github.com/ddliu/go-httpclient v0.6.9
github.com/gin-gonic/gin v1.7.5
github.com/gin-gonic/gin v1.7.6
github.com/go-redis/redis/v8 v8.11.4
github.com/go-redis/redis_rate/v9 v9.1.2
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible

View File

@ -11,14 +11,16 @@ import (
"context"
"math"
"time"
"go.etcd.io/etcd/clientv3"
)
// WatchKey 监听key的变化,永久监听
// WatchKeyWithOption ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:58 下午 2021/11/23
func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) {
// Date : 2:29 下午 2021/11/26
func WatchKeyWithOption(ctx context.Context, watchKey string, callbackFunc WatcherHandler, optionList ...clientv3.OpOption) {
if nil == callbackFunc {
// 变化之后,没有任何逻辑处理,视为不需要监听变化
return
@ -27,7 +29,7 @@ func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler)
ctx = context.Background()
}
rch := Client.Watch(ctx, watchKey) // <-chan WatchResponse
rch := Client.Watch(ctx, watchKey, optionList...) // <-chan WatchResponse
for watchResp := range rch {
for _, ev := range watchResp.Events {
callbackFunc(ev)
@ -35,6 +37,24 @@ func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler)
}
}
// WatchKey 监听key的变化,永久监听
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:58 下午 2021/11/23
func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) {
WatchKeyWithOption(ctx, watchKey, callbackFunc)
}
// WatchWithKeyPrefix ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:30 下午 2021/11/26
func WatchWithKeyPrefix(ctx context.Context, watchKey string, callbackFunc WatcherHandler) {
WatchKeyWithOption(ctx, watchKey, callbackFunc, clientv3.WithPrefix())
}
// WatchKeyWithCancel 可以随时取消的
//
// Author : go_developer@163.com<白茶清欢>