一致性哈希算法介绍:一致性哈希算法
如果我们想使用一致性哈希算法,只需要添加pool.setHashingAlg(SockIOPool.CONSISTENT_HASH);这行代码即可
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class Test {
public static void main(String[] args) {
MemCachedClient client = new MemCachedClient();
String[] servers = {"192.168.52.129:9999",
"192.168.52.131:9999"};
Integer[] weights = {1, 1};
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(servers);
pool.setWeights(weights);
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000 * 60 * 60 * 6);
pool.setMaintSleep(30);
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
pool.setHashingAlg(SockIOPool.CONSISTENT_HASH); // 3
pool.initialize();
client.set("test", "This is a test String");
String test = (String) client.get("test");
System.out.println(test);
}
}
来看下实际效果
sean@ubuntu1:~$ telnet 192.168.52.131 9999
Trying 192.168.52.131...
Connected to 192.168.52.131.
Escape character is '^]'.
get test
END
sean1@ubuntu2:~$ telnet 192.168.52.129 9999
Trying 192.168.52.129...
Connected to 192.168.52.129.
Escape character is '^]'.
get test
VALUE test 32 21
This is a test String
END
MemcachedClient的初始化方法,通过该方法可确定Client的具体实现类为AscIIClient
public MemCachedClient() {
this(null, true, false);
}
public MemCachedClient(String var1, boolean var2, boolean var3) {
this.BLAND_DATA_SIZE = " ".getBytes();
if(var3) {
this.client = new BinaryClient(var1);
} else {
this.client = (MemCachedClient)(var2?
new AscIIClient(var1):new AscIIUDPClient(var1));
}
}
默认使用的连接池名称是default
public class AscIIClient extends MemCachedClient {
// var1 = null
public AscIIClient(String var1) {
su