1、配置过程,参见http://blog.csdn.net/morning99/article/details/40426133
2、测试代码如下
import java.io.IOException; import java.util.List; import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; import common.Logger; /** * zoo管理类 * @author bla * */ public class ZooManager implements Watcher { public ZooKeeper zooKeeper; private static final int SESSION_TIME_OUT = 6000; private CountDownLatch countDownLatch = new CountDownLatch(1); /** * 连接zookeeper * * @param host * @throws IOException * @throws InterruptedException */ public void connectZookeeper(String host) throws IOException, InterruptedException { zooKeeper = new ZooKeeper(host, SESSION_TIME_OUT, this); countDownLatch.await(); System.out.println("zookeeper connect ok"); } /** * 实现watcher的接口方法,当连接zookeeper成功后,zookeeper会通过此方法通知watcher * 此处为如果接受到连接成功的event,则countDown,让当前线程继续其他事情。 */ @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { System.out.println("watcher received event"); countDownLatch.countDown(); } } /** * 根据路径创建节点,并且设置节点数据 * * @param path * @param data * @return * @throws KeeperException * @throws InterruptedException */ public String createNode(String path, byte[] data) throws KeeperException, InterruptedException { return this.zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } /** * 根据路径获取所有孩子节点 * * @param path * @return * @throws KeeperException * @throws InterruptedException */ public List<String> getChildren(String path) throws KeeperException, InterruptedException { return this.zooKeeper.getChildren(path, false); } public Stat setData(String path, byte[] data, int version) throws KeeperException, InterruptedException { return this.zooKeeper.setData(path, data, version); } /** * 根据路径获取节点数据 * * @param path * @return * @throws KeeperException * @throws InterruptedException */ public byte[] getData(String path) throws KeeperException, InterruptedException { return this.zooKeeper.getData(path, false, null); } /** * 删除节点 * * @param path * @param version * @throws InterruptedException * @throws KeeperException */ public void deletNode(String path, int version) throws InterruptedException, KeeperException { this.zooKeeper.delete(path, version); } /** * 关闭zookeeper连接 * * @throws InterruptedException */ public void closeConnect() throws InterruptedException { if (null != zooKeeper) { zooKeeper.close(); } } private static Logger logger = Logger.getLogger(ZooManager.class); public static void main(String[] args) { try { ZooManager baseZookeeper = new ZooManager(); String host = "127.0.0.1:2182,127.0.0.1:2183"; // 连接zookeeper baseZookeeper.connectZookeeper(host); System.out.println("--------connect zookeeper ok-----------"); // 创建节点 /*byte [] data = {1, 2, 3, 4, 5}; String result = baseZookeeper.createNode("/test", data); System.out.println(result); System.out.println("--------create node ok-----------");*/ // 获取某路径下所有节点 List<String> children = baseZookeeper.getChildren("/"); for (String child : children) { logger.info(child); } logger.info("--------get children ok-----------"); // 获取节点数据 /* byte [] nodeData = baseZookeeper.getData("/test"); logger.info(new String(nodeData,"UTF-8")); logger.info("--------get node data ok-----------");*/ // 更新节点数据 byte[] data = "测试".getBytes("UTF-8"); baseZookeeper.createNode("/test", data);//版本号为当前的版本 System.out.println("--------set node data ok-----------"); byte[] nodeData = baseZookeeper.getData("/test"); System.out.println(new String(nodeData,"UTF-8")); System.out.println("--------get node new data ok-----------"); baseZookeeper.closeConnect(); System.out.println("--------close zookeeper ok-----------"); } catch (Exception e) { e.printStackTrace(); logger.error("...zookeeper操作出错....",e); } } }
相关推荐
3. **junit.jar**:JUnit是一个用于Java语言的单元测试框架,这里可能是为了在开发和测试过程中验证Zookeeper客户端代码的正确性。开发者可以编写JUnit测试用例来测试Zookeeper客户端的功能,确保其按照预期工作。 ...
安装完成后,你可以进一步学习Zookeeper的基本操作,如创建、删除节点,以及利用Java API和Curator框架进行更复杂的分布式操作,如服务注册与发现、分布式锁等。Zookeeper作为分布式系统中的重要组件,其稳定性和...
在"zookeeper的java-Demo"项目中,`zookeepertest`可能是一个包含这些基本操作的测试类,用于演示如何使用ZooKeeper的Java API。通过这个Demo,你可以更好地理解ZooKeeper的工作原理,并在实际的分布式应用中灵活...
在我们的示例中,我们使用 JUnit 测试框架来测试 HBase 的 Java API。首先,我们需要创建一个 HBaseConfiguration 对象,并将其连接到 HBase 集群。然后,我们可以使用 Admin 对象来创建一个名为 "ict01" 的表。 ...
通过阅读和运行测试代码,我们可以验证ZooKeeper的功能,也可以作为学习如何使用ZooKeeper API进行测试的参考。 此外,`zookeeper-client`模块包含了客户端API的实现,如`org.apache.zookeeper.ZooKeeper`类,它是...
在Java API中访问HBase是大数据处理中常见的一项任务,HBase作为一个分布式、列式存储的NoSQL数据库,常用于海量数据的实时读写。在这个Java API访问HBase的Maven项目中,我们将探讨如何配置项目,引入依赖,以及...
2. **Java API**:Zookeeper提供了丰富的Java API,允许开发者在应用程序中集成Zookeeper,实现对节点的读写操作以及设置Watcher。 **四、ZookeeperDemo的运行步骤** 1. **构建项目**:使用Maven构建Java项目,...
3. testng-6.9.4.jar:TestNG是另一个测试框架,提供了更灵活的测试配置和报告,可能用于编写Curator或Zookeeper的集成测试。 4. zookeeper-3.4.6.jar:这是Zookeeper的核心库,包含了所有与Zookeeper交互的类和...
对于搭建Dubbo示例,你需要创建一个提供者和服务消费者项目,配置ZooKeeper地址,并使用Dubbo的API声明服务接口和实现。在Windows上,你可以利用IDEA或Eclipse等Java开发工具进行快速开发和测试。 总的来说,...
在多服务器设置中,ZooKeeper集群通常由3个或更多服务器组成。这种配置提供了高可用性和数据一致性。为了确保集群的稳定性和性能,建议遵循以下指导原则: - 每个服务器都应具有足够的CPU资源、内存和磁盘空间来...
它使用Java编写,同时支持Java和C语言的API。ZooKeeper 的目标是简化分布式环境中的协调工作,减少竞态条件和死锁等问题的发生。 在第二部分中,深入探讨了ZooKeeper的数据模型和命名空间。ZooKeeper的数据模型采用...
在本课程中,重点讲解了如何使用Storm进行实时数据分析,并结合Kafka进行Java API的开发与测试。首先,我们了解到Kafka是一个分布式流处理平台,它最初由LinkedIn开发,后来成为Apache软件基金会的顶级项目。Kafka的...
- `src/java/test`目录下的代码用于测试Zookeeper的各种功能,包括单元测试和集成测试,确保源码的正确性。 9. **配置文件**: - `conf/zoo.cfg`:Zookeeper的主要配置文件,包含服务器地址、端口、数据存储路径...
对于开发人员来说,它可以加速对Zookeeper API的理解和测试,提高开发效率。此外,当面临分布式锁、队列管理等复杂场景时,ZooInspector能直观地展示这些分布式数据结构,便于理解和调试。 总的来说,Zookeeper...
3. **集成Spring与Zookeeper**:在Spring Boot项目中引入Spring Cloud Zookeeper依赖,配置Zookeeper的连接信息。创建服务提供者和服务消费者,利用Spring Cloud Zookeeper的API进行服务注册和发现。 4. **集成...
3. **配置管理**: ZooKeeper可以作为集中式的配置中心,存储所有微服务的配置信息。当配置发生变化时,Zookeeper可以通知相关服务进行更新,实现配置的动态管理和推送。 4. **负载均衡**: 在微服务架构中,通常会有...
它支持多种编程语言的客户端API,包括Java、C、Python等,方便开发者在不同的语言环境下使用Zookeeper。在Zookeeper中,每个节点(称为znode)都可以存储数据,同时具有唯一的路径标识,使得数据管理和访问变得直观...
- C API:对于非Java语言的支持,Zookeeper也提供了C语言的API。 7. **Zookeeper的版本特性**: - Zookeeper 3.7.0引入了更多的性能优化,比如更快的数据同步和更高的吞吐量。 - 错误处理和稳定性得到了增强,...
3. **连接管理**:支持创建、管理和测试ZooKeeper连接,查看ZooKeeper会话状态,便于调试。 4. **节点操作**:提供对ZooKeeper数据节点的创建、删除、更新、读取等操作,可以实时查看ZooKeeper的数据树结构。 5. *...
$ java -cp zookeeper.jar:lib/slf4j-api-1.6.1.jar:lib/slf4j-log4j12-1.6.1.jar:lib/log4j-1.2.15.jar:conf org.apache.zookeeper.server.quorum.QuorumPeerMain zoo.cfg ``` 需要注意的是,不同版本的ZooKeeper...