使用Curator實現的zookeeper分佈式鎖出現的Unimplemented for {root.path}

  1. 問題描述
    Curator使用 ZooKeeper 做爲分佈式鎖,啓動時發生該異常。
    Curator 客戶端版本: curator-recipes-2.10.0
    ZooKeeper 服務器版本:3.4.13
$ echo stat | nc localhost 2181
Zookeeper version: 3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
  1. 異常日誌
Exception in thread "pool-5-thread-4789" java.lang.NoClassDefFoundError: Could not initialize class com.itstyle.seckill.distributedlock.zookeeper.ZkLockUtil

以及html

org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for /curator/lock/_c_9e23f33f-49e7-48df-a268-0dde3afb0e35-lock-
  1. 問題分析 UnimplementedException 的描述是 Operation is unimplemented,即該操做未實現異常。 最後的異常調用棧 ZooKeeper.create(ZooKeeper.java:1297) 方法的源代碼:
/**
     * Create a node with the given path and returns the Stat of that node. The
     * node data will be the given data and node acl will be the given acl.
     */
    public String create(final String path, byte data[], List<ACL> acl,
            CreateMode createMode, Stat stat)
            throws KeeperException, InterruptedException {
        final String clientPath = path;
        PathUtils.validatePath(clientPath, createMode.isSequential());

        final String serverPath = prependChroot(clientPath);

        RequestHeader h = new RequestHeader();
        h.setType(createMode.isContainer() ? ZooDefs.OpCode.createContainer : ZooDefs.OpCode.create2);
        CreateRequest request = new CreateRequest();
        Create2Response response = new Create2Response();
        request.setData(data);
        request.setFlags(createMode.toFlag());
        request.setPath(serverPath);
        if (acl != null && acl.size() == 0) {
            throw new KeeperException.InvalidACLException();
        }
        request.setAcl(acl);
        ReplyHeader r = cnxn.submitRequest(h, request, response, null);
        if (r.getErr() != 0) {
            throw KeeperException.create(KeeperException.Code.get(r.getErr()),
                    clientPath);
        }
        if (stat != null) {
            DataTree.copyStat(response.getStat(), stat);
        }
        if (cnxn.chrootPath == null) {
            return response.getPath();
        } else {
            return response.getPath().substring(cnxn.chrootPath.length());
        }
    }

ZooKeeper.create(ZooKeeper.java:1297) 方法看,是ZooKeeper建立節點時發生了異常。java

本人使用的是zookeeper的最新穩定版3.4.13(最新不穩定版是3.5.X),由於使用curator的版本必須匹配服務器上安裝zookeeper的版本, 因此curator不能使用最新版本,不然建立節點時就會報錯誤。 org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for /curator/test01 官方解釋:node

Versions
The are currently two released versions of Curator, 2.x.x and 3.x.x:

Curator 2.x.x - compatible with both ZooKeeper 3.4.x and ZooKeeper 3.5.x
Curator 3.x.x - compatible only with ZooKeeper 3.5.x and includes support for new features such as dynamic reconfiguration, etc.

Apache Curator對ZooKeeper版本兼容性ZooKeeper Version Compatibility 解釋以下:apache

ZooKeeper 3.5.x
    Curator 4.0 has a hard dependency on ZooKeeper 3.5.x
ZooKeeper 3.4.x
    Curator 4.0 supports ZooKeeper 3.4.x ensembles in a soft-compatibility mode.

能夠肯定,問題根源是Curator的2.10.0版本沒法匹配ZooKeeper服務器的3.4.13版本。服務器

  1. 解決方案
  • 將 Curator 版本升級到最新的 curator-recipes-4.1.0,剔除包含的zookeeper的jar,並增長zookeeper最新的3.4.13的jar
<dependency> 
	<groupId>org.apache.curator</groupId> 
	<artifactId>curator-recipes</artifactId> 
	<version>4.1.0</version>
	<exclusions>
		<exclusion>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.4.13</version>
</dependency>
  • 或者將curator-recipes升級到2.x.x最新的 curator-recipes-2.13.0
相關文章
相關標籤/搜索