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();  
  }  
   
 } 

您的回应...

相关话题

查看全部

也许你感兴趣

换一批

热门标签

更多