ZeeKeeper 集群 伪分布式配置 zookeeper的使用

纯粹笔记 :不多说了。

解压tar.gz文件,配置修改配置conf

1,将zoo_sample.cfg 拷贝3分 zoo1.cfg,zoo2.cfg,zoo3.cfg

******************************************************************

1,zoo1.cfg:

---------------------------

tina@ubuntu:~/zookeeper/conf$ cat zoo1.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
dataDir=/home/sina/zookeeper/data
# the port at which the clients will connect
clientPort=2184
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890

×××××××××××××××××××××××××××××××××××××××××××××××

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

×××××××××××××××××××××××××××××××××××××××××××××××××

2,zoo2.cfg

------------------------------------------------------------------------------------

tina@ubuntu:~/zookeeper/conf$ more zoo2.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
dataDir=/home/sina/zookeeper/data1
# the port at which the clients will connect
clientPort=2182
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890

******************************************************************

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

*********************************************************************

3,zoo3.cfg

-------------------------------------------------------------------------------------

tina@ubuntu:~/zookeeper/conf$ more zoo3.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
dataDir=/home/sina/zookeeper/data2
# the port at which the clients will connect
clientPort=2183
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890

×××××××××××××××××××××××××××××××××××××××××××××××××

》》》分别在 /data,data1,data2,下建立myid 文件,内容分别是1,2,3

---------------------------------


启动zookeeper服务

分别启动1,2,3:

./zkServer.sh start zoo1.cfg

./zkServer.sh start zoo2.cfg

./zkServer.sh start zoo3.cfg

前面两个起的时候可能会有错误提示:

2012-11-21 01:28:12,848 - WARN [QuorumPeer:/0:0:0:0:0:0:0:0:2184:QuorumCnxManager@379] - Cannot open channel to 3 at election address localhost/127.0.0.1:3890
java.net.ConnectException: Connection refused

2012-11-21 01:28:12,847 - WARN [QuorumPeer:/0:0:0:0:0:0:0:0:2184:QuorumCnxManager@379] - Cannot open channel to 2 at election address localhost/127.0.0.1:3889
java.net.ConnectException: Connection refused

这个可以不用管,等服务都起来之后就不会有这个提示了

好了 一样画瓢来个示例:

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;

public class demo {

// 会话超时时间,设置为与系统默认时间一致

private static final int SESSION_TIMEOUT = 30000;

// 创建 ZooKeeper 实例

ZooKeeper zk;

// 创建 Watcher 实例

Watcher wh = new Watcher() {

public void process(org.apache.zookeeper.WatchedEvent event) {
System.out.println("event="+event.toString());
}

};

// 初始化 ZooKeeper 实例

private void createZKInstance() throws IOException{
zk = new ZooKeeper("localhost:2183", demo.SESSION_TIMEOUT, this.wh);

//这里随便随便3个中的任何一个都是可以连接的了
}

private void ZKOperations() throws IOException, InterruptedException, KeeperException{

System.out.println("\n 创建 ZooKeeper 节点 (znode : zoo2, 数据: myData2 ,权限: OPEN_ACL_UNSAFE ,节点类型: Persistent");

zk.create("/zoo2", "myData2".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

System.out.println("\n 查看是否创建成功: ");

System.out.println(new String(zk.getData("/zoo2", false, null)));

System.out.println("\n 修改节点数据 ");

zk.setData("/zoo2", "shenlan211314".getBytes(), -1);

System.out.println("\n 查看是否修改成功: ");

System.out.println(new String(zk.getData("/zoo2", false, null)));
System.out.println("\n 修改节点数据 ");

zk.setData("/zoo2", "Hello_World".getBytes(), -1);

System.out.println("\n 查看是否修改成功: ");

System.out.println(new String(zk.getData("/zoo2", false, null)));

System.out.println("\n 删除节点 ");

zk.delete("/zoo2", -1);

System.out.println("\n 查看节点是否被删除: ");

System.out.println(" 节点状态: [" + zk.exists("/zoo2", false) + "]");

}

private void ZKClose() throws InterruptedException{
zk.close();
}

public static void main(String[] args) throws IOException,InterruptedException, KeeperException {
demo dm = new demo();
dm.createZKInstance();
dm.ZKOperations();
dm.ZKClose();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值