linux 搭建zookeeper叢集__linux

來源:互聯網
上載者:User
1.準備3台虛擬機器,關閉各自防火牆

系統可採用centos或者red hat

本樣本採用centos7


2.下載zookeeper

wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz


3.解壓縮

tar -zxvf zookeeper-3.4.8.tar.gz


4.zookeeper-3.4.8/conf 下面新增zoo.cfg

三台機器進行相同操作!

cd zookeeper-3.4.8/conf

vi zoo.cfg

內容如下:

tickTime=2000
dataDir=/usr/soft/data-zookeeper
clientPort=2181
initLimit=5
syncLimit=2
server.1=192.168.100.15:2888:3888
server.2=192.168.100.16:2888:3888
server.3=192.168.100.17:2888:3888


5.建立data目錄/usr/soft/data-zookeeper

cd /usr/soft/data-zookeeper  新增myid檔案

3台虛擬機器分別定義myid檔案內容,192.168.100.15 --> 1;192.168.100.16 --> 2;192.168.100.17 --> 3;


6.三台機器分別啟動

./zkServer.sh start


7.檢查狀態

./zkServer.sh status

結果:



8.測試

Mode:leader有讀寫權限,Mode: follower 只有讀取許可權

測試指令碼java

8.1 添加maven依賴

<dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.8</version> </dependency>

8.2

public class ZooKeeperTest {    private static final String HOST2 = "192.168.100.15:2181";    private static final String HOST1 = "192.168.100.16:2181";    private static final Integer TIMEOUT = 2000;    public static void main(String[] args) {        try {            ZooKeeper zooKeeper1 = new ZooKeeper(HOST1, TIMEOUT, null);            ZooKeeper zooKeeper2 = new ZooKeeper(HOST2, TIMEOUT, null);            System.out.println("16 建立節點...");            if (zooKeeper2.exists("/tests", false) == null) {                zooKeeper2.create("/tests", "zNode1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);            }            System.out.println("=========查看節點是否建立成功========");            System.out.println(new String(zooKeeper1.getData("/tests", false, null)));            System.out.println("========修改節點的資料========");            System.out.println("16 修改節點...");            String data = "zNode2";            zooKeeper2.setData("/tests",data.getBytes(),-1);            System.out.println("========查看修改的節點是否成功=========");            System.out.println(new String(zooKeeper1.getData("/tests", false, null)));            System.out.println("========刪除節點========");            System.out.println("16 修改節點...");            zooKeeper2.delete("/tests",-1);            System.out.println("========查看節點是否被刪除=========");            System.out.println("節點狀態:" + zooKeeper2.exists("/tests",false));            System.out.println("=========查看節點是否存在========");            System.out.println("15 查看節點...");            System.out.println(new String(zooKeeper1.getData("/tests", false, null)));        } catch (IOException e) {            e.printStackTrace();        } catch (InterruptedException e) {            e.printStackTrace();        } catch (KeeperException e) {            e.printStackTrace();        }    }}

測試結果:



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.