- A+
简介
Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeeper客户端的开发量。
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
Zookeeper安装部署
1:安装jdk,这个就不说了,都知道怎么安装
2:到apache官网下载zookeeper
#wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
#tar zxvf zookeeper-3.4.6.tar.gz
#cd zookeeper-3.4.6
#cp conf/zoo_sample.cfg conf/zoo.cfg
#./bin/zkServer.sh start
这样zookeeper就搞定了,现在我们通过客户端练上去看看状态
#./bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 0] help ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port
我们简单测试几个常用的命令:
- ls
查看路径下所有节点
[zk: localhost:2181(CONNECTED) 2] ls /
[zk55, zookeeper]
- create
用来创建路径节点
[zk: localhost:2181(CONNECTED) 5] create /zk123 mydata Created /zk123 [zk: localhost:2181(CONNECTED) 6] ls / [zk55,zk123, zookeeper]
- get
获取节点值
[zk: localhost:2181(CONNECTED) 8] get /zk123
mydata
cZxid = 0x29
ctime = Thu Dec 28 16:24:07 CST 2017
mZxid = 0x29
mtime = Thu Dec 28 16:24:07 CST 2017
pZxid = 0x29
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
- set
设置节点值
[zk: localhost:2181(CONNECTED) 9] set /zk123 hello cZxid = 0x29 ctime = Thu Dec 28 16:24:07 CST 2017 mZxid = 0x2a mtime = Thu Dec 28 16:26:08 CST 2017 pZxid = 0x29 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 5 numChildren = 0 [zk: localhost:2181(CONNECTED) 10] get /zk123 hello cZxid = 0x29 ctime = Thu Dec 28 16:24:07 CST 2017 mZxid = 0x2a mtime = Thu Dec 28 16:26:08 CST 2017 pZxid = 0x29 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 5 numChildren = 0
- delete
删除节点
zk: localhost:2181(CONNECTED) 11] delete /zk123 [zk: localhost:2181(CONNECTED) 12] ls / [zk55, zookeeper]
curator连接Zookeeper
先说一下版本的问题吧,官网给的说明是:
While ZooKeeper 3.5.x is still considered "beta" by the ZooKeeper development team, the reality is that it is used in production by many users. However, ZooKeeper 3.4.x is also used in production. Prior to Apache Curator 4.0, both versions of ZooKeeper were supported via two versions of Apache Curator. Starting with Curator 4.0 both versions of ZooKeeper are supported via the same Curator libraries.
Curator 4.0 supports ZooKeeper 3.4.x ensembles in a soft-compatibility mode. To use this mode you must exclude ZooKeeper when adding Curator to your dependency management tool.
Maven
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>${curator-version}</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency>
这里我maven的配置如下:
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.0</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency>
官方给的样例中提供了两种连接方式如下:
public static CuratorFramework createSimple(String connectionString) { // these are reasonable arguments for the ExponentialBackoffRetry. The first // retry will wait 1 second - the second will wait up to 2 seconds - the // third will wait up to 4 seconds. ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3); // The simplest way to get a CuratorFramework instance. This will use default values. // The only required arguments are the connection string and the retry policy return CuratorFrameworkFactory.newClient(connectionString, retryPolicy); }
public static CuratorFramework createWithOptions(String connectionString, RetryPolicy retryPolicy, int connectionTimeoutMs, int sessionTimeoutMs) { // using the CuratorFrameworkFactory.builder() gives fine grained control // over creation options. See the CuratorFrameworkFactory.Builder javadoc // details return CuratorFrameworkFactory.builder() .connectString(connectionString) .retryPolicy(retryPolicy) .connectionTimeoutMs(connectionTimeoutMs) .sessionTimeoutMs(sessionTimeoutMs) // etc. etc. .build(); }
我们随便选一种对之前用命令操作的场景模拟一次,核心代码如下:
连接zk: CuratorFramework client = ClientConnect.createSimple(ZK_ADDRESS); client.start();
创建节点(create): client.create().creatingParentsIfNeeded().forPath(ZK_PATH, data1.getBytes());
查询节点(ls /): client.getChildren().forPath("/");
写入数据(set): client.setData().forPath(ZK_PATH, data2.getBytes());
获取数据(get): client.getData().forPath(ZK_PATH);
删除节点(delete): client.delete().forPath(ZK_PATH);
判断节点是否存在: client.checkExists().forPath(ZK_PATH)
对指到节点监听: PathChildrenCache watcher = new PathChildrenCache( client, ZK_PATH, true // if cache data ); watcher.getListenable().addListener((client1, event) -> { ChildData data = event.getData(); if (data == null) { System.out.println("No data in event[" + event + "]"); } else { System.out.println("Receive event: " + "type=[" + event.getType() + "]" + ", path=[" + data.getPath() + "]" + ", data=[" + new String(data.getData()) + "]" + ", stat=[" + data.getStat() + "]");}}); watcher.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); System.out.println("Register zk watcher successfully!"); Thread.sleep(Integer.MAX_VALUE); 这时你就会发现如果有人操作ZK_PATH,就会有日志打印出来
- 安卓客户端下载
- 微信扫一扫
- 微信公众号
- 微信公众号扫一扫