ZOOKEEPER集羣搭建及測試

     ①. zk是由java編寫的須要java運行環境,因此你們首先要安裝JDKjava

具體安裝步驟,再也不贅述node

     ②. 首先進入zk的conf目錄,將zoo_sample.cfg文件更名爲zoo.cfg,並修改內容apache

tickTime=2000 #ZK中的一個時間單元。ZK中全部時間都是以這個時間單元爲基礎,進行整數倍配置的。例如,session的最小超時時間是2*tickTime。
initLimit=5 #Leader容許Follower在 initLimit 時間內完成這個工做
syncLimit=2 #心跳檢測,Leader發出心跳包在syncLimit以後,尚未從F那裏收到響應,那麼就認爲這個F已經不在線了
dataDir=/usr/local/zookeeper/data #咱們須要在此目錄下建立myid用於標識zk節點,對應server.x,事務日誌也會存儲在這裏
dataLogDir=/usr/local/zookeeper/logs #事務日誌輸出目錄
clientPort=2181 #對外提供的端口號,默認2181
server.1=zk1:2888:3888 #server.x跟myid對應,兩個通訊端口第一個端口用於F和L之間的數據同步和其它通訊,第二個端口用於Leader選舉過程當中投票通訊。
server.2=zk2:2888:3888
server.3=zk3:2888:3888

      ③. 設置myid, 在zk根目錄建立前面zoo.cfg中配置的dataDir指定的文件夾,建立myid,myid中的值需與server.x保持一致session

    ④. 都設置完以後,記得檢查一下各節點端口是否打通,而且關閉防火牆app

     ⑤. 進入bin目錄,執行 sh zkServer.sh start &  命令,經過JPS檢查是否啓動,而且查看日誌:socket

  tail -fn 100 zookeeper.out,第一個節點啓動,會報以下異常:工具

此時不作理會,當全部節點啓動完成以後即恢復正常this

2017-09-14 14:50:24,993 [myid:2] - INFO  [QuorumPeer[myid=2]/0:0:0:0:0:0:0:0:2181:QuorumPeer$QuorumServer@149] - Resolved hostname: 192.168.116.127 to address: /zk2
2017-09-14 14:50:24,995 [myid:2] - WARN  [QuorumPeer[myid=2]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@400] - Cannot open channel to 3 at election address /zk2:3888
java.net.ConnectException: 拒絕鏈接
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:579)
	at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:381)
	at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:426)
	at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:843)
	at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:822)

,使用 sh zkServer.sh status檢查節點狀態,通常myid大的爲leader.net

    客戶端驗證是否成功:日誌

private final static String CONNECT_STRING = "zk1:2181,zk2:2181,zk3:2181";
	private final static int SESSION_TIMEOUT = 2000;
	private ZooKeeper zk = null;

	@Before
	public void init() throws Exception {
		zk = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, new ZKWatcher());
	}

	@Test
	public void create() throws Exception {
		String cResponse = zk.create("/app-01", "hello this is app-01 node".getBytes(),
		        Ids.OPEN_ACL_UNSAFE,
		        CreateMode.PERSISTENT);
		Thread.sleep(1000);
		System.out.println("--->create:" + cResponse);
	}

	@Test
	public void get() throws Exception {
		System.out.println(zk.getChildren("/", new ZKWatcher()));
	}

	@Test
	public void set() throws Exception {
		System.out.println(zk.setData("/app-01", "this is app-01 node.update count 4".getBytes(), -1));
	}

	@Test
	public void getData() throws Exception {
		System.out.println(new String(zk.getData("/app-01/hello", new ZKWatcher(), null)));
//		Thread.sleep(Long.MAX_VALUE);
	}

或使用工具:ZooInspector鏈接,檢查使用狀況以及服務端狀態

相關文章
相關標籤/搜索