首页 > 编程知识 正文

zookeeper的java,Zookeeper是什么

时间:2023-12-27 22:26:06 阅读:323613 作者:SJDU

本文目录一览:

zookeeper 集群 部署好后怎么使用 java

很多使用Zookeeper的情景是需要我们嵌入Zookeeper作为自己的分布式应用系统的一部分来提供分布式服务,此时我们需要通过程序的方式来启动Zookeeper。此时可以通过Zookeeper API的ZooKeeperServerMain类来启动Zookeeper服务。

下面是一个集群模式下启动Zookeeper服务的例子

这里假定我们运行Zookeeper集群的三台机器名分别为fanbinx1,fanbinx2,fanbinx3

首先是zoo.cfg配置文件

[plain] view plain copy print?

tickTime=2000

dataDir=/tmp/zookeeper/data

clientPort=2181

initLimit=10

syncLimit=5

server.1=fanbinx1:2888:3888

server.2=fanbinx2:2888:3888

server.3=fanbinx3:2888:3888

启动Zookeeper集群服务的类,如下

* 这个类同时使用同一个zoo.cfg配置文件来启动Zookeeper服务。

* 在每台机器上启动Zookeeper服务的时候判断当前机器是不是定义在zoo.cfg文件里,如果是获取其中的ID号,然后生成myid文件并将ID写入其中。

* 最后启动Zookeeper服务。

[java] view plain copy print?

package my.zookeeperstudy.server;

import org.apache.commons.io.FileUtils;

import org.apache.zookeeper.server.ServerConfig;

import org.apache.zookeeper.server.ZooKeeperServerMain;

import org.apache.zookeeper.server.quorum.QuorumPeerConfig;

import java.io.File;

import java.io.InputStream;

import java.net.InetAddress;

import java.util.Properties;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class ClusteredZKServer {

public static void main(String[] args) throws Exception {

InputStream is = ClusteredZKServer.class.getResourceAsStream("/my/zookeeperstudy/server/zoo.cfg");

Properties props = new Properties();

try {

props.load(is);

} finally {

is.close();

}

for (String key : props.stringPropertyNames()) {

Pattern pKey = Pattern.compile("^server\.(\d)");

Pattern pValue = Pattern.compile("([\w|.]*):\d*:\d*");

Matcher mKey = pKey.matcher(key);

Matcher mValue = pValue.matcher(props.getProperty(key));

if (mKey.find() mValue.find()) {

String id = mKey.group(1);

String host = mValue.group(1);

String thisHostName = InetAddress.getLocalHost().getHostName();

String thisHostAddress = InetAddress.getLocalHost().getHostAddress();

if (host.equals(thisHostName) || host.equals(thisHostAddress)) {

//System.out.println(new File(props.getProperty("dataDir"), "myid").getAbsolutePath());

FileUtils.write(new File(props.getProperty("dataDir"), "myid"), id);

QuorumPeerConfig quorumConfig = new QuorumPeerConfig();

quorumConfig.parseProperties(props);

final ZooKeeperServerMain zkServer = new ZooKeeperServerMain();

final ServerConfig config = new ServerConfig();

config.readFrom(quorumConfig);

zkServer.runFromConfig(config);

}

}

}

}

}

客户端测试代码如下,这里可以修改hostname为集群中的任意一台机器

[java] view plain copy print?

package my.zookeeperstudy.server;

import org.apache.zookeeper.*;

import java.util.List;

public class Client {

public static void main(String[] args) throws Exception {

ZooKeeper zk = new ZooKeeper("fanbinx1:2181,fanbinx2:2181,fanbinx3:2181", 10000,

new Watcher() {

public void process(WatchedEvent event) {

System.out.println("event: " + event.getType());

}

});

System.out.println(zk.getState());

zk.create("/myApps", "myAppsData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

zk.create("/myApps/App1", "App1Data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

zk.create("/myApps/App2", "App2Data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

zk.create("/myApps/App3", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

zk.setData("/myApps/App3","App3Data".getBytes(), -1);

System.out.println(zk.exists("/myApps", true));

System.out.println(new String(zk.getData("/myApps", true, null)));

ListString children = zk.getChildren("/myApps", true);

for (String child : children) {

System.out.println(new String(zk.getData("/myApps/" + child, true, null)));

zk.delete("/myApps/" + child,-1);

}

zk.delete("/myApps",-1);

zk.close();

}

}

测试

* 在集群中的各个机器上分别运行ClusteredZKServer类来启动Zookeeper服务。

* 然后在任意一台机器上运行Client类来连接Zookeeper并操作数据。

zookeeper怎么用java创建临时节点

基本操作

下面给出基本的操作 ZooKeeper 的示例代码,这样你就能对 ZooKeeper 有直观的认识了。下面的清单包括了创建与 ZooKeeper 服务器的连接以及最基本的数据操作:

ZooKeeper 基本的操作示例

// 创建一个与服务器的连接

ZooKeeper zk = new ZooKeeper("localhost:" + CLIENT_PORT,

ClientBase.CONNECTION_TIMEOUT, new Watcher() {

// 监控所有被触发的事件

public void process(WatchedEvent event) {

System.out.println("已经触发了" + event.getType() + "事件!");

}

});

// 创建一个目录节点

zk.create("/testRootPath", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE,

CreateMode.PERSISTENT);

// 创建一个子目录节点

zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(),

Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

System.out.println(new String(zk.getData("/testRootPath",false,null)));

// 取出子目录节点列表

System.out.println(zk.getChildren("/testRootPath",true));

// 修改子目录节点数据

zk.setData("/testRootPath/testChildPathOne","modifyChildDataOne".getBytes(),-1);

System.out.println("目录节点状态:["+zk.exists("/testRootPath",true)+"]");

// 创建另外一个子目录节点

zk.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(),

Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

System.out.println(new String(zk.getData("/testRootPath/testChildPathTwo",true,null)));

// 删除子目录节点

zk.delete("/testRootPath/testChildPathTwo",-1);

zk.delete("/testRootPath/testChildPathOne",-1);

// 删除父目录节点

zk.delete("/testRootPath",-1);

// 关闭连接

zk.close();

输出的结果如下:

已经触发了 None 事件!

testRootData

[testChildPathOne]

目录节点状态:[5,5,,,0,1,0,0,12,1,6]

已经触发了 NodeChildrenChanged 事件!

testChildDataTwo

已经触发了 NodeDeleted 事件!

已经触发了 NodeDeleted 事件!

当对目录节点监控状态打开时,一旦目录节点的状态发生变化,Watcher 对象的 process 方法就会被调用。

安装zookeeper需要先装java吗

ZooKeeper是用Java编写的,运行在Java环境上,因此,在部署zk的机器上需要安装Java运行环境。为了正常运行zk,我们需要JRE1.6或者以上的版本。

对于集群模式下的ZooKeeper部署,3个ZooKeeper服务进程是建议的最小进程数量,而且不同的服务进程建议部署在不同的物理机器上面,以减少机器宕机带来的风险,以实现ZooKeeper集群的高可用。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。