java 连接 zk 的问题小记

今天  学习 zookeeper 相关知识,在使用java  代码 连接zookeeper  过程中,报了一个错  

Opening socket connection to server 192.168.1.157/192.168.1.157:2181. Will not attempt to authenticate using SASL (unknown error)
2018-01-05 14:20:43,073 [org.apache.zookeeper.ClientCnxn]-[INFO] Client session timed out, have not heard from server in 9014ms for sessionid 0x0, closing socket connection and attempting reconnect

堆栈信息很多  就不粘贴了,出现后第一时间百度,度娘结果大部分认为是hosts  文件的问题。

话不多说,本机ping了  zk server  所在的机器,经过互ping  都能ping  通。网络问题排除。

就在迷茫之际,想用本地zkcli.cmd  连接  碰碰看,居然能成功!连接时间挺长。  连接结果如下:

 

豁然开朗,链接时间这么久,要30000!!!!

   将java  代码中的连接超时时间加长,改为30000,运行测试,最终成功!

 

 

 

写这篇博文 ,希望能提醒自己,也为大家遇到这个问题,提供一个解决思路。

 

转载于:https://www.cnblogs.com/miaomiaoquanfa/p/8204373.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于 ZooKeeper 实现的分布式锁的 Java 代码示例: ```java import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; public class DistributedLock { private static final String LOCK_ROOT_PATH = "/locks"; private static final String LOCK_NODE_PREFIX = "lock-"; private final String lockPath; private final ZooKeeper zk; private String lockNode; private CountDownLatch lockAcquiredSignal; public DistributedLock(String zookeeperUrl, String lockPath) throws Exception { this.lockPath = LOCK_ROOT_PATH + "/" + lockPath; this.zk = new ZooKeeper(zookeeperUrl, 3000, null); ensureLockPathExists(); } public void lock() throws Exception { while (true) { try { lockNode = zk.create(lockPath + "/" + LOCK_NODE_PREFIX, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); lockAcquiredSignal = new CountDownLatch(1); waitForLock(); return; } catch (KeeperException.NodeExistsException e) { // node already exists, wait for the previous holder to release the lock waitForPreviousLock(); } } } public void unlock() throws Exception { zk.delete(lockNode, -1); lockNode = null; lockAcquiredSignal.countDown(); } private void ensureLockPathExists() throws Exception { if (zk.exists(lockPath, false) == null) { zk.create(lockPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } private void waitForPreviousLock() throws Exception { List<String> lockNodes = zk.getChildren(lockPath, false); Collections.sort(lockNodes); int currentLockIndex = lockNodes.indexOf(lockNode.substring(lockPath.length() + 1)); if (currentLockIndex <= 0) { throw new IllegalStateException("Lock node not found: " + lockNode); } String previousLockNode = lockPath + "/" + lockNodes.get(currentLockIndex - 1); Stat previousLockStat = zk.exists(previousLockNode, new LockWatcher()); if (previousLockStat != null) { lockAcquiredSignal.await(); } } private void waitForLock() throws Exception { List<String> lockNodes = zk.getChildren(lockPath, false); Collections.sort(lockNodes); int currentLockIndex = lockNodes.indexOf(lockNode.substring(lockPath.length() + 1)); if (currentLockIndex == 0) { // this is the first lock node, lock acquired return; } String previousLockNode = lockPath + "/" + lockNodes.get(currentLockIndex - 1); Stat previousLockStat = zk.exists(previousLockNode, new LockWatcher()); if (previousLockStat == null) { // previous lock node has been deleted, lock acquired return; } lockAcquiredSignal.await(); } private class LockWatcher implements Watcher { @Override public void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeDeleted) { lockAcquiredSignal.countDown(); } } } } ``` 使用示例: ```java DistributedLock lock = new DistributedLock("localhost:2181", "my-lock"); lock.lock(); try { // critical section } finally { lock.unlock(); } ``` 该示例代码使用了 ZooKeeper 的临时顺序节点来实现分布式锁。当多个节点同时请求锁时,它们会在 ZooKeeper 中创建一个临时顺序节点,并按照节点名称的顺序等待前面的节点释放锁。当锁被释放时,它会通知等待的节点,让它们尝试重新获得锁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值