etcd 笔记(03)— etcd 客户端使用(键值的增、删、改、查)、watch监测键、lease使用(创建租约、撤销租约、刷新租期、查询租期)

1. etcd 客户端

etcdctl 是一个命令行客户端,便于我们进行服务测试或手动修改数据库内容,etcdctl 在两个不同的 etcd 版本(v2 和 v3)下的功能和使用方式也完全不同。

一般通过如下方式来指定使用 etcd 的版本:

export ETCDCTL_API=2
export ETCDCTL_API=3

在前面我们已经在 /tmp/etcd-download-test/ 安装了 etcd,那么就可以将当前的安装路径加到环境变量 PATH 中,如下:

vi ~/.bashrc

在最后加入

export PATH=$PATH:/tmp/etcd-download-test/

然后执行以下命令让其生效:

source ~/.bashrc

此时就可以直接输入 etcdctl 命令进行操作了,如下:

wohu@ubuntu-dev:~/tools$ etcdctl -h
NAME:
	etcdctl - A simple command line client for etcd3.

USAGE:
	etcdctl [flags]

VERSION:
	3.4.15

API VERSION:
	3.4

支持的 COMMANDS 见下图:
commands

支持的 OPTIONS 见下图:
options

etcdctl 支持的命令大体上分为数据库操作和非数据库操作两类。

  • 数据库操作基本围绕着对键值和目录的 CRUD 操作(即增删改查);
  • 非数据库操作如用户、角色、授权、认证相关;

etcd 在键的组织上采用了类似文件系统中目录的概念,即层次化的空间结构,我们指定的键可以作为键名,实际上,此时键值对放于根目录 / 下面。

我们也可以为键的存储指定目录结构,如 /cluster/node/key,如果不存在 /cluster/node 目录,则 etcd Server 将会创建相应的目录结构。

2 键值对操作

键操作包括最常用的增删改查操作,包括 PUTGETDELETE 等命令。

  • PUT 设置或者更新某个键的值
wohu@ubuntu-dev:~/tools$ etcdctl put /home/wohu/key1 "hello world1"
OK
wohu@ubuntu-dev:~/tools$ etcdctl put /home/wohu/key2 "hello world2"
OK
wohu@ubuntu-dev:~/tools$ etcdctl put /home/wohu/key3 "hello world3"
OK
wohu@ubuntu-dev:~/tools$ 
  • GET 获取指定键的值
wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/key3 
/home/wohu/key3
hello world3
wohu@ubuntu-dev:~/tools$

加上 --print-value-only 可以读取对应的值。

wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/key3  --print-value-only
hello world3
wohu@ubuntu-dev:~/tools$ 

GET 通过选项 --hex 获取指定键的对应值的 16 进制格式

wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/key3 --hex
\x2f\x68\x6f\x6d\x65\x2f\x77\x6f\x68\x75\x2f\x6b\x65\x79\x33
\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x33
wohu@ubuntu-dev:~/tools$ 

十六进制在 etcd 中有多处使用,如租约 ID 也是十六进制。

  • GET 指定键范围内的值
wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/key1 /home/wohu/key3
/home/wohu/key1
hello world1
/home/wohu/key2
hello world2
wohu@ubuntu-dev:~/tools$

上述操作获取了大于等于 /home/wohu/key1,且小于 /home/wohu/key3 的键值对。key3 不在范围之内,因为范围是半开区间 [key1, key3),不包含 key3

  • 通过 --prefix 获取指定键前缀的所有键值对
wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/ --prefix
/home/wohu/key1
hello world1
/home/wohu/key2
hello world2
/home/wohu/key3
hello world3
wohu@ubuntu-dev:~/tools$ 

这样就能获取所有以 /home/wohu/ 开头的键值对,当前缀获取的结果过多时,还可以通过 --limit=N 限制获取的数量,其中 N 为指定的要返回的个数。

wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/ --prefix --limit=2
/home/wohu/key1
hello world1
/home/wohu/key2
hello world2
wohu@ubuntu-dev:~/tools$ 
  • DELETE 删除一个键或者特定范围的键
  1. 删除单个键
