Zookeeper實踐
一虐秋、安裝搭建:
在h1窍帝、h2努潘、h3三臺機(jī)器上安裝搭建Zookeeper
在h1機(jī)器上執(zhí)行以下操作:
1.下載解壓zookeeper壓縮包
2.cd /home/vagrant/zookeeper-3.4.10
mkdir data
mkdir logs
cd data
vi myid
1
3.cd /home/vagrant/zookeeper-3.4.10/conf
cp zoo_sample.cfg zoo.cfg
vi zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/vagrant/zookeeper-3.4.10/data
dataLogDir=/home/vagrant/zookeeper-3.4.10/logs
clientPort=2181
server.1=h1:2888:3888
server.2=h2:2888:3888
server.3=h3:2888:3888
4.
scp -r zookeeper-3.4.10 root@h2:/home/vagrant
scp -r zookeeper-3.4.10 root@h3:/home/vagrant
vi /etc/profile
export ZOOKEEPER_HOME=/home/vagrant/zookeeper-3.4.10
export PATH=$JAVA_HOME/bin:$HIVE_HOME/bin:$SCALA_HOME/bin:$SPARK_HOME/bin:$HBASE_HOME/bin:$ZOOKEEPER_HOME/bin:$PATH
source /etc/profile
將h2上的zookeeper的data目錄下的myid改為2,設(shè)置zookeeper環(huán)境變量
將h3上的zookeeper的data目錄下的myid改為3坤学,設(shè)置zookeeper環(huán)境變量
二疯坤、啟動操作zookeeper
在三臺機(jī)器上分別啟動zookeeper:zkServer.sh start
分別查看狀態(tài):zkServer.sh status
連接客戶端:zkCli.sh -server h1:2181
image
1.查看當(dāng)前zookeeper所包含的內(nèi)容:ls /
2.創(chuàng)建一個新的Znode節(jié)點"syj",以及相關(guān)字符:create /syj 123456
3.查看:ls /
4.獲取某節(jié)點對應(yīng)的字符:get /syj
5.設(shè)置節(jié)點對應(yīng)的字符:set /syj 7890
6.get /syj
7.刪除節(jié)點:delete /syj
8.查看:ls /
image
分別關(guān)閉:zkServer.sh stop
三、java API操作zookeeper
package testZK;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.ZooDefs.Ids;
public class ZkTest {
public ZooKeeper zk = null;
public static void main(String[] args) {
ZkTest zkTest = new ZkTest();
zkTest.createConnection("10.0.34.112:2181", 10000);
// zkTest.createNode("/syj/cy", "1111");
// zkTest.deleteNode("/syj/cy");
// zkTest.setDate("/syj", "11");
// String data = zkTest.getData("/syj");
// System.out.println(data);
// zkTest.getChildren("/");
zkTest.isExist("/syj");
zkTest.closeConnection();
}
// 建立連接
public void createConnection(String connectString,int timeout) {
try {
zk = new ZooKeeper(connectString, timeout, null);
} catch (IOException e) {
e.printStackTrace();
}
}
// 關(guān)閉連接
public void closeConnection() {
try {
this.zk.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 創(chuàng)建節(jié)點
public void createNode(String path,String data) {
try {
this.zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 刪除節(jié)點
public void deleteNode(String path) {
try {
this.zk.delete(path, -1);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 設(shè)置節(jié)點對應(yīng)數(shù)據(jù)
public void setDate(String path, String data) {
try {
this.zk.setData(path, data.getBytes(), -1);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 獲取某節(jié)點對應(yīng)數(shù)據(jù)
public String getData(String path) {
String ret = null;
try {
ret = new String(this.zk.getData(path, false, null));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return ret;
}
// 查詢某節(jié)點的子節(jié)點
public void getChildren(String path) {
try {
List<String> zkChildren = this.zk.getChildren(path, false);
for (int i = 0; i < zkChildren.size(); i++) {
System.out.println(zkChildren.get(i));
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 判斷某節(jié)點是否存在
public void isExist(String path) {
try {
Stat x = this.zk.exists(path, false);
if(x != null) {
System.out.println("存在此節(jié)點");
// System.out.println(x);
}else {
System.out.println("不存在此節(jié)點");
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}