Use zookeeper to implement consistent hash.
When the redis service starts, it writes its route information to ZK through a temporary node, and the client reads available route information through ZK client.
Zkclient is required to monitor node changes and update routing policies in a timely manner
The following is the consistent version hash of C #.Algorithm:
1:ClassKetamanodelocator
2:{
3:PrivateDictionary <Long, Rediscluster> ketamanodes;
4:PrivateHashalgorithm hashalg;
5:Private IntNumreps = 160;
6:Private Long[] Keys;
7:
8:PublicKetamanodelocator (list <rediscluster> nodes)
9:{
10:Ketamanodes =NewDictionary <Long, Rediscluster> ();
11:
12:// Generate ncopies virtual nodes for all nodes
13:For(IntJ = 0; j <nodes. Count; j ++ ){
14:Rediscluster node = nodes [J];
15:IntNumreps = node. weight;
16:
17:// Each four virtual nodes is a group
18:For(IntI = 0; I <numreps/4; I ++ ){
19:Byte[] Digest = computemd5 (
20:String. Format ("{0 }_{ 1 }_{ 2 }", Node. rolename, node. routevalue, I ));
21:
22:/** MD5 is a 16-byte array, which groups 16-byte arrays every four bytes,
23:* Corresponds to one virtual node, which is why four virtual nodes are divided into one group */
24:For(IntH = 0; H <4; H ++ ){
25:
26:LongRv = ((Long) (Digest [3 + H * 4] & 0xff) <24)
27:| ((Long) (Digest [2 + H * 4] & 0xff) <16)
28:| ((Long) (Digest [1 + H * 4] & 0xff) <8)
29:| ((Long) Digest [0 + H * 4] & 0xff );
30:
31:Rv = RV & 0 xffffffffl;/* Truncate to 32-bits */
32:Ketamanodes [RV] = node;
33:}
34:}
35:}
36:
37:Keys = ketamanodes. Keys. orderby (P => P). toarray ();
38:}
41:PublicRediscluster getworkernode (StringK)
42:{
43:Byte[] Digest = computemd5 (k );
44:ReturnGetnodeinner (Hash (Digest, 0 ));
45:}
46:
47:Rediscluster getnodeinner (LongHash)
48:{
49:If(Ketamanodes. Count = 0)
50:Return Null;
51:LongKey = hash;
52:IntNear = 0;
53:IntIndex = array. binarysearch (keys, hash );
54:If(Index <0 ){
55:Near = (~ Index );
56:If(Near = keys. length)
57:Near = 0;
58:}
59:Else{
60:Near = index;
61:}
62:
63:ReturnKetamanodes [Keys [near];
64:}
65:
66:Public Static LongHash (Byte[] Digest,IntNtime)
67:{
68:LongRv = ((Long) (Digest [3 + ntime * 4] & 0xff) <24)
69:| ((Long) (Digest [2 + ntime * 4] & 0xff) <16)
70:| ((Long) (Digest [1 + ntime * 4] & 0xff) <8)
71:| ((Long) Digest [0 + ntime * 4] & 0xff );
72:
73:ReturnRV & 0 xffffffffl;/* Truncate to 32-bits */
74:}
79:Public Static Byte[] Computemd5 (StringK)
80:{
81:MD5 MD5 =NewMd5cryptoserviceprovider ();
82:
83:Byte[] Keybytes = md5.computehash (encoding. utf8.getbytes (k ));
84:Md5.clear ();
85:ReturnKeybytes;
86:}
87:}