etcd v3 php,etcd v3命令和API

etcd v3版本和以前的v2在使用和操作上面还是有些区别的,首先是命令区别其次是使用上面。先看命令行使用,我尽量用命令,不需要太多的文字描述,最大的区别莫过于ls命令没有了。

创建、更新key

etcdctl put /test/ok 11

OK

etcdctl put /test/ok 22

OK

删除key

etcdctl del /test/gg

1

#删除所有/test前缀的节点

etcdctl del /test --prefix

4

查询key

etcdctl get /test/ok

/test/ok

11

# 前缀查询

etcdctl get /test/ok --prefix

/test/ok

11

/test/ok/gg

88

watch key

etcdctl watch /test/ok

PUT

/test/ok

11

#如果监听子节点

etcdctl watch /test/ok --prefix

PUT

/test/ok

11

PUT

/test/ok/first

11

申请租约

从申请开始计算时间

etcdctl lease grant 40 lease 4e5e5b853f528859 granted with TTL(40s)

授权租约

节点的生命伴随着租约到期将会被DELETE

etcdctl put --lease=4e5e5b853f528859 /test/ok/first xx

OK

撤销租约

撤销租约和租约到期一样,节点都会被删除

etcdctl lease revoke 4e5e5b853f5286cc

lease 4e5e5b853f5286cc revoked

租约续约

每当到期将会续约

etcdctl lease keep-alive 4e5e5b853f52892b

lease 4e5e5b853f52892b keepalived with TTL(40)

lease 4e5e5b853f52892b keepalived with TTL(40)

. . .

上面介绍了命令行的基本使用。补充一下,当前默认还是v2版本通过设定环境变量export ETCDCTL_API=3,设置成V3版本。

v3版本支持rpc的远程调用,这样比http的方式效率更高

先看API定义

关于key value操作

type KV interface { Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error) Get(ctx context.Context, key string, opts ...OpOption) (*GetResponse, error) Delete(ctx context.Context, key string, opts ...OpOption) (*DeleteResponse, error) Compact(ctx context.Context, rev int64, opts ...CompactOption) (*CompactResponse, error) Do(ctx context.Context, op Op) (OpResponse, error) Txn(ctx context.Context) Txn }

关于租约

type Lease interface { Grant(ctx context.Context, ttl int64) (*LeaseGrantResponse, error) Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error) TimeToLive(ctx context.Context, id LeaseID, opts ...LeaseOption) (*LeaseTimeToLiveResponse, error) KeepAlive(ctx context.Context, id LeaseID) (

还有认证和watch等,不一一列举,看看client里面统一由哪些方法

type Client struct { Cluster KV Lease Watcher Auth Maintenance conn *grpc.ClientConn cfg Config creds *credentials.TransportCredentials balancer *simpleBalancer retryWrapper retryRpcFunc retryAuthWrapper retryRpcFunc ctx context.Context cancel context.CancelFunc // Username is a username for authentication Username string // Password is a password for authentication Password string // tokenCred is an instance of WithPerRPCCredentials()'s argument tokenCred *authTokenCredential }

看看具体怎么使用吧,由于篇幅问题下面之介绍一下简单使用:

package main

import (

"github.com/coreos/etcd/clientv3"

"time"

"golang.org/x/net/context"

"fmt"

)

var (

dialTimeout = 5 * time.Second

requestTimeout = 10 * time.Second

endpoints = []string{"10.39.0.6:2379",}

)

func main() {

cli, err := clientv3.New(clientv3.Config{

Endpoints: []string{"10.39.0.6:2379"},

DialTimeout: dialTimeout,

})

if err != nil {

println(err)

}

defer cli.Close()

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)

_, err = cli.Put(ctx, "/test/hello", "world")

cancel()

ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)

resp,err := cli.Get(ctx, "/test/hello")

cancel()

for _, ev := range resp.Kvs {

fmt.Printf("%s : %s\n", ev.Key, ev.Value)

}

_, err = cli.Put(context.TODO(), "key", "xyz")

ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)

_, err = cli.Txn(ctx).

If(clientv3.Compare(clientv3.Value("key"), ">", "abc")).

Then(clientv3.OpPut("key", "XYZ")).

Else(clientv3.OpPut("key", "ABC")).

Commit()

cancel()

rch := cli.Watch(context.Background(), "/test/hello", clientv3.WithPrefix())

for wresp := range rch {

for _, ev := range wresp.Events {

fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)

}

}

if err != nil {

println(err)

}

}

最后面是一个事务处理Txn,watch是会等待监控,程序不会结束!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值