Ip地址 ifconfig 查看 windows能ping通
主机名 /etc/hostname
Ip映射 hosts
Java –version查看java版本
解压zookeeper压缩包
配置zookeeper环境变量path /etc/prpfile
source /etc/profile
配置zoo.cfg配置文件 配置文件位置conf 修改dataDir与clientPort
bin 启动目录
zkServer.sh start
上传zookeeper包
解压:tar -zxvf zookeeper-3.4.10.tar.gz
移动zookeeper包到/opt/software目录下:mv zookeeper-3.4.10 /opt/software software(自己创建的目 录)
配置zookeeper的环境变量:vim /etc/profile
export ZOOKEEPER_PREFIX=/opt/software/zookeeper-3.4.10
export PATH=$PATH:$ZOOKEEPER_PREFIX/bin
生效profile:source /etc/profile
配置zookeeper的配置文件
进入zookeeper家目录中conf目录下,可看到一个zoo_sample.cfg文件
拷贝重命名:cp zoo_sample.cfg zoo.cfg (直接重命名也可以)
配置zoo.cfg: vim zoo.cfg
server.1=node01:2888:38888
server.2=node02:2888:38888
server.3=node03:2888:38888
在conf目录下创建一个data目录
1.然后执行 echo 1 > myid echo 2 > myid echo 3 > myid(分别在node01 node02 node03操作)
2.或者直接在一个节点中配置完分发到各节点:
scp -r zookeeper-3.4-10/ root@node02 /opt/software/
scp -r zookeeper-3.4-10/ root@node03 /opt/software/
zkServer.sh start (node01、node02、node03分别操作)
zkServer.sh status 查看启动状态
zkServer.sh stop 关闭zookeeper
一个leader,多个follower
在单机的基础上去配置伪分布式。(如:配置3个节点的伪分布式)。
在配置单机的时候已经有一个节点了。
有几个节点就需要有几个配置文件。
配置3个配置文件(数据目录,端口号不能冲突)
dataDir clientPort
server.1=hadoop:2888:3888
server.2=hadoop:2889:3889
server.3=hadoop:2890:3890
myid文件的创建,里面输入zk的id
需要制定配置文件启动 zkServer start zoo.cfg
命令行:
./zkCli.sh –server ip:port
./zkCli.sh
zookeeper配置文件中的2888和3888端口
2888端口:提供zookeeper对外通信
3888端口:当leader挂掉之后,重新选择leader的时候提供对外通信
选举机制
原子广播
1.有一个写入请求,将请求传给某一个follower
2.这个follower将请求转发给leader
3.leader将请求发给每一个follower,进行投票
4.每一个follower将自己的想法和决定返回给leader
5.原则:过半原则 少数服从多数
6.zookeeper的节点最好是奇数 节点要求<=255个节点
zab协议
1.广播模式
已经选举出来的leader,开始对外提供服务-----原子广播
2.恢复模式
还没有选举出leader(或者集群刚启动,leader挂掉了,新的还没起来)
looking-----观望
following-----跟从且有自己的想法
leading-----继承(准备被继承)
observing-----监听
监听机制
follower查看leader的状态,然后将查看到的状态信息进行更新和发送
工龄大的权利大
zxid-----逻辑时钟
当工龄一样时,股份各加一
zookeeper的文件节点-----znode
启动客户端:zkCli.sh -server 192.168.79.125:2181
操作文件的命令
ls /:列出“/”下面的其它的节点
create -e /name1 xiaozhang 创建临时节点,用quit命令退出或结束,会话节点消失
create -s /name2 xiaowang 创建顺序节点
creae -e -s /name3 xiaoli 创建临时数据节点
get /name 通过名字获取节点
rmr /name 删除指定名字的节点
set /name3 xiaoxiao 设置节点内容
znode类型
持久节点
临时节点-e
持久顺序节点-s
临时顺序节点
创建节点/name
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
public class TestZ {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
String create = client.create(“/name”, “zhangsan”.getBytes(), Ids.OPEN_ACL_UNSAFE/*开放,任何应用都可以操作zookeeper*/, CreateMode.PERSISTENT);
System.out.println(create);
}
}
创建子节点/name/sex
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
public class TestZ {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
String create = client.create("/name/sex", "man".getBytes()/*指定路径上存储的值*/, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT/*持久化节点*/);
System.out.println(create);
}
}
创建指定节点“/name/sex”的内容
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
public class TestZ {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
byte[] data = client.getData(“/name/sex”/*指定节点*/, true, null);
String db = new String(data);//将获取的数据转换为字符串
System.out.println(db);//打印字符串
}
}
删除指定节点“/name1111”
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class TestZ {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
client.delete("/name1111", -1);
}
}
设置指定节点“/namewjf”的内容未22222
import java.io.IOException;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
public class Testset {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
// TODO Auto-generated method stub
ZooKeeper zk = new ZooKeeper("172.16.245.5:2181", 1000, null);
zk.setData("/namewjf", "222222".getBytes(), zk.exists("/namewjf", true).getVersion()/*znode当前版本*/);
}
}
获取指定节点“/name”的子节点
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
public class testgetchild {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
List list = client.getChildren("/name", true);
for(String str:list){
System.out.println(str);
}
}
}
监听指定节点/name
public class TestWatcher implements Watcher{
//watcher只有在长链接的情况下才起作用,而且只能监听一次。
private static final int SESSION_TIMEOUT=1000;
private ZooKeeper zk = null;
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
byte[] bs = client.getData("/name", new TestWatcher(), null);
System.out.println(new String(bs));
Thread.sleep(Long.MAX_VALUE);
}
//监听到指定的目录有变化,会触发此方法
@Override
public void process(WatchedEvent event) {
System.out.println(event.getPath());
}
}