• 让天下没有难学的技术
    多数学员都来自推荐,这就是口碑的力量

Java客户端如何进行操作

Java客户端操作
Maven工程和配置POM

<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.atguigu.mall</groupId>

<artifactId>zk0919</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>zk0919</name>

<url> http://maven.apache.org</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<dependencies>

<!–  https://mvnrepository.com/artifact/com.101tec/zkclient –>

<dependency>

<groupId>com.101tec</groupId>

<artifactId>zkclient</artifactId>

<version>0.10</version>

</dependency>

<!–  https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper –>

<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

<version>3.4.9</version>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.9</version>

<scope>test</scope>

</dependency>

</dependencies>

</project>

程序代码

package com.atguigu.zk3;

import java.io.IOException;

import org.apache.log4j.Logger;

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.Ids;

import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.data.Stat;

public class HelloZK

{

/**

* Logger for this class

*/

private static final Logger logger = Logger.getLogger(HelloZK.class);

private static final String CONNECTSTRING = “192.168.67.167:2181”;

private static final String PATH = “/atguigu”;

private static final int SESSION_TIMEOUT = 50*1000;

public ZooKeeper startZK() throws IOException

{

return new ZooKeeper(CONNECTSTRING, SESSION_TIMEOUT, new Watcher() {

@Override

public void process(WatchedEvent event)

{

}

});

}

public void stopZK(ZooKeeper zk) throws InterruptedException

{

if(zk != null)

{

zk.close();

}

}

public void createZNode(ZooKeeper zk,String path,String nodeValue) throws KeeperException, InterruptedException

{

zk.create(path,nodeValue.getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

}

public String getZNode(ZooKeeper zk,String path) throws KeeperException, InterruptedException

{

byte[] byteArray = zk.getData(path, false, new Stat());

return new String(byteArray);

}

public static void main(String[] args) throws IOException, KeeperException, InterruptedException

{

HelloZK hello = new HelloZK();

ZooKeeper zk = hello.startZK();

Stat stat = zk.exists(PATH, false);

if(stat == null)

{

hello.createZNode(zk, PATH, “zk1014”);

String result = hello.getZNode(zk, PATH);

System.out.println(“**********result: “+result);

}else{

System.out.println(“***********znode has already ok***********”);

}

hello.stopZK(zk);

}

}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注