wohu@ubuntu:~$ etcdctl get /home/wohu/key4
/home/wohu/key4
hello world4
wohu@ubuntu:~$ etcdctl del /home/wohu/key4
1
wohu@ubuntu:~$ etcdctl get /home/wohu/key4
wohu@ubuntu:~$ 
  1. 删除某一范围的键,删除键区间为 [key1 key3),返回已经删除键的个数
wohu@ubuntu:~$ etcdctl del /home/wohu/key1 /home/wohu/key3
2
wohu@ubuntu:~$ 
  1. --prev-kv 删除键并返回该键的值
wohu@ubuntu:~$ etcdctl del /home/wohu/key1 --prev-kv 
1
/home/wohu/key1
hello world1
wohu@ubuntu:~$ etcdctl get /home/wohu/key1
wohu@ubuntu:~$ 
  1. --prefix 删除指定前缀的键
wohu@ubuntu:~$ etcdctl get /home/wohu/key --prefix
/home/wohu/key1
hello world1
/home/wohu/key2
hello world2
/home/wohu/key3
hello world3
wohu@ubuntu:~$ etcdctl del /home/wohu/key --prefix
3
wohu@ubuntu:~$ etcdctl get /home/wohu/key --prefix
wohu@ubuntu:~$ 

3 watch 对象

watch 监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。

其中 watch 终端和 put 分别在不同的终端。

  1. 监听单个键
    图1

  2. 监听某个范围的键
    图2

etcd 保存修订版本以便应用客户端可以读取键的历史版本。但是,为了避免积累无限数量的历史数据,需要对历史的修订版本进行压缩 compact 。经过压缩,etcd 删除历史修订版本,释放存储空间,且在压缩修订版本之前的数据将不可访问。

4 lease 对象

lease 意为租约,类似于 Redis 中的 TTL(Time To Live)etcd 中的键值对可以绑定到租约上,实现存活周期控制。在实际应用中,常用来实现服务的心跳,即服务在启动时获取租约,将租约与服务地址绑定,并写入 etcd 服务器,为了保持心跳状态,服务会定时刷新租约。

4.1 授予租约

应用客户端可以为 etcd 集群里面的键授予租约。当键被附加到租约时,它的存活时间被绑定到租约的存活时间,而租约的存活时间相应的被 TTL 管理。在授予租约时,每个租约的最小 TTL 值由应用客户端指定。

一旦租约的 TTL 到期,租约就会过期并且所有附带的键都将被删除。

创建一个租约,时间为 100s

wohu@ubuntu:~$ etcdctl lease grant 100
lease 694d78e3bc12d11a granted with TTL(100s)

将创建的租约绑定到键 /home/wohu/key1 上

wohu@ubuntu:~$ etcdctl put /home/wohu/key1 "hello" --lease=694d78e3bc12d11a
OK

查询键,在租约有效期内,可以获取到键值,租约到期后键值对被删除。

wohu@ubuntu:~$ etcdctl get /home/wohu/key1 
/home/wohu/key1
hello
wohu@ubuntu:~$ etcdctl get /home/wohu/key1 
wohu@ubuntu:~$ 

4.2 撤销租约

应用通过租约 ID 可以撤销租约。撤销租约将删除所有附带的 key

wohu@ubuntu:~$ etcdctl lease grant 100000
lease 694d78e3bc12d122 granted with TTL(100000s)
wohu@ubuntu:~$ etcdctl put /home/wohu/key1 "hello" --lease=694d78e3bc12d122
OK
wohu@ubuntu:~$ etcdctl lease revoke 694d78e3bc12d122
lease 694d78e3bc12d122 revoked
wohu@ubuntu:~$ etcdctl get /home/wohu/key1 

4.3 刷新租期

应用程序可以通过刷新其 TTL 保持租约存活,确保其不会过期。

wohu@ubuntu:~$ etcdctl lease grant 30
lease 694d78e3bc12d134 granted with TTL(30s)
wohu@ubuntu:~$ etcdctl lease keep-alive  694d78e3bc12d134
lease 694d78e3bc12d134 keepalived with TTL(30)
lease 694d78e3bc12d134 keepalived with TTL(30)
lease 694d78e3bc12d134 keepalived with TTL(30)
lease 694d78e3bc12d134 keepalived with TTL(30)
lease 694d78e3bc12d134 keepalived with TTL(30)

