linux ZooKeeper叢集安裝
伺服器:hadoop-master,hadoop-slave01,hadoop-slave02
zookeeper版本:zookeeper-3.4.5.tar.gz
二:安裝
tar -zxvf zookeeper-3.4.5.tar.gz -C /usr/local
chown -R hadoop:hadoop /usr/local/zookeeper-3.4.5.tar.gz
三:引數配置
在conf檔案目錄下面
cp zoo_sample.cfg zoo.cfg
編輯zoo.cfg檔案
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/hadoop/zookeeper
clientPort=2181
server.1=hadoop-master:28888:38888
server.2=hadoop-slave01:28888:38888
server.3=hadoop-slave02:28888:38888
tickTime傳送心跳時間間隔,單位毫秒
initlimit和sysnclimit,兩者都是以ticktime的總數進行度量(上面的時間為10*2000=20s)。initLimit引數設定了允許所有跟隨者與領導者進行連線並同步的時間,如果在設定的時間內內,半數以上的跟隨者未能完成同步,領導者便會宣佈放棄領導地位,然後進行另外一次領導 者選舉。如果這種情況經常發生,透過檢視日誌中的記錄發現,則表明設定的值太小。
syscLimit引數設定了允許一個跟隨者與領導者進行同步的時間。如果在設定的時間內,一個跟隨者未能完成同步,它將會自己重啟,所有關聯到這個跟隨者的客戶端將連線到另外一個跟隨者。
datadir儲存的zk中持久化的資料,zk中存在兩種資料,一種用完即消失,一種需要持久存在,zk的日誌也儲存在這
建立zookeeper資料目錄
mkdir -p /data/hadoop/zookeeper(三臺服務上都建立)
echo "1" > /data/hadoop/zookeeper/myid (hadoop-master)
echo "2" > /data/hadoop/zookeeper/myid (hadoop-slave01)
echo "3" > /data/hadoop/zookeeper/myid (hadoop-slave02)
四:啟動
在對應的節點上啟動服務
/bin/zkServer.sh start
三個節點啟動完之後,檢視
jps
10060 QuorumPeerMain
9030 NameNode
9228 SecondaryNameNode
13755 Jps
9317 JobTracker
五:測試案例
public class ZooKeeperTest implements Watcher{
private static final int SEESSION_TIMEOUT=10000;
private static final String CONNECTION_STRING="10.0.57.142:2181";
private static final String ZK_PATH="/configTest";
private ZooKeeper zk=null;
private CountDownLatch countDownLatch=new CountDownLatch(1);
/**
* @description 建立zookeeper連線
* @param
* @return
* @throws
*/
public void createConnection(String connectString,int sessionTimeout)
{
this.releaseConnection();
try {
zk = new ZooKeeper(connectString, sessionTimeout, this);
countDownLatch.await();
} catch (InterruptedException e) {
System.out.println("連線建立失敗,發生InterruptedException");
e.printStackTrace();
}
catch (IOException e) {
System.out.println("連線建立失敗,發生IOException");
e.printStackTrace();
}
}
/**
* @description 釋放zookeeper連線
* @param
* @return
* @throws
*/
public void releaseConnection()
{
if(null != zk)
{
try {
this.zk.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* @description 建立節點
* @param
* @return
* @throws
*/
public boolean createPath(String path,String data)
{
try {
if(null == this.zk.exists(path, true))
{
System.out.println("節點建立成功,Path:"+this.zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
}
} catch (KeeperException e) {
System.out.println("節點建立失敗 ,發生KeeperException");
e.printStackTrace();
}
catch (InterruptedException e) {
System.out.println("節點建立失敗 ,發生InterruptedException");
e.printStackTrace();
}
return true;
}
/**
* @description 讀取節點資料內容
* @param
* @return
* @throws
*/
public String readData(String path)
{
try {
System.out.println("獲取資料成功,path:"+path);
return new String(this.zk.getData(path, true, null));
} catch (KeeperException e) {
System.out.println("讀取資料失敗 ,發生KeeperException");
e.printStackTrace();
return "";
}
catch (InterruptedException e) {
System.out.println("讀取資料失敗 ,發生InterruptedException");
e.printStackTrace();
return "";
}
}
/**
* @description 更新節點資料內容
* @param
* @return
* @throws
*/
public boolean wirteData(String path,String data)
{
try {
System.out.println( "更新資料成功,path:" + path + ", stat: " +
this.zk.setData( path, data.getBytes(), -1 ) );
}catch (KeeperException e) {
System.out.println("更新資料失敗 ,發生KeeperException");
e.printStackTrace();
}
catch (InterruptedException e) {
System.out.println("更新資料失敗 ,發生InterruptedException");
e.printStackTrace();
}
return true;
}
/**
* @description 刪除指定節點
* @param
* @return
* @throws
*/
public void deleteNode(String path){
try {
this.zk.delete(path, -1);
System.out.println( "刪除節點 成功,path:" + path);
}catch (KeeperException e) {
System.out.println("刪除節點 失敗 ,發生KeeperException");
e.printStackTrace();
}
catch (InterruptedException e) {
System.out.println("刪除節點 失敗 ,發生InterruptedException");
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent)
*/
@Override
public void process(WatchedEvent event) {
System.out.println("收到事件通知:"+event.toString()+"\n");
if(KeeperState.SyncConnected==event.getState())
{
countDownLatch.countDown();
}
}
public static void main(String[] args) throws Exception
{
ZooKeeperTest zkTest = new ZooKeeperTest();
zkTest.createConnection(CONNECTION_STRING, SEESSION_TIMEOUT);
// if(zkTest.createPath(ZK_PATH, "節點初始內容"))
// {
// System.out.println("資料內容:"+zkTest.readData(ZK_PATH));
// zkTest.wirteData(ZK_PATH, "節點更新內容");
// System.out.println("資料內容:"+zkTest.readData(ZK_PATH));
// zkTest.deleteNode(ZK_PATH);
// }
zkTest.createPath(ZK_PATH, "節點初始內容");
zkTest.createPath("/configTest/a", "節點初始內容");
zkTest.createPath("/configTest/b", "節點初始內容");
System.out.println(zkTest.zk.getChildren(ZK_PATH, true));
zkTest.releaseConnection();
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28624388/viewspace-1102255/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【Zookeeper】zookeeper叢集安裝
- zookeeper 叢集安裝
- ZooKeeper叢集安裝和部署
- Zookeeper介紹與叢集安裝
- hadoop+hbase+zookeeper叢集安裝方法Hadoop
- centos7下zookeeper叢集安裝部署CentOS
- zookeeper 叢集安裝(單點與分散式成功安裝)分散式
- 【redis叢集安裝】Redis
- kafka叢集安裝Kafka
- hbase叢集安裝
- StarRocks 叢集安裝
- cdh 叢集安裝
- ElasticSearch 6.6.0叢集安裝Elasticsearch
- TiDB叢集安裝TiDB
- Elasticsearch 叢集安裝部署Elasticsearch
- 記HBase叢集安裝
- Hadoop叢集安裝Hadoop
- 大資料規整-10-zookeeper單機叢集安裝和使用大資料
- Hadoop叢集安裝配置Hadoop
- kubernetes叢集安裝
- elasticsearch叢集安裝(3臺)Elasticsearch
- Storm叢集安裝與部署ORM
- WAS叢集安裝配置過程
- Kubernetes 叢集安裝
- redis cluster 4.0.9 叢集安裝搭建Redis
- WAS中介軟體垂直叢集安裝
- WAS中介軟體水平叢集安裝
- mongo副本集叢集安裝配置Go
- K8S叢集安裝K8S
- CDH5 叢集安裝教程H5
- Hadoop 叢集安裝與配置Hadoop
- hadoop叢集安裝檔案Hadoop
- Flink(四)叢集安裝(二)
- k8s 叢集安裝K8S
- Kafka叢集安裝Version2.10Kafka
- Redis概述和單機、叢集安裝Redis
- Hadoop叢集安裝詳細教程Hadoop
- ElasticSearch 2.3.3 叢集安裝+ Marvel + kibanaElasticsearch