一致性hash算法
2014-09-28 20:06
267 查看
//如果采用hash取宇的操作,则之前做的映射操作基本都会无效,所以引入了一致性hash算法
package hash; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*; /** * Created by IntelliJ IDEA. * User: test * Date: 12-5-24 * Time: 下午5:37 * To change this template use File | Settings | File Templates. */ public class ConsistencyHash { private TreeMap<Long,Object> nodes = null; //真实服务器节点信息 private List<Object> shards = new ArrayList(); //设置虚拟节点数目 private int VIRTUAL_NUM = 4; /** * 初始化一致环 */ public void init() { shards.add("192.168.0.0-服务器0"); shards.add("192.168.0.1-服务器1"); shards.add("192.168.0.2-服务器2"); shards.add("192.168.0.3-服务器3"); shards.add("192.168.0.4-服务器4"); nodes = new TreeMap<Long,Object>(); for(int i=0; i<shards.size(); i++) { Object shardInfo = shards.get(i); for(int j=0; j<VIRTUAL_NUM; j++) { nodes.put(hash(computeMd5("SHARD-" + i + "-NODE-" + j),j), shardInfo); } } } /** * 根据key的hash值取得服务器节点信息 * @param hash * @return */ public Object getShardInfo(long hash) { Long key = hash; SortedMap<Long, Object> tailMap=nodes.tailMap(key); if(tailMap.isEmpty()) { key = nodes.firstKey(); } else { key = tailMap.firstKey(); } return nodes.get(key); } /** * 打印圆环节点数据 */ public void printMap() { System.out.println(nodes); } /** * 根据2^32把节点分布到圆环上面。 * @param digest * @param nTime * @return */ public long hash(byte[] digest, int nTime) { long rv = ((long) (digest[3+nTime*4] & 0xFF) << 24) | ((long) (digest[2+nTime*4] & 0xFF) << 16) | ((long) (digest[1+nTime*4] & 0xFF) << 8) | (digest[0+nTime*4] & 0xFF); return rv & 0xffffffffL; /* Truncate to 32-bits */ } /** * Get the md5 of the given key. * 计算MD5值 */ public byte[] computeMd5(String k) { MessageDigest md5; try { md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("MD5 not supported", e); } md5.reset(); byte[] keyBytes = null; try { keyBytes = k.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unknown string :" + k, e); } md5.update(keyBytes); return md5.digest(); } public static void main(String[] args) { Random ran = new Random(); ConsistencyHash hash = new ConsistencyHash(); hash.init(); hash.printMap(); //循环50次,是为了取50个数来测试效果,当然也可以用其他任何的数据来测试 for(int i=0; i<50; i++) { System.out.println(hash.getShardInfo(hash.hash(hash.computeMd5(String.valueOf(i)),ran.nextInt(hash.VIRTUAL_NUM)))); } } }
相关文章推荐
- Consistent Hashing Algorithm 一致性hash算法
- 一致性Hash算法
- 一致性hash算法 - consistent hashing
- 编程艺术第十六~第二十章:全排列/跳台阶/奇偶调序,及一致性Hash算法
- 基于一致性hash算法(consistent hashing)的使用详解
- C# Redis客户端对一致性hash算法的实现
- 【转】一致性hash算法(consistent hashing)
- 编程艺术第十六~第二十章:全排列/跳台阶/奇偶调序,及一致性Hash算法
- 一致性hash算法(二)
- (转) 一致性Hash算法在Memcached中的应用
- memcache分布式 [一致性hash算法] 的php实现
- java学习一致性HASH算法的JAVA实现
- 对一致性hash算法的理解
- 什么是一致性Hash算法?
- 一致性Hash算法
- 基于一致性hash算法 C++语言的实现详解
- python学习(swift 一致性hash算法分析)
- 追踪分布式Memcached默认的一致性hash算法
- 一致性hash算法
- 基于一致性hash算法 C++语言的实现详解