Java操作zookeeper集群 zookeeper
程序员文章站
2022-03-22 17:39:04
...
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion>4.0.0</modelVersion> <groupId>com.kovansys.test</groupId> <version>1.1.0</version> <artifactId>zookeeper_test</artifactId> <packaging>jar</packaging> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <spring.version>5.1.6.RELEASE</spring.version> <logback.version>1.2.3</logback.version> <slf4j.version>1.7.26</slf4j.version> <junit.version>5.4.1</junit.version> <java.encoding>UTF-8</java.encoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <zookeeper.version>3.4.14</zookeeper.version> </properties> <build> <directory>target</directory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> </dependencies> </project>
测试类
package zookeeper_test; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; import org.junit.jupiter.api.Test; public class ZookeeperTest { @Test public void testZookeeper() { int SESSION_TIMEOUT = 30 * 1000; Watcher wh = new Watcher() { public void process(WatchedEvent event) { System.out.println("WatchedEvent >>> " + event.toString()); } }; ZooKeeper zk = null; try { zk = new ZooKeeper("192.168.0.180:2181,192.168.0.180:2182,192.168.0.180:2183", SESSION_TIMEOUT, wh); // create node zk.create("/test", "My Data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // get node data System.out.println("data of /test is:" + String.valueOf(new String(zk.getData("/test", false, null)))); // add watcher zk.exists("/test", wh); // update node zk.setData("/test", "Data updated".getBytes(), -1); // get node data System.out.println("data of /test is:" + String.valueOf(new String(zk.getData("/test", false, null)))); // add watcher zk.exists("/test", wh); // delete zk.delete("/test", -1); // if node exist System.out.println(" node /test exists: [" + (zk.exists("/test", false) != null) + "]"); } catch (Exception e) { e.printStackTrace(); } finally { if (zk != null) { try { zk.close(); } catch (Exception e) { e.printStackTrace(); } } } } }
推荐阅读
-
ZooKeeper 03 - ZooKeeper集群的脑裂问题 (Split Brain问题)
-
Linux下ZooKeeper分布式集群安装教程
-
zookeeper的shell操作
-
zookeeper-操作与应用场景-《每日五分钟搞定大数据》
-
死磕 java同步系列之zookeeper分布式锁
-
JAVAEE——宜立方商城08:Zookeeper+SolrCloud集群搭建、搜索功能切换到集群版、Activemq消息队列搭建与使用
-
Zookeeper的安装与集群搭建
-
第8章 ZooKeeper操作
-
2020 java ZooKeeper 面试题及答案(最全版本持续更新)
-
java端连接zookeeper出现unknowHostException错误