咱們新建一個springboot的項目java
前提:保證三臺服務器上 Zookeeper 集羣服務端啓動正常,我在第二篇文章已經搭建好了三臺node
建立一個工程:zookeeperspring
添加pom文件:apache
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.3.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.3.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-client</artifactId> <version>4.3.0</version> </dependency>
而後在 src/main/resources 目錄下,新建一個文件,命名爲「log4j.properties」,在 文件中填入如下內容:springboot
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
而後建立包名、類名zkClient,zk客戶端用來鏈接服務端,客戶端代碼:服務器
public class zkClient { // 注意:逗號左右不能有空格 private String connectString = "ip:2181,ip:2182,ip:2183"; private int sessionTimeout = 2000; private ZooKeeper zkClient; //@Before @Test public void init() throws IOException { zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { } }); } }
這個時候執行一下單元測試,能夠發現鏈接成功:session
而後咱們能夠使用客戶端建立節點app
@Test public void create() throws KeeperException, InterruptedException { String nodeCreated = zkClient.create("/person", "楊戩".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); }
但是執行的時候會報錯:org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /ide
其實這個錯是超時時間問題,我將它設置的特別大才能夠了,因此只要調大超時時間就好了單元測試
也能夠監聽獲取節點信息等,代碼以下:
package com.atguigu.zk; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.List; public class zkClient { // 注意:逗號左右不能有空格 private String connectString = "116.62.71.39:2181,116.62.71.39:2182,116.62.71.39:2183"; private int sessionTimeout = 200000000; private ZooKeeper zkClient; @Before public void init() throws IOException { zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { System.out.println("-------------------------------"); List<String> children = null; try { children = zkClient.getChildren("/", true); for (String child : children) { System.out.println(child); } System.out.println("-------------------------------"); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }); } @Test public void create() throws KeeperException, InterruptedException { String nodeCreated = zkClient.create("/person", "楊戩".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } @Test public void getChildren() throws KeeperException, InterruptedException { List<String> children = zkClient.getChildren("/", true); //true就表示使用上面的監聽器 for (String child : children) { System.out.println(child); } // 延時 Thread.sleep(Long.MAX_VALUE); } @Test public void exist() throws KeeperException, InterruptedException { Stat stat = zkClient.exists("/person", false); System.out.println(stat==null? "not exist " : "exist"); } }