etcd 和zookeeper 很像,都可以用来做配置管理。并且etcd可以在目前流行的Kubernetes中使用。

但是etcd 提供了v2版本合v3的版本的两种api。我们现在分别来介绍一下这两个版本api的使用。

一、Etcd V2版本API

1、java工程中使用maven引入 etcd v2的java api操作jar包

  1. <dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.21.Final</version>
    </dependency>
  1. <dependency>
    <groupId>org.mousio</groupId>
    <artifactId>etcd4j</artifactId>
    <version>2.15.0</version>
    </dependency>

    2、etcd的链接
  1. import mousio.etcd4j.EtcdClient;
    import java.io.InputStream;
    import java.net.URI;
    import java.util.Properties;


    public class EtcdUtil {
    //etcd客户端链接
    private static EtcdClient client = null;
    //链接初始化
    public static synchronized EtcdClient getClient(){
    if (null == client){
    client = new EtcdClient (URI.create(properties.getProperty("http://127.0.0.1:2379")));
    }
    return client;
    }
    }
    3、如何获取etcd的配置并且对配置进行监听
    //初始化获取etcd配置并且进行配置监听
  1. private void initEtcdConfig() {
    EtcdKeysResponse dataTree ;
    try {
    final EtcdClient etcdClient = EtcdUtil.getClient();
    //获取etcd中名称叫ETCD文件夹下的配置
    EtcdKeyGetRequest etcdKeyGetRequest = etcdClient.getDir("ETCD").consistent();
    dataTree = etcdKeyGetRequest.send().get();
    //获取etcd的版本
    System.out.println("ETCD's version:"+etcdClient.getVersion());
    getConfig("/ETCD/example.config",dataTree); //加载配置项
       //启动一个线程进行监听
    startListenerThread(etcdClient);
    } catch (Exception e) {
    System.out.println("EtcdClient init cause Exception:"+e.getMessage());
    e.printStackTrace();
    }
    }
  1. private String getConfig(String configFile,EtcdKeysResponse dataTree){
    if(null != dataTree && dataTree.getNode().getNodes().size()>0){
    for(EtcdKeysResponse.EtcdNode node:dataTree.getNode().getNodes()){
    if(node.getKey().equals(configFile)){
    return node.getValue();
    }
    }
    }
    System.out.println("Etcd configFile"+ configFile+"is not exist,Please Check");
    return null;
    }
  1. public void startListenerThread(EtcdClient etcdClient){
    new Thread(()->{
    startListener(etcdClient);
    }).start();
    }
    public void startListener(EtcdClient etcdClient){
    ResponsePromise promise =null;
    try {
    promise = etcdClient.getDir(SYSTEM_NAME).recursive().waitForChange().consistent().send();
    promise.addListener(promisea -> {
    logger.info("found ETCD's config:{}cause change",ETCD_CONFIG_FILE_NAME);
    try {
    getConfig("/ETCD/example.config", etcdClient.getDir("ETCD").consistent().send().get()); //加载配置项
    } catch (Exception e) {
    e.printStackTrace();
    logger.info("listen etcd 's config change cause exception:{}",e.getMessage());
    }
    startListener(etcdClient);
    });
    } catch (Exception e) {
    startListener(etcdClient);
    System.out.println("listen etcd 's config change cause exception:"+e.getMessage());
    e.printStackTrace();

    }
    }
    4、使用dcmp管理 etcd的配置项
  1. dcmp 是一个使用go语言开发的etcd配置界面,不足的时,这个只支持v2API,项目的地址:

https://github.com/silenceper/dcmp

 

一、Etcd V3版本API

1、在工程引入如下依赖

  1. <dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.15.Final</version>
    </dependency>
    <dependency>
    <groupId>com.coreos</groupId>
    <artifactId>jetcd-core</artifactId>
    <version>0.0.2</version>
    </dependency>
    2、v3 api操作工具类
  1. public class EtcdUtil {
    //etcl客户端链接
    private static Client client = null;
    //链接初始化
    public static synchronized Client getEtclClient(){
    if(null == client){
    client = Client.builder().endpoints(props.getProperty("http://127.0.0.1:2379")).build();
    }
    return client;
    }

    /**
    * 根据指定的配置名称获取对应的value
    * @param key 配置项
    * @return
    * @throws Exception
    */
    public static String getEtcdValueByKey(String key) throws Exception {
    List<KeyValue> kvs = EtcdUtil.getEtclClient().getKVClient().get(ByteSequence.fromString(key)).get().getKvs();
    if(kvs.size()>0){
    String value = kvs.get(0).getValue().toStringUtf8();
    return value;
    }
    else {
    return null;
    }
    }

    /**
    * 新增或者修改指定的配置
    * @param key
    * @param value
    * @return
    */
    public static void putEtcdValueByKey(String key,String value) throws Exception{
    EtcdUtil.getEtclClient().getKVClient().put(ByteSequence.fromString(key),ByteSequence.fromBytes(value.getBytes("utf-8")));

    }

    /**
    * 删除指定的配置
    * @param key
    * @return
    */
    public static void deleteEtcdValueByKey(String key){
    EtcdUtil.getEtclClient().getKVClient().delete(ByteSequence.fromString(key));

    }
    }
    //V3 api配置初始化和监听
    public void init(){
  1. try {
    //加载配置
    getConfig(EtcdUtil.getEtclClient().getKVClient().get(ByteSequence.fromString(ETCD_CONFIG_FILE_NAME)).get().getKvs());
    //启动监听线程
    new Thread(() -> {
    //对某一个配置进行监听
    Watch.Watcher watcher = EtcdUtil.getEtclClient().getWatchClient().watch(ByteSequence.fromString("etcd_key"));
    try {
    while(true) {
    watcher.listen().getEvents().stream().forEach(watchEvent -> {
    KeyValue kv = watchEvent.getKeyValue();
                //获取事件变化类型
                System.out.println(watchEvent.getEventType());
       //获取发生变化的key
    System.out.println(kv.getKey().toStringUtf8());
  1.           //获取变化后的value
    String afterChangeValue = kv.getValue().toStringUtf8();

    });
    }
    } catch (InterruptedException e) {
    e.printStackTrace();

    }
    }).start();
    } catch (Exception e) {
    e.printStackTrace();

    }

  1. }
  1. private String getConfig(List<KeyValue> kvs){
    if(kvs.size()>0){
    String config = kvs.get(0).getValue().toStringUtf8();
    logger.info("etcd 's config:{} 's configValue is :{}",ETCD_CONFIG_FILE_NAME,config);
    return config;
    }
    else {
    return null;
    }
    }

版权声明:本文为laoqing原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/laoqing/p/8967549.html