zookeeper-API相关操作-修改节点

工程具体结构见上文

https://www.cnblogs.com/aoligei/p/15010287.html

  //======================================set============================

    /**
     * 修改数据
     * 1.修改数据 client.getData().forPath("/test1");
     * 2.根据版本修改(多线程环境下 ABA 问题) client.setData().withVersion(version).forPath("/test2", "A".getBytes());
     * version 是通过查询出来的,目的就是不受其他客户端或者线程的干扰
     * @throws Exception
     */
    @Test
    public void testSet1() throws Exception {
        client.setData().forPath("/test1", "set1".getBytes());
        byte[] bytes = client.getData().forPath("/test1");
        System.out.println(new String(bytes));
    }

    @Test
    //ABA问题
    public void testSetForversion() throws Exception {
        Stat status = new Stat();
        client.getData().storingStatIn(status).forPath("/test2");
        int version = status.getVersion();
        System.out.println(version);
        client.setData().withVersion(version).forPath("/test2", "A".getBytes());

    }

1、修改数据

2、根据版本修改数据

 

原文地址:https://www.cnblogs.com/aoligei/p/15010889.html