- 启动单机版的zookeeper
- 配置Maven环境
(1) IDEA自带maven
(2) 更新Maven库镜像地址:
① 拷贝D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.5\plugins\maven\lib\maven3\conf\settings.xml [IntelliJ的安装目录]到 C:/用户/username/.m2 (如果.m2文件不存在,可以新建一个.m2文件夹);
② 修改.m2文件夹下的settings.xml,在 和之间添加:
(为了可以从国内的aliyun镜像站下载依赖,国外的依赖下载慢 )
xml
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
(3)可以修改.m2文件夹的位置:https://blog.csdn.net/qq_27093465/article/details/52957253,此处我没有修改
-
新建Maven项目,选好JDK,不要选"Create from archetype",设置完之后next,最后finish
-
打开新建的项目之后,右下加会出现,import changes即可
-
打开后的pom.xml文件
-
添加依赖,添加的依赖放在
<dependencies></dependencies>
之间,每个依赖用<dependency></dependency>
括起来
-
zookeeper版本位置发红,可以去aliyun查看一下zookeeper的版本,选择GAV搜索
-
根据搜索结果修改zookeeper的版本,然后更新,稍等片刻
- 继续完善pom.xml文件,添加org.apache.zookeeper依赖,可以从maven仓库找,其实不需要上面的org.apache.hadoop也可以,后来来我把它删了,一下是完整代码
-
-
xml
<?xml version="1.0" encoding="UTF-8"?>
<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.example.zk1</groupId>
<artifactId>zookeeper1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
</project>
①从maven仓库中查找zookeeper,选择自己的zookeeper版本,复制下面的代码
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200329223908666.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDI5ODU5MA==,size_16,color_FFFFFF,t_70)![在这里插入图片描述](https://img-blog.csdnimg.cn/20200329222851853.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDI5ODU5MA==,size_16,color_FFFFFF,t_70)
②Loj4j的依赖,日志用,也是在Mave仓库找apache Log4j Core,选择一个版本复制依赖到dependencies节点
在resources目录下新建log4j.properties
powershell
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
这部分是为了下图
③ 还有一个做测试用的[Junit](https://mvnrepository.com/artifact/junit/junit)
11. 重新install,有时有些依赖没有下载下来但并不影响使用
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200330103232585.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDI5ODU5MA==,size_16,color_FFFFFF,t_70)
13. 接下来写代码,新建package,新建java类
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200329222939838.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDI5ODU5MA==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200329222945531.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDI5ODU5MA==,size_16,color_FFFFFF,t_70)
13. 查看Centos7的ip地址,使用ifconfig命令,查看ens33的ip
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200329222956252.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDI5ODU5MA==,size_16,color_FFFFFF,t_70)
14. 写代码
```java
package demo.zookeeper;
import org.apache.zookeeper.*;
import org.apache.zookeeper.ZooDefs.Ids;
import java.io.IOException;
public class ZKBasicOperation {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
// 创建一个与服务器的连接
ZooKeeper zk = new ZooKeeper("192.168.79.128:2181", 60000, new Watcher() {
// 监控所有被触发的事件
public void process(WatchedEvent event) {
System.out.println("EVENT:" + event.getType());
}
});
// 查看根节点
System.out.println("ls / => " + zk.getChildren("/", true));
// 创建一个目录节点
if (zk.exists("/node", true) == null) {
zk.create("/node", "conan".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("create /node conan");
// 查看/node节点数据
System.out.println("get /node => " + new String(zk.getData("/node", false, null)));
// 查看根节点
System.out.println("ls / => " + zk.getChildren("/", true));
}
// 创建一个子目录节点
if (zk.exists("/node/sub1", true) == null) {
zk.create("/node/sub1", "sub1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("create /node/sub1 sub1");
// 查看node节点
System.out.println("ls /node => " + zk.getChildren("/node", true));
}
// 修改节点数据
if (zk.exists("/node", true) != null) {
zk.setData("/node", "changed".getBytes(), -1);
// 查看/node节点数据
System.out.println("get /node => " + new String(zk.getData("/node", false, null)));
}
// 删除节点
if (zk.exists("/node/sub1", true) != null) {
zk.delete("/node/sub1", -1);
zk.delete("/node", -1);
// 查看根节点
System.out.println("ls / => " + zk.getChildren("/", true));
}
// 关闭连接
zk.close();
}
}
```
运行结果(要等一会,一开始会有一些Warning)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RNQxT7Yf-1585491574683)(img/image-20200329214716360.png)]
接
zk.close();
}
}
```
运行结果(要等一会,一开始会有一些Warning)