- 引入jectd依赖
<dependency>
<groupId>io.etcd</groupId>
<artifactId>jetcd-core</artifactId>
<version>0.5.11</version>
</dependency>
- 使用
1、KV client使用
public class EtcdDemo {
public static void main(String[] args) throws Exception{
Client client = Client.builder()
.endpoints("http://localhost:2379","http://localhost:12379"
,"http://localhost:22379").build();
KV kv = client.getKVClient();
ByteSequence key = ByteSequence.from("sample".getBytes());
ByteSequence value = ByteSequence.from("sample_value".getBytes());
// 设置kv
PutResponse putResponse = kv.put(key,value).get();
System.out.println(putResponse );
// 获取kv
GetResponse getResponse = kv.get(key, GetOption.newBuilder().isPrefix(true).build()).get();
System.out.println(getResponse );
// 删除kv
DeleteResponse deleteResponse = kv.delete(key).get();
System.out.println(response2);
}
}
2、watch client使用
public class EtcdDemo {
public static void main(String[] args) throws Exception{
Client client = Client.builder()
.endpoints("http://localhost:2379","http://localhost:12379"
,"http://localhost:22379").build();
Watch watch = client.getWatchClient();
WatchOption watchOption = WatchOption.newBuilder()
.isPrefix(true)
.withNoDelete(true)
.build();
watch.watch(key, watchResponse -> {
for (WatchEvent event : watchResponse.getEvents()) {
System.out.println("eventType:" + event.getEventType()
+ ",key:" + event.getKeyValue().getKey().toString()
+ ",value:"+ event.getKeyValue().getValue().toString());
}
});
}
}
3、lease client使用
public class EtcdDemo {
public static void main(String[] args) throws Exception{
Client client = Client.builder()
.endpoints("http://localhost:2379","http://localhost:12379"
,"http://localhost:22379").build();
// 创建租约
LeaseGrantResponse leaseGrantResponse = lease.grant(3000).get();
System.out.println("lease:(" + leaseGrantResponse + ")");
PutOption putOption = PutOption.newBuilder().withLeaseId(leaseGrantResponse.getID()).build();
PutResponse putResponse = kv.put(key,value, putOption).get();
System.out.println("put:(" + putResponse + ")");
// 续约租约一次
LeaseKeepAliveResponse leaseKeepAliveResponse = lease.keepAliveOnce(leaseGrantResponse.getID()).get();
System.out.println("keepAliveOnce:(" + leaseKeepAliveResponse + ")");
// 保持租户一直存活
lease.keepAlive(leaseGrantResponse.getID(), new StreamObserver<LeaseKeepAliveResponse>() {
@Override
public void onNext(LeaseKeepAliveResponse leaseKeepAliveResponse) {
System.out.println("keepAlive:(" + leaseKeepAliveResponse + ")");
}
@Override
public void onError(Throwable throwable) {
System.out.println("keepAlive error:(" + throwable + ")");
}
@Override
public void onCompleted() {
System.out.println("keepAlive completed");
}
});
// 获取租约剩余存活时间
LeaseOption leaseOption = LeaseOption.newBuilder().withAttachedKeys().build();
LeaseTimeToLiveResponse leaseTimeToLiveResponse = lease.timeToLive(leaseGrantResponse.getID(),leaseOption).get();
System.out.println("timeToLive:(" + leaseTimeToLiveResponse + ")");
// 回收租约
LeaseRevokeResponse leaseRevokeResponse = lease.revoke(leaseGrantResponse.getID()).get();
System.out.println("revoke:(" + leaseRevokeResponse + ")");
}
}
4、lock client使用
public class EtcdDemo {
public static void main(String[] args) throws Exception{
Client client = Client.builder()
.endpoints("http://localhost:2379","http://localhost:12379"
,"http://localhost:22379").build();
// 创建租约
LeaseGrantResponse leaseGrantResponse = lease.grant(3000).get();
System.out.println("lease:(" + leaseGrantResponse + ")");
Lock lock = client.getLockClient();
LockResponse lockResponse = lock.lock(key,leaseGrantResponse.getID()).get();
System.out.println("lock:(" + lockResponse + ")");
UnlockResponse unlockResponse = lock.unlock(key).get();
System.out.println("unlock:(" + unlockResponse + ")");
}
}
网友评论