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());
        
        // 設(shè)置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();

        // 創(chuàng)建租約
        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 + ")");

        // 續(xù)約租約一次
        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();

        // 創(chuàng)建租約
        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 + ")");
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文是閱讀了Etcd client v3官方代碼后,結(jié)合個人理解做的一些整理,有理解不正確不到位的地方還望指正~ ...
    Bravo_z閱讀 8,972評論 0 2
  • etcd的lease是一個非常有用的機(jī)制,主要作用是給特定的key附加一個過期時間。既然調(diào)用client.Put方...
    克羅地亞催眠曲閱讀 1,996評論 0 0
  • 概述 在傳統(tǒng)單體應(yīng)用單機(jī)部署的情況下,可以使用Java并發(fā)處理相關(guān)的API(如ReentrantLock或Sync...
    程序員札記閱讀 3,051評論 0 7
  • 概述 在傳統(tǒng)單體應(yīng)用單機(jī)部署的情況下,可以使用Java并發(fā)處理相關(guān)的API(如ReentrantLock或Sync...
    徐亞松_v閱讀 8,521評論 0 3
  • 最近一年一直從事webrtc的開發(fā),webrtc的服務(wù)性能測試也達(dá)到了大多大廠宣揚(yáng)的并發(fā)性能,甚至略超過聲網(wǎng)...
    耐寒閱讀 1,484評論 0 0

友情鏈接更多精彩內(nèi)容