Below 2 images from: http://www.open-open.com/doc/view/2e0a82e0081d489dace301a2c512053c
For zookeeper service installation, configuration, startup, client operations see:
http://aiilive.blog.51cto.com/1925756/1684451
http://aiilive.blog.51cto.com/1925756/1684145
1. Generate serial numbers using Zookeeper's Znode data version
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/83/wKioL1XS7Crg6_YaAAQNPtfUuYg724.jpg "style=" float: none; "title=" Zk-seq-1.png "alt=" Wkiol1xs7crg6_yaaaqnptfuuyg724.jpg "/>
Using the Zkclient Encapsulation package operation:
Create a "/CREATESEQ" node that stores the SEQ in advance createmode.persistentpublic static final string seq_znode = "/SEQ"//Distributed SEQ generation via Znode data version public static class task1 implements runnable { private final string taskname; Public task1 (String taskname) { this.taskname = taskName; } @Override public void run () { zkclient zkclient = new zkclient ("192.168.88.153:2181", 3000, 1000); stat stat = zkclient.writedata (seq_znode, new byte[0], -1); int versionasseq = stat.getversion (); syStem.out.println (taskname + " obtain seq=" + versionasseq); zkclient.close (); }}//mainfinal executorservice service = executors.newfixedthreadpool (;for ) (int i = 0; i < 20; i++) { service.execute (New task1 ("[Concurrent-" + i + "]));}
2. Implementation of Znode and distributed lock persistence Znode with temporary serial number
2.1 is implemented by using distributed lock persistence Znode
The client uses the Apache curator framework, code: HTTPS://CODE.CSDN.NET/SNIPPETS/929300
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/83/wKioL1XS7CrzHIyFAAQ4t7a79_I242.jpg "style=" float: none; "title=" Zk-seq-2.png "alt=" Wkiol1xs7crzhiyfaaq4t7a79_i242.jpg "/>
2.2 Znode with a temporary serial number
Client use: Zkclient (https://github.com/adyliu/zkclient)
Create the node "/lock" of the lock object in advance createmode.persistentpublic static final String Lock_znode = "/lock";
Distributed-type lock for generating public static class task2 implements runnable, izkchildlistener of distributive seq { private final string taskname; private final zkclient zkclient; private final string lockprefix = "/loc"; private final string selfznode; public task2 (String taskname) { This.taskname = taskname; zkclient = new zkclient ("192.168.88.153:2181", 30000, 10000); Selfznode = zkclient.createephemeralsequential (lock_znode + lockprefix, new byte [0]); } @Override public void run () { zkclient.subscribechildchanges (lock_znode, this); do { } while ( Zkclient.isconnected ()); } private void createseq () { stat stat = new stat (); byte[] olddata = zkclient.readdata (LOCK_ZNODE, stat); byte[] newdata = update (OldData); zkclient.writedata (Lock_znode, newdata); system.out.println (taskname + selfznode + " obtain seq=" + new string (NewData)); } private byte[] update (Byte[] currentdata) { string s = new string (CurrentData) ; int d = integer.parseint (s); d = d + 1; s = string.valueof (d); return s.getbytes (); } @Override public void Handlechildchange (String parentpath, list<string> currentchildren) throws exception { string[] childrensznode = Currentchildren.toarray (New string[currentchildren.size ()); arrays.sort (Childrensznode); string minznode = LOCK_ZNODE + "/" + childrensznode[0]; if (SelfZnode.equals (MinZnode)) { createseq (); zkclient.unsubscribechildchanges (LOCK_ZNODE, this); zkclient.delete (SelfZnode); zkclient.close (); } }}
Complete code See: https://code.csdn.net/snippets/929320
This article is from the "Red Horse Red" blog, please be sure to keep this source http://aiilive.blog.51cto.com/1925756/1685614
Relying on zookeeper to generate globally unique serial numbers