Memcached多台服务器多节点负载均衡测试

在主机和从机上分别开启Memcached多个端口,这里以192.183.3.230为主机(Master,开启1121111212两个Memcached端口,对应的监听端口分别是1221112212.,如下:


[root@nn ~]# /usr/local/bin/memcached -v -d -l 192.183.3.230 -p 11211 -u root -c 1024 -m 1024 -X 12211
[root@nn ~]# /usr/local/bin/memcached -v -d -l 192.183.3.230 -p 11212 -u root -c 1024 -m 1024 -X 12212

Memcached多台服务器多节点负载均衡测试_第1张图片

以192.183.3.189为从机(Slaver,开启1121111212两个Memcached端口,对应的监听端口分别是1221112212.,如下:

[root@zengxiangtao ~]#/usr/local/bin/memcached -v -d -l 192.183.3.189 -p 11211 -u root -x 192.183.3.230 -X 12211
[root@zengxiangtao ~]#/usr/local/bin/memcached -v -d -l 192.183.3.189 -p 11212 -u root -x 192.183.3.230 -X 12212

Memcached多台服务器多节点负载均衡测试_第2张图片

测试的JAVA代码如下:

import java.util.Date;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemcachedDemo1 {
	protected static MemCachedClient mcc = new MemCachedClient();
	public static MemCachedClient getMcc() {
		return mcc;
	}
	protected static MemcachedDemo1 memCached = new MemcachedDemo1();
	protected MemcachedDemo1() {
	}
	public static MemcachedDemo1 getInstance() {
		return memCached;
	}
	public boolean add(String key, Object value) {
		return mcc.add(key, value);
	}
	public boolean add(String key, Object value, Date expiry) {
		return mcc.add(key, value, expiry);
	}
	public Object get(String key) {
		return mcc.get(key);
	}
	public boolean flash_all() {
		return mcc.flushAll();
	}
	public static void main(String[] args) {
		MemcachedDemo1 cache = MemcachedDemo1.getInstance();
		
	cache.setServers(new String[]{"192.183.3.189:11211","192.183.3.230:11211","192.183.3.189:11212","192.183.3.230:11212"});
		for(int i = 0 ; i<2000;i++) {
	     cache.add("key"+i, "value"+i);
//	     cache.flash_all();
		}
	
		int count1 = 0;
		cache.setServers(new String[]{"192.183.3.189:11211"});
		for(int i = 0 ; i<2000;i++) {
			if(cache.get("key"+i) != null) {
				count1++;
			}
		}
		System.out.println("189-11211 " + count1);
		int count2 = 0;
		cache.setServers(new String[]{"192.183.3.230:11211"});
		for(int i = 0 ; i<2000;i++) {
			if(cache.get("key"+i) != null) {
				count2++;
			}
		}
		System.out.println("230-11211 " + count2);
		
		int count3 = 0;
		cache.setServers(new String[]{"192.183.3.189:11212"});
		for(int i = 0 ; i<2000;i++) {
			if(cache.get("key"+i) != null) {
				count3++;
			}
		}
		System.out.println("189-11212 " + count3);
		int count4 = 0;
		cache.setServers(new String[]{"192.183.3.230:11212"});
		for(int i = 0 ; i<2000;i++) {
			if(cache.get("key"+i) != null) {
				count4++;
			}
		}
		System.out.println("230-11212 " + count4);
	}
	
	public void setServers(String[] servers) {
		Integer[] weights = {3,3,3,3};
		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.initialize();
	}
}

理论上23018911211端口、23018911212端口插入的数据量应该一致,并且应该是均衡的,运行结果显示:189-11211 999230-11211 999、189-11212 1001、230-11212 1001,这说明开启了高可用之后,主从机对应节点的数据量是一致的,并且基本是均衡的。


你可能感兴趣的:(负载均衡,memcached,NoSQL)