hadoop 离线 day03 zookeeper的安装和javaAPI的使用
一、zookeeper 环境搭建
1、下载安装包
推荐到到中科大的镜像网站下载,速度会比较快:http://mirrors.hust.edu.cn/apache/zookeeper/
2、修改配置文件
下载后复制到集群的node01目录下,解压到指定目录
tar -zxvf apache-zookeeper-3.5.8.tar.gz /export/servers/zookeeper-3.5.8/
进入解压后的目录
cd /export/servers/zookeeper-3.5.8/
备份conf目录下的 zoo_sample.cfg为 zoo.cfg
mv zoo_sample.cfg zoo.cfg
新建文件夹zkdatas
mkdir zkdatas
修改刚才复制的 zoo.cfg
vi zoo.cfg
- 修改dataDir路径为我们刚才新建的文件夹的路径
- 放开被注销的autopurge.snapRetainCount
- 放开被注销的autopurge.purgeInterval=1
保存退出
3.添加myid的值
touch myid
echo 1 > myid
4.将配置好的zookeeper复制到其他机器
scp -r zookeeper-3.5.8/ node02:/export/servers/
scp -r zookeeper-3.5.8/ node03:/export/servers/
5.启动zookeeper
三台机器均执行以下命令
bin/zkServer.sh start
启动后用下面命令查看状态
bin/zkServer.sh status
至此三台集群的zookeeper已经安装完成
二. zookeeper的初步使用
1、zookeeper的shell脚本
- 登录客户端
zkCli.sh -client
- 创建永久节点
create /abc helloword #创建节点/abc,节点值为helloword
create /abc/aaa word # abc节点创建子节点aaa,并赋值
- 创建顺序节点
create -s /ttt helloword
- 创建临时节点,断开连接后会消失,临时节点不能有子节点
create -e /temp helloword
- 创建临时顺序节点
create -s -e /temp2 helloword
- 获取节点的值
get /abc
- 修改节点的值
set /abc word
- 删除节点
delete /abc # 只能删除没有子节点的节点
rmr /abc #全部删除
zookeeper的数据模型:既具有文件夹的特性,也具有文件的特性
2.zookeeper的watch机制
一次性触发:触发后监听器死亡,
事件封装:所有的节点的变化都封装成了对应的事件的变化,节点的每一个操作都对应的封装了一个事件,监听器专门获取这个事件即可
event异步发送:监听器与节点之间的操作是异步的
先注册再触发:先注册监听器,才能触发
node01上注册一个监听器,监听/abc 节点
ls /abc watch
node02上删除/abc的子节点
delete /abc/abc1
我们可以看到node01上已经打印出了修改信息
3. zookeeper的java API
mvnrepository.com原始的API
curator framework
jar包下载不下的三种解决办法
刷新工程,重新下载
删除maven仓库里面的.lastupdated的文件
换maven的版本。
package com.kkt.zk.demo1;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.junit.jupiter.api.Test;
public class ZkOperator {
@Test
public void createNode() throws Exception {
System.out.println("Hello world");
ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(3000,3);
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("node01:2181,node02:2181,node03:2181",
exponentialBackoffRetry);
// 开启服务端
curatorFramework.start();
// 创建临时节点
curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/bigdata/hello/abc","hello".getBytes());
// 关闭服务端
curatorFramework.close();
}
@Test
public void createTempNode() throws Exception {
System.out.println("Hello world");
ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(3000,3);
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("node01:2181,node02:2181,node03:2181",
exponentialBackoffRetry);
// 开启服务端
curatorFramework.start();
// 创建临时节点
curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/tempNode","hellotemp".getBytes());
Thread.sleep(8000);
// 关闭服务端
curatorFramework.close();
}
@Test
public void UpdateNode() throws Exception {
ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(3000, 3);
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("node01:2181,node02:2181,node03:2181",
exponentialBackoffRetry);
// 开启服务端
curatorFramework.start();
//
curatorFramework.setData().forPath("/abc","xxx".getBytes());
// 关闭服务端
curatorFramework.close();
}
@Test
public void GetNode() throws Exception {
ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(3000, 3);
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("node01:2181,node02:2181,node03:2181",
exponentialBackoffRetry);
// 开启服务端
curatorFramework.start();
byte[] bytes = curatorFramework.getData().forPath("/abc");
String s = new String(bytes);
System.out.println(s);
// 关闭服务端
curatorFramework.close();
}
@Test
public void watchNose() throws Exception {
ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(3000, 3);
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("node01:2181,node02:2181,node03:2181",
exponentialBackoffRetry);
// 开启服务端
curatorFramework.start();
TreeCache treeCache = new TreeCache(curatorFramework, "/abc");
treeCache.getListenable().addListener(new TreeCacheListener() {
public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
ChildData data = treeCacheEvent.getData();
if(null!=data){
TreeCacheEvent.Type type = treeCacheEvent.getType();
switch (type){
case NODE_ADDED:
System.out.println("添加");
break;
case INITIALIZED:
System.out.println("添加");
break;
case NODE_REMOVED:
System.out.println("删除");
break;
case NODE_UPDATED:
System.out.println("更新");
break;
default:
System.out.println("something");
}
}
}
});
treeCache.start();
Thread.sleep(8000000);
// 关闭服务端
curatorFramework.close();
}
}
二、网络编程
网络通信三要素
网络模型
Socket机制
IO通信模型
阻塞
非阻塞