zoukankan      html  css  js  c++  java
  • Zookeeper(三)Spring整合Zookeeper【基础操作篇】

    一、引言

      前两篇写了一些概念性的和在服务器上操作的信息,那么这一篇我们来学习如何用Spring来对Zookeeper做一个整合。首先需要先简单介绍一些Curator这个客户端。

    Curator

      Curator是Netflix公司开源的一套zookeeper客户端框架,解决了很多Zookeeper客户端非常底层的细节开发工作,包括基础操作、连接与重连、反复注册Watcher、分布式锁等等。

    二、干货代码搞起来~

    POM

    <!--引入Zookeeper依赖和curator客户端【有梦想的肥宅】-->
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>5.0.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.zookeeper</groupId>
                        <artifactId>zookeeper</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.5.8</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>5.1.0</version>
            </dependency>

    Zookeeper客户端工具类

      我这里把初始化zk客户端的代码抽成了一个工具类来使用,减少重复代码:

    /**
     * Zookeeper客户端工具类
     *
     * @author zhanghao
     * @date 2021/8/18
     */
    public class ZookeeperClientUtil {
    
        /**
         * 获取Curator客户端 【有梦想的肥宅】
         */
        public static CuratorFramework getCuratorFramework() {
    
            //1、配置重试策略 5000:重试间隔 5:重试次数
            ExponentialBackoffRetry policy = new ExponentialBackoffRetry(5 * 1000, 5);
    
            //2、构造Curator客户端
            CuratorFramework client = CuratorFrameworkFactory.builder().connectString("这里填写自己zk的ip地址:2181")
                    .connectionTimeoutMs(60 * 1000)
                    .sessionTimeoutMs(60 * 1000)
                    .retryPolicy(policy).build();
    
            //3、启动客户端
            client.start();
    
            //4、输出信息
            System.out.println("zookeeper启动成功,获取到客户端链接");
            return client;
        }
    }

    基础操作测试类

      当前类用于测试一些基础的zk操作,都是自己手打测试过的,直接拿走~

    /**
     * 基础操作测试类
     *
     * @author 有梦想的肥宅
     * @date 2021/8/18
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = CuratorBaseControlDemo.class)
    public class CuratorBaseControlDemo {
    
        //创建节点【有梦想的肥宅】
        @Test
        public void testCreate() throws Exception {
            CuratorFramework curatorFramework = ZookeeperClientUtil.getCuratorFramework();
            curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath("/nodeForJava", "this is value!".getBytes());
            System.out.println("=========【有梦想的肥宅】节点创建成功========");
        }
    
        //获取节点值【有梦想的肥宅】
        @Test
        public void testGetData() throws Exception {
            CuratorFramework curatorFramework = ZookeeperClientUtil.getCuratorFramework();
            byte[] bytes = curatorFramework.getData().forPath("/nodeForJava");
            System.out.println("=========【有梦想的肥宅】获取到的节点值:" + new String(bytes) + "========");
        }
    
        //更新节点值【有梦想的肥宅】
        @Test
        public void testSetData() throws Exception {
            CuratorFramework curatorFramework = ZookeeperClientUtil.getCuratorFramework();
            curatorFramework.setData().forPath("/nodeForJava", "changed!".getBytes());
            byte[] bytes = curatorFramework.getData().forPath("/nodeForJava");
            System.out.println("=========【有梦想的肥宅】更新后节点的值:" + new String(bytes) + "========");
        }
    
        //删除节点【有梦想的肥宅】
        //PS:guaranteed() 强制删除
        //PS:deletingChildrenIfNeeded() 删除一个节点,并且递归删除其所有的子节点
        @Test
        public void testDelete() throws Exception {
            CuratorFramework curatorFramework = ZookeeperClientUtil.getCuratorFramework();
            String pathWithParent = "/nodeForJava";
            curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath(pathWithParent);
            System.out.println("=========【有梦想的肥宅】已成功删除节点:" + pathWithParent + "========");
        }
    
    }
  • 相关阅读:
    【java】jfairy和java-faker假数据利器
    【Spring boot】【gradle】idea新建spring boot+gradle项目
    【mac】mac上使用brew 安装速度慢/每次使用brew 都会卡在updating homebrew不动/更换homebrew的镜像源
    【gradle】mac上安装gradle
    【mac】mac上安装JDK
    如何解决ajax跨域问题(转)
    java实现点选汉字验证码(自己修改后的)
    AES加解密
    java随机打乱集合顺序
    利用StringEscapeUtils对字符串进行各种转义与反转义(Java)
  • 原文地址:https://www.cnblogs.com/riches/p/15157142.html
Copyright © 2011-2022 走看看