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();  
  }  
   
 } 
命令行操作:

 ./zkCli.sh -server localhost:2181

[zk: localhost:2181(CONNECTED) 2] create /testzoo1 Testdata
Created /testzoo1
[zk: localhost:2181(CONNECTED) 3] ls /
[testzoo1, zookeeper]
[zk: localhost:2181(CONNECTED) 4] ls /testzoo1
[]
[zk: localhost:2181(CONNECTED) 5] get /testzoo1
Testdata
cZxid = 0x200000013
ctime = Tue May 14 10:37:37 CST 2013
mZxid = 0x200000013
mtime = Tue May 14 10:37:37 CST 2013
pZxid = 0x200000013
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
[zk: localhost:2181(CONNECTED) 6] 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要安装分布式Zookeeper,首先需要下载Zookeeper的压缩包并解压。可以使用命令"mv zookeeper-3.4.6 zookeeper"将解压后的文件夹重命名为zookeeper,以方便后续操作。 在部署Zookeeper之前,需要确保服务器上已经安装了Java Development Kit(JDK)。可以使用命令"java -version"来检查是否已经安装了相关的Java环境。 Zookeeper是大数据领域中非常重要的一个服务,尤其是在Hadoop生态圈中。几乎所有分布式服务都依赖于Zookeeper来解决分布式一致性的问题。Zookeeper最初是由雅虎开发,并后来被Apache接手并孵化成为顶级项目。 所以,安装分布式Zookeeper可以按照以下步骤进行: 1. 下载Zookeeper的压缩包,并解压缩。 2. 将解压后的文件夹重命名为zookeeper。 3. 确保服务器上已经安装了JDK,可以使用"java -version"命令来检查。 4. 配置Zookeeper的相关参数,如端口号等。 5. 启动Zookeeper服务。 请注意,分布式安装只是在一台机器上运行多个Zookeeper应用程序,并不是真正的分布式部署。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [zookeeper 分布式安装](https://blog.csdn.net/weixin_34186128/article/details/93298838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [zookeeper分布式搭建(1)1](https://download.csdn.net/download/weixin_35741494/86344095)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [2020/04/27 06-Zookeeper分布式安装配置](https://blog.csdn.net/qq_42227818/article/details/105800645)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值