zookeeper基本操作
来源:互联网 发布:mac 快捷方式 参数 编辑:程序博客网 时间:2024/05/16 12:19
zookeeper基本操作
一、安装zookeeper
启动命令
bin/zookeeper-server-start.sh config/zookeeper.properties
二、Maven依赖
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.9.0</version></dependency><dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-client</artifactId> <version>2.9.0</version></dependency><dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.9.0</version></dependency>
三、zookeeper基本操作
package zookeeper;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.curator.utils.ZKPaths;import org.apache.zookeeper.ZooKeeper;import java.nio.charset.Charset;/** * zookeeper 基本操作<br/> * Created by zhengyong on 16/11/24. */public class ZookeeperCURD { private static final int sessionTimeout = 15000; private static final String ZK_HOST = "127.0.0.1:2181"; private static final String ZK_PATH = "/zkPath"; private static CuratorFramework curatorClient = null; public static void main(String[] args) throws Exception { createClient(); create(ZK_PATH); setDataValue(ZK_PATH, "test data"); getDataValue(ZK_PATH); delete(ZK_PATH); } /** * 创建zookeeper访问客户端 * * @throws Exception */ private static void createClient() throws Exception { if (curatorClient == null) { synchronized (ZookeeperCURD.class) { curatorClient = CuratorFrameworkFactory.builder().connectString(ZK_HOST).sessionTimeoutMs(sessionTimeout).retryPolicy(new ExponentialBackoffRetry(1000, 10, 5000)).build(); curatorClient.start(); } } } /** * 创建zookeeper节点信息 * * @throws Exception */ private static void create(String path) throws Exception { ZooKeeper zookeeper = new ZooKeeper(ZK_HOST, sessionTimeout, null); ZKPaths.mkdirs(zookeeper, path); System.out.println(String.format("create path=%s", path)); } /** * 设置path node 值 * * @param path 路径 * @param data 值 * @throws Exception */ private static void setDataValue(String path, String data) throws Exception { curatorClient.setData().forPath(path, data.getBytes(Charset.forName("UTf-8"))); System.out.println(String.format("set path=%s, data=%s", path, data)); } /** * 获取path node 值 * * @param path 路径 * @return 值 * @throws Exception */ private static String getDataValue(String path) throws Exception { byte[] value = curatorClient.getData().forPath(path); String result = new String(value, Charset.forName("UTf-8")); System.out.println(String.format("get path=%s, data=%s", path, result)); return result; } /** * 删除节点 * * @param path 路径 */ private static void delete(String path) throws Exception { curatorClient.delete().forPath(path); System.out.println(String.format("delete path=%s", path)); }}
运行结果:
create path=/zkPathset path=/zkPath, data=test dataget path=/zkPath, data=test datadelete path=/zkPath
0 0
- ZooKeeper 基本操作
- zookeeper基本操作
- zookeeper基本操作
- zookeeper之基本操作命令
- ZooKeeper 基本的操作示例
- zookeeper(四)基本操作
- Zookeeper集群搭建与基本操作
- zkclient实现对zookeeper基本操作
- 操作zookeeper之原生API的基本操作
- zookeeper安装(单节点)及基本操作
- ZooKeeper基本理解
- ZooKeeper 基本介绍
- 【Zookeeper】基本配置
- ZooKeeper 基本API使用
- zookeeper-04-基本命令
- zookeeper入门基本介绍
- ZooKeeper 基本介绍
- zookeeper基本api
- iOS开发系列---面向对象思想
- Oracle表分区
- netty与websocket通信demo
- Swap file ".Podfile.swp" already exists!
- FLEX完整demo
- zookeeper基本操作
- 润乾报表打印报错:invalid stream header的几种情况
- eclipse+tomcat+maven+git环境安装及配置
- #425–使用SharedSizeGroup在Grid间共享列的大小(SharedSizeGroup Allows Sharing Column Sizes Across Differet Grid)
- NYOJ 57 6174问题
- Flex和Flash的区别和联系
- AngularJS 0002:表达式
- springcloud-config client配置文件为什么要用bootstrap命名
- bzoj1012