java springboot zookeeper curator 监听


            org.apache.curator
            curator-framework
            4.3.0
        
        
            org.apache.curator
            curator-client
            4.3.0
        
        
            org.apache.curator
            curator-recipes
            4.3.0
        
public class ZookeeperHelper {
    /**
     * 可以写多个地址,逗号分隔
     */
    private static final String zkServerIps = "127.0.0.1:2181";
    private static RetryPolicy retryPolicy = new ExponentialBackoffRetry(
            1000,
            3);
    /**
     * 实例化Curator客户端
     */
    private final static CuratorFramework client = CuratorFrameworkFactory.builder()
             放入zookeeper服务器ip
            .connectString(zkServerIps)
            // 设定会话时间以及重连策略
            .sessionTimeoutMs(15000).retryPolicy(retryPolicy)
            // 建立连接通道
            .build();

    public static CuratorFramework getClient(){
        return client;
    }

    public static void start(){
        // 启动Curator客户端
        client.start();
        //获取连接状态
        System.out.println("client.getState()="+client.getState());
    }

    public static void clientClose() {
        if (client != null) {
            client.close();
        }
    }
    /**
     * 注册监听
     * TreeCache: 可以将指定的路径节点作为根节点(祖先节点),对其所有的子节点操作进行监听,
     * 呈现树形目录的监听,可以设置监听深度,最大监听深度为 int 类型的最大值。
     */
    public static void zkWatch(String path) throws Exception {
        TreeCache treeCache = new TreeCache(client, path);

        treeCache.getListenable().addListener((client, event) -> {
            ChildData eventData = event.getData();
            if(eventData==null){
                return;
            }
            String path1 = eventData.getPath();
            TreeCacheEvent.Type type = event.getType();
            StaticLog.warn("type="+type);
            switch (type) {
                case NODE_ADDED:
                    StaticLog.warn("节点添加" + path1 +
                            "\t添加数据为:" + new String(eventData.getData()));
                    break;
                case NODE_UPDATED:
                    StaticLog.warn(path1 + "节点数据更新\t更新数据为:" +
                            new String(eventData.getData()) + "\t版本为:" + eventData.getStat().getVersion());
                    break;
                case NODE_REMOVED:
                    StaticLog.warn(path1 + "节点被删除");
                    break;
                default:
                    break;
            }
        });

        treeCache.start();
        //如果不执行 watch.countDown(),进程会一致阻塞在 watch.await()
//        watch.await();
    }
}
@SpringBootApplication
public class Demo8Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Demo8Application.class, args);
        String path = "/test";
        ZookeeperHelper.start();
        ZookeeperHelper.zkWatch(path);
    }

}
    /**
     * 创建临时节点,单元测试结束节点会被删除
     * @throws Exception
     */
    @Test
    public void AddEPHEMERALNodeTest() throws Exception {
        String path = "/test/3";
        curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath(path);
        byte[] bytes = curatorFramework.getData().forPath(path);
        String s = new String(bytes);
        System.out.println("s="+s);
    }

    @Test
    public void test02() throws Exception {
        String path="/test";
        //获取一个节点下的全部子节点
        List stringList = curatorFramework.getChildren().forPath(path);

        for (String s : stringList) {
            //获取子节点数据
            String clildPath=path + "/" + s;
            byte[] bytes = curatorFramework.getData().forPath(clildPath);
            String s1 = new String(bytes);
            System.out.println(s+",s1="+s1);
            //删除子节点数据
            //curatorFramework.delete().guaranteed().forPath(clildPath);
        }
    }

你可能感兴趣的:(java,开发语言,zookeeper,spring,boot)