feature/etcd #6

Merged
zhangdeman merged 5 commits from feature/etcd into master 2021-11-24 14:25:10 +08:00
3 changed files with 57 additions and 8 deletions
Showing only changes of commit 1da5793132 - Show all commits

2
go.mod
View File

@ -8,6 +8,7 @@ replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
require ( require (
github.com/Shopify/sarama v1.30.0 github.com/Shopify/sarama v1.30.0
github.com/coreos/etcd v3.3.27+incompatible
github.com/ddliu/go-httpclient v0.6.9 github.com/ddliu/go-httpclient v0.6.9
github.com/gin-gonic/gin v1.7.5 github.com/gin-gonic/gin v1.7.5
github.com/go-redis/redis/v8 v8.11.4 github.com/go-redis/redis/v8 v8.11.4
@ -32,7 +33,6 @@ require (
github.com/StackExchange/wmi v1.2.1 // indirect github.com/StackExchange/wmi v1.2.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/coreos/bbolt v1.3.4 // indirect github.com/coreos/bbolt v1.3.4 // indirect
github.com/coreos/etcd v3.3.27+incompatible // indirect
github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect

View File

@ -37,12 +37,12 @@ func Put(ctx context.Context, key string, val string, operateTimeout time.Durati
return err return err
} }
// Get 读取数据 // GetWithOption 使用各种option选项读取数据
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 12:09 下午 2021/11/23 // Date : 11:17 上午 2021/11/24
func Get(ctx context.Context, key string, operateTimeout time.Duration) ( func GetWithOption(ctx context.Context, key string, operateTimeout time.Duration, optionList ...clientv3.OpOption) (
[]*mvccpb.KeyValue, []*mvccpb.KeyValue,
error, error,
) { ) {
@ -57,11 +57,45 @@ func Get(ctx context.Context, key string, operateTimeout time.Duration) (
if nil == ctx { if nil == ctx {
ctx = context.Background() ctx = context.Background()
} }
ctx, cancel = context.WithTimeout(context.Background(), time.Second) ctx, cancel = context.WithTimeout(context.Background(), operateTimeout)
result, err = Client.Get(ctx, key) defer cancel()
cancel() if result, err = Client.Get(ctx, key, optionList...); err != nil {
if err != nil {
return nil, err return nil, err
} }
return result.Kvs, nil return result.Kvs, nil
} }
// Get 读取数据,按照key精确匹配
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:09 下午 2021/11/23
func Get(ctx context.Context, key string, operateTimeout time.Duration) (
[]*mvccpb.KeyValue,
error,
) {
return GetWithOption(ctx, key, operateTimeout)
}
// GetWithPrefix 使用key前缀查找
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:58 上午 2021/11/24
func GetWithPrefix(ctx context.Context, keyPrefix string, operateTimeout time.Duration) (
map[string]*mvccpb.KeyValue,
error,
) {
var (
result []*mvccpb.KeyValue
err error
)
if result, err = GetWithOption(ctx, keyPrefix, operateTimeout, clientv3.WithPrefix()); nil != err {
return make(map[string]*mvccpb.KeyValue), err
}
formatResult := make(map[string]*mvccpb.KeyValue)
for _, item := range result {
formatResult[string(item.Key)] = item
}
return formatResult, nil
}

View File

@ -12,6 +12,8 @@ import (
"testing" "testing"
"time" "time"
"git.zhangdeman.cn/zhangdeman/gopkg/util"
"go.etcd.io/etcd/clientv3" "go.etcd.io/etcd/clientv3"
) )
@ -42,6 +44,19 @@ func TestGet(t *testing.T) {
fmt.Println(Get(nil, "name", 0)) fmt.Println(Get(nil, "name", 0))
} }
// TestGetWithPrefix 根据key前缀读取数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:01 上午 2021/11/24
func TestGetWithPrefix(t *testing.T) {
prefix := "/test/dir/"
for i := 0; i < 10; i++ {
_ = Put(nil, fmt.Sprintf("%s%d", prefix, i), util.GenRandomString("", 8), 0)
}
fmt.Println(GetWithPrefix(nil, prefix, 0))
}
// TestWatchKey ... // TestWatchKey ...
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>