ETCD Client

  • 引入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 + ")");
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文是阅读了Etcd client v3官方代码后,结合个人理解做的一些整理,有理解不正确不到位的地方还望指正~ ...
    Bravo_z阅读 12,843评论 0 2
  • etcd的lease是一个非常有用的机制,主要作用是给特定的key附加一个过期时间。既然调用client.Put方...
    克罗地亚催眠曲阅读 5,940评论 0 0
  • 概述 在传统单体应用单机部署的情况下,可以使用Java并发处理相关的API(如ReentrantLock或Sync...
    程序员札记阅读 8,046评论 0 7
  • 概述 在传统单体应用单机部署的情况下,可以使用Java并发处理相关的API(如ReentrantLock或Sync...
    徐亚松_v阅读 12,620评论 0 3
  • 最近一年一直从事webrtc的开发,webrtc的服务性能测试也达到了大多大厂宣扬的并发性能,甚至略超过声网...
    耐寒阅读 5,261评论 0 0

友情链接更多精彩内容