4.4 查询租期

客户端可以查询租赁信息,检查续订或租赁的状态,是否存在或者是否已过期。应用客户端还可以查询特定租约绑定的 key

wohu@ubuntu:~$ etcdctl lease grant 300
lease 694d78e3bc12d137 granted with TTL(300s)
wohu@ubuntu:~$ etcdctl put /home/wohu/key1 "hello" --lease=694d78e3bc12d137
OK
wohu@ubuntu:~$ etcdctl lease timetolive 694d78e3bc12d137
lease 694d78e3bc12d137 granted with TTL(300s), remaining(271s)
wohu@ubuntu:~$ etcdctl lease timetolive 694d78e3bc12d137 --keys
lease 694d78e3bc12d137 granted with TTL(300s), remaining(261s), attached keys([/home/wohu/key1])
wohu@ubuntu:~$ 
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用jetcd-core可以通过以下步骤获取etcd下的文件目录: 1. 创建EtcdClient对象,连接到etcd服务器: ```java EtcdClient etcdClient = new EtcdClient(URI.create("http://localhost:2379")); ``` 2. 使用get方法获取指定key的值,设置withPrefix参数为true可以获取该key的所有子节点: ```java CompletableFuture<GetResponse> getFuture = etcdClient.get("key", GetOption.newBuilder().withPrefix(true).build()); GetResponse getResponse = getFuture.get(); ``` 3. 遍历getResponse.getKvs(),获取所有子节点的key和value: ```java for (KeyValue keyValue : getResponse.getKvs()) { String key = keyValue.getKey().toStringUtf8(); String value = keyValue.getValue().toStringUtf8(); System.out.println(key + " -> " + value); } ``` 完整代码示例: ```java import io.etcd.jetcd.ByteSequence; import io.etcd.jetcd.Client; import io.etcd.jetcd.KV; import io.etcd.jetcd.KV.GetOption; import io.etcd.jetcd.KV.GetResponse; import io.etcd.jetcd.Lease; import io.etcd.jetcd.LeaseGrantResponse; import io.etcd.jetcd.Lock; import io.etcd.jetcd.Txn; import io.etcd.jetcd.Watch; import io.etcd.jetcd.auth.Auth; import io.etcd.jetcd.auth.Permission; import io.etcd.jetcd.auth.Role; import io.etcd.jetcd.auth.User; import io.etcd.jetcd.cluster.Cluster; import io.etcd.jetcd.launcher.EtcdCluster; import io.etcd.jetcd.launcher.EtcdClusterFactory; import io.etcd.jetcd.launcher.EtcdClusterSpec; import io.etcd.jetcd.lock.LockResponse; import io.etcd.jetcd.options.PutOption; import io.etcd.jetcd.support.CloseableClient; import io.etcd.jetcd.support.Scheduler; import io.etcd.jetcd.support.Schedulers; import io.etcd.jetcd.watch.WatchEvent; import io.etcd.jetcd.watch.WatchResponse; import java.net.URI; import java.util.concurrent.CompletableFuture; public class EtcdExample { public static void main(String[] args) throws Exception { // 创建EtcdClient对象,连接到etcd服务器 EtcdClient etcdClient = new EtcdClient(URI.create("http://localhost:2379")); // 使用get方法获取指定key的值,设置withPrefix参数为true可以获取该key的所有子节点 CompletableFuture<GetResponse> getFuture = etcdClient.get("key", GetOption.newBuilder().withPrefix(true).build()); GetResponse getResponse = getFuture.get(); // 遍历getResponse.getKvs(),获取所有子节点的key和value for (KeyValue keyValue : getResponse.getKvs()) { String key = keyValue.getKey().toStringUtf8(); String value = keyValue.getValue().toStringUtf8(); System.out.println(key + " -> " + value); } // 关闭EtcdClient对象 etcdClient.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值