java实现:http://www.oschina.net/code/snippet_730640_22941
参考:http://amix.dk/blog/post/19367
python实现:
import md5 class HashRing(object): def __init__(self, nodes=None, replicas=3): """Manages a hash ring. `nodes` is a list of objects that have a proper __str__ representation. `replicas` indicates how many virtual points should be used pr. node, replicas are required to improve the distribution. """ self.replicas = replicas self.ring = dict() self._sorted_keys = [] if nodes: for node in nodes: self.add_node(node) def add_node(self, node): """Adds a `node` to the hash ring (including a number of replicas). """ for i in xrange(0, self.replicas): key = self.gen_key('%s:%s' % (node, i)) self.ring[key] = node self._sorted_keys.append(key) self._sorted_keys.sort() def remove_node(self, node): """Removes `node` from the hash ring and its replicas. """ for i in xrange(0, self.replicas): key = self.gen_key('%s:%s' % (node, i)) del self.ring[key] self._sorted_keys.remove(key) def get_node(self, string_key): """Given a string key a corresponding node in the hash ring is returned. If the hash ring is empty, `None` is returned. """ return self.get_node_pos(string_key)[0] def get_node_pos(self, string_key): """Given a string key a corresponding node in the hash ring is returned along with it's position in the ring. If the hash ring is empty, (`None`, `None`) is returned. """ if not self.ring: return None, None key = self.gen_key(string_key) nodes = self._sorted_keys for i in xrange(0, len(nodes)): node = nodes[i] if key <= node: return self.ring[node], i return self.ring[nodes[0]], 0 def get_nodes(self, string_key): """Given a string key it returns the nodes as a generator that can hold the key. The generator is never ending and iterates through the ring starting at the correct position. """ if not self.ring: yield None, None node, pos = self.get_node_pos(string_key) for key in self._sorted_keys[pos:]: yield self.ring[key] while True: for key in self._sorted_keys: yield self.ring[key] def gen_key(self, key): """Given a string key it returns a long value, this long value represents a place on the hash ring. md5 is currently used because it mixes well. """ m = md5.new() m.update(key) return long(m.hexdigest(), 16) memcache_servers = ['192.168.1.1:11212','192.168.1.2:11212'] ring = HashRing(memcache_servers) server = ring.get_node('my_key') print server
java实现:
package util; import java.util.Arrays; import java.util.Collection; import java.util.SortedMap; import java.util.TreeMap; /** * 一致性Hash算法 * 算法详解:http://blog.csdn.net/sparkliang/article/details/5279393 * 算法实现:https://weblogs.java.net/blog/2007/11/27/consistent-hashing * @author xiaoleilu * * @param <T> 节点类型 */ public class ConsistentHash<T> { /** Hash计算对象,用于自定义hash算法 */ HashFunc hashFunc; /** 复制的节点个数 */ private final int numberOfReplicas; /** 一致性Hash环 */ private final SortedMap<Integer, T> circle = new TreeMap<Integer, T>(); /** * 构造,使用Java默认的Hash算法 * @param numberOfReplicas 复制的节点个数,增加每个节点的复制节点有利于负载均衡 * @param nodes 节点对象 */ public ConsistentHash(int numberOfReplicas, Collection<T> nodes) { this.numberOfReplicas = numberOfReplicas; this.hashFunc = new HashFunc() { @Override public Integer hash(Object key) { String data = key.toString(); //默认使用FNV1hash算法 final int p = 16777619; int hash = (int) 2166136261L; for (int i = 0; i < data.length(); i++) hash = (hash ^ data.charAt(i)) * p; hash += hash << 13; hash ^= hash >> 7; hash += hash << 3; hash ^= hash >> 17; hash += hash << 5; return hash; } }; //初始化节点 for (T node : nodes) { add(node); } } /** * 构造 * @param hashFunc hash算法对象 * @param numberOfReplicas 复制的节点个数,增加每个节点的复制节点有利于负载均衡 * @param nodes 节点对象 */ public ConsistentHash(HashFunc hashFunc, int numberOfReplicas, Collection<T> nodes) { this.numberOfReplicas = numberOfReplicas; this.hashFunc = hashFunc; //初始化节点 for (T node : nodes) { add(node); } } /** * 增加节点<br> * 每增加一个节点,就会在闭环上增加给定复制节点数<br> * 例如复制节点数是2,则每调用此方法一次,增加两个虚拟节点,这两个节点指向同一Node * 由于hash算法会调用node的toString方法,故按照toString去重 * @param node 节点对象 */ public void add(T node) { for (int i = 0; i < numberOfReplicas; i++) { circle.put(hashFunc.hash(node.toString() + i), node); } } /** * 移除节点的同时移除相应的虚拟节点 * @param node 节点对象 */ public void remove(T node) { for (int i = 0; i < numberOfReplicas; i++) { circle.remove(hashFunc.hash(node.toString() + i)); } } /** * 获得一个最近的顺时针节点 * @param key 为给定键取Hash,取得顺时针方向上最近的一个虚拟节点对应的实际节点 * @return 节点对象 */ public T get(Object key) { if (circle.isEmpty()) { return null; } int hash = hashFunc.hash(key); if (!circle.containsKey(hash)) { SortedMap<Integer, T> tailMap = circle.tailMap(hash); //返回此映射的部分视图,其键大于等于 hash hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); } //正好命中 return circle.get(hash); } /** * Hash算法对象,用于自定义hash算法 * @author xiaoleilu * */ public interface HashFunc { public Integer hash(Object key); } public static void main(String[] args) { ConsistentHash<String> hash = new ConsistentHash<String>(3, Arrays.asList("192.168.1.1:11211", "192.168.1.2:11211")); System.out.println(hash.get("test")); } }
相关推荐
该项目是一款基于freeRTOS操作系统和STM32F103x微控制器的手机远程控制浴室温度系统设计源码,共包含1087个文件,包括580个C语言源文件、269个头文件、45个汇编源文件、36个数据文件、36个目标文件、35个编译规则文件、28个包含文件、27个文本文件、6个源文件、3个归档文件。此系统通过手机远程实现对浴室温度的有效控制,适用于智能浴室环境管理。
labview程序代码参考学习使用,希望对你有所帮助。
labview程序代码参考学习使用,希望对你有所帮助。
labview程序代码参考学习使用,希望对你有所帮助。
labview程序代码参考学习使用,希望对你有所帮助。