Maven搭建Zookeeper环境

 

1、创建一个Maven项目:

 

 

更改路径:

 

 

设置  groupId 和 artifactId

 

 

 

Maven会加载这个项目:




创建好的目录:

 

 

 

官网Mavenzookeeper配置文件内容:

地址:

http://www.mvnrepository.com/search?q=zookeeper

 

点击zookeeper

 

 

点击3.4.6

 

 

可以看到配置文件代码了:

 

 

 

具体如下:

<dependency>

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

<artifactId>zookeeper</artifactId>

<version>3.4.6</version>

</dependency>

 

 

修改pom.xml文件:

 

具体如下:

<dependencies>

<dependency>

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

<artifactId>zookeeper</artifactId>

<version>3.4.6</version>

</dependency>

</dependencies>

 

 

 

新建一个类:

 

 

 

 

 

 

测试代码如下:

 

 

 

具体代码:

 

package zk1;
 
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
 
public class ZK1 {
private static String connectString="192.168.1.100:2181";
private static int sessionTimeout=999999;
public static void main(String[] args) throws Exception{
Watcher watcher=new Watcher(){
public void process(WatchedEvent event) {
System.out.println("监听到的事件:"+event);
}
};
final ZooKeeper zookeeper=new ZooKeeper(connectString,sessionTimeout,watcher);
System.out.println("获得连接:"+zookeeper);
final byte[] data=zookeeper.getData("/zk1", watcher, null);
System.out.println("读取的值:"+new String(data));
zookeeper.close();
}
}


 

Console运行之后的结果:

 

 

具体如下:

 

log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

获得连接:State:CONNECTING sessionid:0x0 local:null remoteserver:null lastZxid:0 xid:1 sent:0 recv:0 queuedpkts:0 pendingresp:0 queuedevents:0

监听到的事件:WatchedEvent state:SyncConnected type:None path:null

读取的值:mycomputer

 

 

用zkCli.sh 查看 zk1 节点的内容:

 

 

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