国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

ZooKeeper(3.4.5)

2019-11-14 23:06:55
字體:
來源:轉載
供稿:網(wǎng)友
ZooKeeper(3.4.5) - 開源客戶端 Curator(2.7.0) 的簡單示例一、創(chuàng)建會話1. 創(chuàng)建會話
package com.huey.dream.demo;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;/** * 使用Curator創(chuàng)建會話 * @author  huey * @version 1.0  * @created 2015-3-1 */public class CarutorDemo {    public static void main(String[] args) throws Exception {        CuratorFramework client = CuratorFrameworkFactory.newClient(                "192.168.1.109:2181",                    // 服務器列表                5000,                                    // 會話超時時間,單位毫秒                3000,                                    // 連接創(chuàng)建超時時間,單位毫秒                new ExponentialBackoffRetry(1000, 3)     // 重試策略        );        client.start();                client.close();    }    }
2.使用鏈式風格的API接口創(chuàng)建會話
package com.huey.dream.demo;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;/** * 使用鏈式風格的API接口創(chuàng)建會話 * @author  huey * @version 1.0  * @created 2015-3-1 */public class CarutorDemo {    public static void main(String[] args) throws Exception {        CuratorFramework client = CuratorFrameworkFactory.builder()                .connectString("192.168.1.109:2181")                .sessionTimeoutMs(5000)                .connectionTimeoutMs(3000)                .retryPolicy(new ExponentialBackoffRetry(1000, 3))                .build();        client.start();                client.close();    }    }

二、創(chuàng)建節(jié)點
package com.huey.dream.demo;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;/** * 使用Curator創(chuàng)建節(jié)點 * @author  huey * @version 1.0  * @created 2015-3-1 */public class CarutorDemo {    public static void main(String[] args) throws Exception {        CuratorFramework client = CuratorFrameworkFactory.builder()            .connectString("192.168.1.109:2181")            .sessionTimeoutMs(5000)            .connectionTimeoutMs(3000)            .retryPolicy(new ExponentialBackoffRetry(1000, 3))            .build();        client.start();                client.create()            .creatingParentsIfNeeded()            .withMode(CreateMode.PERSISTENT)            .forPath("/zk-huey/cnode", "hello".getBytes());                client.close();    }    }

三、刪除節(jié)點
package com.huey.dream.demo;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;/** * 使用Curator刪除節(jié)點 * @author  huey * @version 1.0  * @created 2015-3-1 */public class CarutorDemo {    public static void main(String[] args) throws Exception {        CuratorFramework client = CuratorFrameworkFactory.builder()            .connectString("192.168.1.109:2181")            .sessionTimeoutMs(5000)            .connectionTimeoutMs(3000)            .retryPolicy(new ExponentialBackoffRetry(1000, 3))            .build();        client.start();                client.create()            .creatingParentsIfNeeded()            .withMode(CreateMode.PERSISTENT)            .forPath("/zk-huey/cnode", "hello".getBytes());                client.delete()            .guaranteed()            .deletingChildrenIfNeeded()            .withVersion(-1)            .forPath("/zk-huey");                client.close();    }    }

四、讀取節(jié)點數(shù)據(jù)
package com.huey.dream.demo;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.data.Stat;/** * 使用Curator讀取節(jié)點數(shù)據(jù) * @author  huey * @version 1.0  * @created 2015-3-1 */public class CarutorDemo {    public static void main(String[] args) throws Exception {        CuratorFramework client = CuratorFrameworkFactory.builder()            .connectString("192.168.1.109:2181")            .sessionTimeoutMs(5000)            .connectionTimeoutMs(3000)            .retryPolicy(new ExponentialBackoffRetry(1000, 3))            .build();        client.start();                client.create()            .creatingParentsIfNeeded()            .withMode(CreateMode.PERSISTENT)            .forPath("/zk-huey/cnode", "hello".getBytes());                Stat stat = new Stat();        byte[] nodeData = client.getData()            .storingStatIn(stat)            .forPath("/zk-huey/cnode");        System.out.

五、更新節(jié)點數(shù)據(jù)
package com.huey.dream.demo;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.data.Stat;/** * 使用Curator更新節(jié)點數(shù)據(jù) * @author  huey * @version 1.0  * @created 2015-3-1 */public class CarutorDemo {    public static void main(String[] args) throws Exception {        CuratorFramework client = CuratorFrameworkFactory.builder()            .connectString("192.168.1.109:2181")            .sessionTimeoutMs(5000)            .connectionTimeoutMs(3000)            .retryPolicy(new ExponentialBackoffRetry(1000, 3))            .build();        client.start();                client.create()            .creatingParentsIfNeeded()            .withMode(CreateMode.PERSISTENT)            .forPath("/zk-huey/cnode", "hello".getBytes());                client.setData()            .withVersion(-1)            .forPath("/zk-huey/cnode", "world".getBytes());                client.close();    }    }

六、 獲取子節(jié)點列表
package com.huey.dream.demo;import java.util.List;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;/** * 使用Curator * @author  huey * @version 1.0  * @created 2015-3-1 */public class CarutorDemo {    public static void main(String[] args) throws Exception {        CuratorFramework client = CuratorFrameworkFactory.builder()            .connectString("192.168.1.109:2181")            .sessionTimeoutMs(5000)            .connectionTimeoutMs(3000)            .retryPolicy(new ExponentialBackoffRetry(1000, 3))            .build();        client.start();                client.create()            .creatingParentsIfNeeded()            .withMode(CreateMode.PERSISTENT)            .forPath("/zk-huey/cnode", "hello".getBytes());                List<String> children = client.getChildren().forPath("/zk-huey");        System.out.println("Children: " + children);                client.close();    }    }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 齐河县| 孟村| 上林县| 安丘市| 莒南县| 滁州市| 开原市| 额尔古纳市| 屏山县| 修水县| 德钦县| 泌阳县| 浦县| 吉木乃县| 荆门市| 监利县| 普兰店市| 运城市| 靖江市| 大冶市| 临漳县| 双柏县| 秭归县| 鸡泽县| 凤阳县| 凤山县| 大姚县| 水城县| 介休市| 荥阳市| 麦盖提县| 广东省| 泽普县| 称多县| 灵武市| 昌吉市| 大方县| 光泽县| 上饶县| 凭祥市| 五华县|