美文网首页
在java中如何使用jetcd的0.5 版本搭建配置中心,监控变

在java中如何使用jetcd的0.5 版本搭建配置中心,监控变

作者: 伐无道 | 来源:发表于2020-04-18 15:44 被阅读0次

项目上有一些配置文件,不想放在数据库中,就搭建了一个etcd服务器配置这块东西。

前提

  1.使用etcd肯定要etcd服务器
      我是用docker搭建的,方便测试
  docker命令
    docker run -d --name etcd-server --publish 2379:2379  --publish 2380:2380    --env ALLOW_NONE_AUTHENTICATION=yes   --env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379  bitnami/etcd:3

     2.关键pom
      <dependency>
          <groupId>io.etcd</groupId>
          <artifactId>jetcd-core</artifactId>
          <version>0.5.3</version>
      </dependency>
  

实际代码


@Slf4j
public class EtcdUtils {

    private static final int maxEvents = Integer.MAX_VALUE;
    //etcl客户端链接
    private static Client client = null;

    /**
     * 根据指定的配置名称获取对应的value
     * @param key 配置项
     * @return
     * @throws Exception
     */
    public static String getEtcdValueByKey(String key) throws Exception {
        List<KeyValue> kvs = EtcdUtils.getEtclClient().getKVClient().get(ByteSequence.from(key,StandardCharsets.UTF_8)).get().getKvs();
        if(kvs.size()>0){
            return kvs.get(0).getValue().toString(StandardCharsets.UTF_8);
        }
        else {
            return null;
        }
    }
    /**
     * 新增或者修改指定的配置
     * @param key
     * @param value
     * @return
     */
    public static void putEtcdValueByKey(String key,String value){
        EtcdUtils.getEtclClient().getKVClient().put(ByteSequence.from(key,StandardCharsets.UTF_8),ByteSequence.from(value.getBytes(StandardCharsets.UTF_8)));
    }
    /**
     * 删除指定的配置
     * @param key key
     * @return
     */
    public static void deleteEtcdValueByKey(String key){
        EtcdUtils.getEtclClient().getKVClient().delete(ByteSequence.from(key,StandardCharsets.UTF_8));
    }

    /**
     * 监听指定key的变化
     * @param keyString key
     */
    public static void watchKey(String keyString){

        CountDownLatch latch = new CountDownLatch(maxEvents);
        ByteSequence key = ByteSequence.from(keyString, StandardCharsets.UTF_8);
        Watch.Listener listener = Watch.listener(response -> {
            log.info("Watching for key={}", key);

            for (WatchEvent event : response.getEvents()) {
                log.info("type={}, key={}, value={}", event.getEventType(),
                        Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(StandardCharsets.UTF_8)).orElse(""),
                        Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(StandardCharsets.UTF_8)).orElse(""));
            }

            latch.countDown();
        });

        try (Watch watch = getEtclClient().getWatchClient();
             Watch.Watcher watcher = watch.watch(key, listener)) {

            latch.await();
        } catch (Exception e) {
            log.error("Watching Error {}", e);
            System.exit(1);
        }
    }


    //链接初始化 单例模式
    private static synchronized Client getEtclClient(){
        if(null == client){
            client = Client.builder().endpoints("http://127.0.0.1:2379").build();
        }
        return client;
    }
    private static String getConfig(List<KeyValue> kvs){
        if(kvs.size()>0){
            String config = kvs.get(0).getKey().toString(StandardCharsets.UTF_8);
            String value = kvs.get(0).getValue().toString(StandardCharsets.UTF_8);
            log.info("etcd 's config 's config key is :{},value is:{}",config,value);
            return value;
        }
        else {
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(()->{
            watchKey("test");
        });
        putEtcdValueByKey("test","aaa");
        String test = getEtcdValueByKey("test");
        System.out.println(test);;
    }
}

github地址
https://github.com/jrd77/etcddemo
参考:
https://www.cnblogs.com/laoqing/p/8967549.html

相关文章

网友评论

      本文标题:在java中如何使用jetcd的0.5 版本搭建配置中心,监控变

      本文链接:https://www.haomeiwen.com/subject/fnavvhtx.html