public class ConsistentHash {
private static String[] tomcatServers = new String[]{"123.111.0.0", "123.101.3.1", "111.20.35.2", "123.98.26.3"};
private static String[] clients = new String[]{"10.78.12.3", "113.25.63.1", "126.12.3.8"};
private static void generalHash() {
int serverCount = tomcatServers.length;
System.out.println("==========>>>> GeneralHash <<<<==========");
for (String client : clients) {
int hash = Math.abs(client.hashCode());
int index = hash % serverCount;
System.out.println("客户端:" + client + " 被路由到服务器编号为:" + index + "(" + tomcatServers[index] + ")");
}
}
private static void consistentHashNoVirtual() {
SortedMap<Integer, String> hashServerMap = new TreeMap<>();
for (String tomcatServer : tomcatServers) {
int serverHash = Math.abs(tomcatServer.hashCode());
hashServerMap.put(serverHash, tomcatServer);
}
System.out.println("==========>>>> ConsistentHashNoVirtual <<<<==========");
for (String client : clients) {
int clientHash = Math.abs(client.hashCode());
SortedMap<Integer, String> integerStringSortedMap = hashServerMap.tailMap(clientHash);
if (integerStringSortedMap.isEmpty()) {
Integer firstKey = hashServerMap.firstKey();
System.out.println("客户端:" + client + " 被路由到服务器:" + hashServerMap.get(firstKey));
} else {
Integer firstKey = integerStringSortedMap.firstKey();
System.out.println("客户端:" + client + " 被路由到服务器:" + hashServerMap.get(firstKey));
}
}
}
private static void consistentHashWithVirtual() {
SortedMap<Integer, String> hashServerMap = new TreeMap<>();
int virtualCount = 3;
for (String tomcatServer : tomcatServers) {
int serverHash = Math.abs(tomcatServer.hashCode());
hashServerMap.put(serverHash, tomcatServer);
for (int i = 0; i < virtualCount; i++) {
int virtualHash = Math.abs((tomcatServer + "#" + i).hashCode());
hashServerMap.put(virtualHash, "----由虚拟节点" + i + "映射过来的请求:" + tomcatServer);
}
}
System.out.println("==========>>>> ConsistentHashWithVirtual <<<<==========");
for (String client : clients) {
int clientHash = Math.abs(client.hashCode());
SortedMap<Integer, String> integerStringSortedMap = hashServerMap.tailMap(clientHash);
if (integerStringSortedMap.isEmpty()) {
Integer firstKey = hashServerMap.firstKey();
System.out.println("客户端:" + client + " 被路由到服务器:" + hashServerMap.get(firstKey));
} else {
Integer firstKey = integerStringSortedMap.firstKey();
System.out.println("客户端:" + client + " 被路由到服务器:" + hashServerMap.get(firstKey));
}
}
}
public static Long hash(String key) {
ByteBuffer buf = ByteBuffer.wrap(key.getBytes());
int seed = 0x1234ABCD;
ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN);
long m = 0xc6a4a7935bd1e995L;
int r = 47;
long h = seed ^ (buf.remaining() * m);
long k;
while (buf.remaining() >= 8) {
k = buf.getLong();
k *= m;
k ^= k >>> r;
k *= m;
h ^= k;
h *= m;
}
if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
finish.put(buf).rewind();
h ^= finish.getLong();
h *= m;
}
h ^= h >>> r;
h *= m;
h ^= h >>> r;
buf.order(byteOrder);
return h;
}
public static void main(String[] args) {
generalHash();
consistentHashNoVirtual();
consistentHashWithVirtual();
long hashCode = hash("1");
System.out.println("Hash:" + hashCode);
}
}