zookeeper
1、上传安装包到集群服务器
2、解压
3、修改配置文件
进入zookeeper的安装目录的conf目录
cp zoo_sample.cfg zoo.cfg
vi zoo.cfg
配置文件里改
# The number of milliseconds of each tick
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/root/zkdata
clientPort=2181
#autopurge.purgeInterval=1
server.1=ip号:2888:3888
server.2=ip号:2888:3888
server.3=ip号:2888:3888
对3台节点,都创建目录 mkdir /root/zkdata 存放数据
对3台节点,在/root/zkdata目录中生成myid文件,内容分别为各自的id: 1,2,3
hdp20-01上: echo 1 > /root/zkdata/myid
hdp20-02上: echo 2 > /root/zkdata/myid
hdp20-03上: echo 3 > /root/zkdata/myid
从配置好的服务器上scp安装目录到其他两个节点
scp -r zookeeper-3.4.6/ 第二台ip号:$PWD
scp -r zookeeper-3.4.6/ 第三台ip号:$PWD
启动zookeeper集群
zookeeper没有提供自动批量启动脚本,需要手动一台一台地起zookeeper进程
在每一台节点上的zookeeper目录下的bin目录下,运行命令:
/zkServer.sh start
启动后,用jps应该能看到一个进程:QuorumPeerMain
但是,光有进程不代表zk已经正常服务,需要用命令检查状态:
在每一台节点上的zookeeper目录下的bin目录下,运行命令:
./zkServer.sh status
能看到角色模式:为leader或follower,即正常了。
public class Zktest {
ZooKeeper zooKeeper=null;
@Before
public void init() throws Exception {
zooKeeper=new ZooKeeper("192.168.26.41,192.168.26.42,192.168.26.43",1000,null);
}
/**
* 创建节点
* @throws Exception
*/
@Test
public void create() throws Exception {
/**
* 参数一 key
* 参数2 value
* 参数3 访问权限 ZooDefs.Ids.OPEN_ACL_UNSAFE 完全开发权限 任何客户端都可以访问
* CREATOR_ALL_ACL 只有创建者有acl权限
* READ_ACL_UNSAFE 只能读
* 参数4 CreateMode.PERSISTENT
*/
String s = zooKeeper.create("/cat", "ela".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(s);
}
/**
* 更新节点
* @throws Exception
*/
@Test
public void setNode() throws Exception {
Stat stat = zooKeeper.setData("/dog", "狗".getBytes(), -1);
System.out.println(stat.getAversion());
}
@Test
public void get() throws Exception {
/**
* 1. 节点路径
* 2. 是否要监听
* 3. null 最新的版本
*/
Stat stat = new Stat();
stat.setVersion(1);
byte[] data = zooKeeper.getData("/dog", false, stat);
String str = new String(data,"utf-8");
System.out.println(str);
}
@Test
public void listnode() throws KeeperException, InterruptedException {
List<String> children = zooKeeper.getChildren("/a/b", null);
for (String child : children) {
System.out.println(child);
}
}
@After
public void after() throws InterruptedException {
zooKeeper.close();
}
public class WatchTest { ZooKeeper zooKeeper = null; @Before public void init() throws Exception { zooKeeper = new ZooKeeper("192.168.26.41:2181,192.168.26.42:2181,192.168.26.43:2181", 10000, new Watcher() { public void process(WatchedEvent event) {//事件 if (event.getType() == Event.EventType.NodeDataChanged) { System.out.println("mysql的数据库的地址发生了改变。。。。。。。。"); try { byte[] data = zooKeeper.getData("/mysql", true, null); String str = new String(data, "UTF-8"); System.out.println("新数据================="); System.out.println(str); } catch (Exception e) { e.printStackTrace(); } } if(event.getType() == Event.EventType.NodeChildrenChanged){ System.out.println("*********** 子节点改变"); List<String> children = null; try { children = zooKeeper.getChildren("/a/b", true); } catch (Exception e) { e.printStackTrace(); } System.out.println("================================"); for (String child : children) { System.out.println(child); } } } }); } @Test public void get() throws Exception{ byte[] data = zooKeeper.getData("/mysql", true, null); List<String> children = zooKeeper.getChildren("/a", true); String str = new String(data, "UTF-8"); System.out.println(str); System.out.println("开始监听了数据拿到了"); Thread.sleep(Long.MAX_VALUE); } }

浙公网安备 33010602011771号