Etcd3 JavaAPI

最近找了很多地方也沒有一個比較全的Etcd3的java api 的文檔,於是自己寫了一個封裝類,也比較好理解一點,有需要的可以用一下:

1.添加pom依賴:

<dependency>
    <groupId>com.coreos</groupId>
    <artifactId>jetcd-core</artifactId>
    <version>0.0.2</version>
</dependency>

2.工具類,有不明白的地方可以自行去官網看api(鏈接地址:https://github.com/etcd-io/jetcd):

package jetcd;

import com.coreos.jetcd.Client;
import com.coreos.jetcd.Watch;
import com.coreos.jetcd.data.ByteSequence;
import com.coreos.jetcd.data.KeyValue;
import com.coreos.jetcd.kv.PutResponse;
import com.coreos.jetcd.lease.LeaseGrantResponse;
import com.coreos.jetcd.options.DeleteOption;
import com.coreos.jetcd.options.GetOption;
import com.coreos.jetcd.options.PutOption;
import com.coreos.jetcd.options.WatchOption;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * Created by tiancan on 2018/9/12.
 */
public class Etcd4j {
    private static Client client = null;

    /**
     * init EtcdClient 初始化Etcd客戶端
     *
     * @return EtcdClient instance
     */
    public static Client getClient() {
        return client;
    }

    /**
     * init EtcdClient 初始化Etcd客戶端
     */
    public static synchronized void ClientInit() {
        if (null == client) {
            //read current ip;
            //String machineIp = getMachineIp();
            //create client;
            //client = Client.builder().endpoints("http://" + machineIp + port).build();
            client = Client.builder().endpoints("http://" + "127.0.0.1:2379").build();
        }
    }

    /**
     * get single etcdKey from etcd; 從Etcd獲取單個key
     *
     * @param key etcdKey
     * @return    etcdKey and value 's instance
     */
    public static KeyValue getEtcdKey(String key) {
        KeyValue keyValue = null;
        try {
            keyValue = client.getKVClient().get(ByteSequence.fromString(key)).get().getKvs().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return keyValue;
    }

    /**
     * get all etcdKey with this prefix 從Etcd獲取滿足前綴的所有key
     *
     * @param prefix etcdKey's prefix
     * @return       all etcdKey with this prefix
     */
    public static List<KeyValue> getEtcdKeyWithPrefix(String prefix) {
        List<KeyValue> keyValues = new ArrayList<>();
        GetOption getOption = GetOption.newBuilder().withPrefix(ByteSequence.fromString(prefix)).build();
        try {
            keyValues = client.getKVClient().get(ByteSequence.fromString(prefix), getOption).get().getKvs();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return keyValues;
    }

    /**
     * put single etcdKey 將單個key放入Etcd中
     *
     * @param key   single etcdKey
     * @param value etcdKey's value
     */
    public static void putEtcdKey(String key, String value) {
        client.getKVClient().put(ByteSequence.fromString(key), ByteSequence.fromString(value));
    }

    /**
     * put single etcdKey with a expire time (by etcd lease) 將一個有過期時間的key放入Etcd,通過lease機制
     *
     * @param key        single etcdKey
     * @param value      etcdKey's value
     * @param expireTime expire time (s) 過期時間,單位秒
     * @return           lease id 租約id
     */
    public static long putEtcdKeyWithExpireTime(String key, String value, long expireTime) {
        CompletableFuture<LeaseGrantResponse> leaseGrantResponse = client.getLeaseClient().grant(expireTime);
        PutOption putOption;
        try {
            putOption = PutOption.newBuilder().withLeaseId(leaseGrantResponse.get().getID()).build();
            client.getKVClient().put(ByteSequence.fromString(key), ByteSequence.fromString(value), putOption);
            return leaseGrantResponse.get().getID();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /**
     * put single etcdKey with a lease id 將一個key綁定指定的租約放入到Etcd。
     * @param key     single etcdKey
     * @param value   etcdKey's value
     * @param leaseId lease id 租約id
     * @return revision id if exception return 0L
     */
    public static long putEtcdKeyWithLeaseId(String key, String value, long leaseId) throws Exception{
        PutOption putOption = PutOption.newBuilder().withLeaseId(leaseId).build();
        CompletableFuture<PutResponse> putResponse = client.getKVClient().put(ByteSequence.fromString(key), ByteSequence.fromString(value), putOption);
        try {
            return putResponse.get().getHeader().getRevision();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /**
     * keep alive for a single lease
     * @param leaseId lease id 租約Id
     */
    public static void keepAliveEtcdSingleLease(long leaseId) {
        EtcdUtil.getEtclClient().getLeaseClient().keepAlive(leaseId);
    }

    /**
     * delete single etcdKey 從Etcd中刪除單個key
     *
     * @param key etcdKey
     */
    public static void deleteEtcdKey(String key) {
        client.getKVClient().delete(ByteSequence.fromString(key));
    }

    /**
     * delete all key with prefix 從Etcd中刪除所有滿足前綴匹配的key
     *
     * @param prefix etcdKey's prefix
     */
    public static void deleteEtcdKeyWithPrefix(String prefix) {
        DeleteOption deleteOption = DeleteOption.newBuilder().withPrefix(ByteSequence.fromString(prefix)).build();
        client.getKVClient().delete(ByteSequence.fromString(prefix), deleteOption);
    }

    /**
     * get single etcdKey's custom watcher 得到一個單個key的自定義觀察者
     *
     * @param key etcdKey
     * @return    single etcdKey's custom watcher
     */
    public static Watch.Watcher getCustomWatcherForSingleKey(String key) {
        return EtcdUtil.getEtclClient().getWatchClient().watch(ByteSequence.fromString(key));
    }

    /**
     * get a watcher who watch all etcdKeys with prefix 得到一個滿足所有前綴匹配的key集合的自定義觀察者
     *
     * @param prefix etcdKey's prefix
     * @return       a watcher who watch all etcdKeys with prefix
     */
    public static Watch.Watcher getCustomWatcherForPrefix(String prefix) {
        WatchOption watchOption = WatchOption.newBuilder().withPrefix(ByteSequence.fromString(prefix)).build();
        return EtcdUtil.getEtclClient().getWatchClient().watch(ByteSequence.fromString(prefix), watchOption);
    }


}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章