概述
Apache 基金会管理(像是Maven、Tomcat都是)
开源分布式、提供协调服务(类似调度中心,协调服务的提供方)
工作机制
基于观察者模式设计的分布式服务管理框架(listener监听、setInterval计时、setTimeout定时)
保存服务注册信息、对外提供服务信息
Zookeeper 可以搭集群 每个成员是一个节点
Server 三个角色:领导者、随从、观察者
Zookeeper 数据模型的数据结构与 Unix 文件系统很类似,都是树结构,树上有若干节点,每个节点可以存1MB数据,同时每个节点都是通过其路径可以唯一标识的
服务包括:统一命名服务(统一命名一般Dubbo使用域名、ip)、统一配置管理(配置文件同步、实时性的更新)、统一集群管理(节点特性和watcher机制;掌握节点状态并调整,实时检测节点变化并更新【集群底部守护线程管理】)、服务器动态上下线(动态更新,不影响整体运行)、软负载均衡(...)等。
下载连接 https://archive.apache.org/dist/zookeeper/zookeeper-3.5.9/
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
zookeeper 客户端访问端口 2181,不是内部集群端口
jps 查看进程 ./zkServer.sh start/status/stop
开启服务、查看状态、停止服务 打开客户端:./zkCli.sh
Zookeeper 选举机制
1)半数机制:集群中半数以上机器存活,集群可用。所以 Zookeeper 适合安装奇数台服务器
2)Zookeeper 虽然在配置文件中 没有指定 Master 和 Slave。 但工作时,是有一个节点为 Leader,其他则为 Follower,Leader 是通过内部的选举机制临时产生的。
Follower 集群扩展 参与选举
Observer 观察者 不参与选举
3)
Zookeeper 节点类型
1)持久节点(Persistent):服务端客户端断开连接后,节点不删除 持久化目录节点、持久化顺序编号目录节点(只是 zook 给该节点名称进行顺序编号)
2)短暂节点(Ephemeral):服务端客户端断开连接后,节点自己删除 持久化目录节点、持久化顺序编号目录节点(只是 zook 给该节点名称进行顺序编号)
Zookeeper 读写机制
复制三份,没份的data清空,data路径更改
增加 myid 文件对应 id 存一个数字编号 1...
增加配置 增加如下
服务端口
server.1=ip地址"2888:3888"
server.2=ip地址"2889:3889"
server.3=ip地址"2890:3890"
客户端端口也需要更改
第一个2181、第二个2182、依次递增
clientPort=2181
./zkCli.sh
help
ls /
ls -s /
create /shuihu "songjiang"
create /shuihu/liangshan "liubei"
get /shuihu
create -e /person2 "guanyu"
create -s /person2 "zhangfei"
set /person2 "zhangfei"
get /person2 watch
ls /person2 watch
delete /person2
deleteall /person2
stat /person2
导入 jar 包
<dependencies>
<!--zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.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.12</version>
</dependency>
</dependencies>
测试
public class ZookeeperTest {
// 集群地址
private String host = "192.168.159.128:2181,192.168.159.128:2182,192.168.159.128:2183,";
// 超时时间
private int timeOut = 60000;
// 客户端属性
private ZooKeeper zkCli;
// 先客户端登录
@Before
public void before() throws IOException {
zkCli = new ZooKeeper(host, timeOut, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
// 连根节点
zkCli.getChildren("/",true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
// 创建节点的测试
@Test
public void testCreateNode() throws KeeperException, InterruptedException {
// 参数1 要创建的节点路径 参数2 节点数据 参数3 节点权限 参数4 节点的类型
String s = zkCli.create("/person2","caocao".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
System.out.println(s);
}
// 获取节点的测试
@Test
public void testGetNode() throws KeeperException, InterruptedException {
byte[] data = zkCli.getData("/person2", false, null);
String str = new String(data);
System.out.println(str);
}
// 获取某个上级节点的所有下级子节点
@Test
public void testGetChildren() throws KeeperException, InterruptedException {
List<String> children = zkCli.getChildren("/", false);
for (String child : children) {
System.out.println(child);
}
}
// 判断某个节点是否存在
@Test
public void testExists() throws KeeperException, InterruptedException {
Stat exists = zkCli.exists("/person2", false);
System.out.println(exists);
}
// 删除节点
@Test
public void testDelete() throws KeeperException, InterruptedException {
zkCli.delete("/person2",0);
}
}
Zookeeper 总结
单节点 Zookeeper 集群 Zookeeper
节点就是 存的 服务器服务地址 ip对外查询地址 位置地址 找到了去调用(服务协调)