java - zookeeper curator 分布式锁,zk服务停了,如何保证业务逻辑的正常处理

 手机用户2402851155 发布于 2022-10-27 13:39

zookeeper curator 分布式锁,zk服务停了,如何保证业务逻辑的正常处理

InterProcessMutex lock = new InterProcessMutex(client, path);
if (lock.acquire(10 * 1000, TimeUnit.SECONDS)) {
doSth();
}

如果zk连接失败,如何跳过锁判断,执行下面的业务逻辑,不至于业务瘫痪呢

各位大神求支招

1 个回答
  • 当连接失败的时候,acquire会马上抛出异常的,你只要把异常catch住就好

    public boolean acquire(long time,
                           TimeUnit unit)
                    throws Exception
                    
    Acquire the mutex - blocks until it's available or the given time expires. Note: the same thread can call acquire re-entrantly. Each call to acquire that returns true must be balanced by a call to release()
    
    Specified by:
    acquire in interface InterProcessLock
    
    Parameters:
    time - time to wait
    unit - time unit
    
    Returns:
    true if the mutex was acquired, false if not
    
    Throws:
    Exception - ZK errors, connection interruptions
    2022-11-12 01:41 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有