repcached实现memcached主备

介绍

repcached是日本人开发的基于Memcached的一个patch,实现Memcached的复制功能,它支持多个Memcached之间相互复制(双向复制,主备都是可读可写的),可以解决Memcached的容灾问题。我们主要是用作Memcached的主备。


安装

repcached 的安装非常简单,首先确定是否已经安装:

yum install libevent-devel -y

之后就开始安装repcached :

wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
tar -zxf memcached-1.2.8-repcached-2.2.tar.gz
cd memcached-1.2.8-repcached-2.2
./configure --enable-replication --program-transform-name=s/memcached/repcached/
make && make install
程序安装在/usr/local/bin目录下。
至此,repcached安装完成。


启动

root用户启动要加参数-u root,非root用户不需要

参数说明:

[root@template memcached-1.2.8-repcached-2.2]# repcached -help
memcached 1.2.8
repcached 2.2
-p <num>      TCP port number to listen on (default: 11211)
-U <num>      UDP port number to listen on (default: 11211, 0 is off)
-s <file>     unix socket path to listen on (disables network support)
-a <mask>     access mask for unix socket, in octal (default 0700)
-l <ip_addr>  interface to listen on, default is INDRR_ANY
-d            run as a daemon
-r            maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num>      max memory to use for items in megabytes, default is 64 MB
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections, default is 1024
-k            lock down all paged memory.  Note that there is a
              limit on how much memory you may lock.  Trying to
              allocate more than that would fail, so be sure you
              set the limit correctly for the user you started
              the daemon with (not for -u <username> user;
              under sh this is done with 'ulimit -S -l NUM_KB').
-v            verbose (print errors/warnings while in event loop)
-vv           very verbose (also print client commands/reponses)
-h            print this help and exit
-i            print memcached and libevent license
-P <file>     save PID in <file>, only used with -d option
-f <factor>   chunk size growth factor, default 1.25
-n <bytes>    minimum space allocated for key+value+flags, default 48
-R            Maximum number of requests per event
              limits the number of requests process for a given con nection
              to prevent starvation.  default 20
-b            Set the backlog queue limit (default 1024)
-x <ip_addr>  hostname or IP address of peer repcached
-X <num>      TCP port number for replication (default: 11212)

1、启动master

/usr/local/bin/repcached -p 11211 -v -d -u root

2、启动slave

/usr/local/bin/repcached -p 11213 -x 127.0.0.1 -v -d -u root

#复制的端口默认是11212的,所以我们slave的启动使用11213.

#slave的启动和master类似,只是多了-x参数,指定复制的ip,如果复制端口不是11212,则需要-X参数指定。

3、检测是否启动成功

[root@template memcached-1.2.8-repcached-2.2]# ps -ef|grep repcached
root     17339     1  0 11:00 ?        00:00:00 /usr/local/bin/repcached -p 11213 -x 127.0.0.1 -v -d -u root
root     17353     1  0 11:01 ?        00:00:00 /usr/local/bin/repcached -p 11211 -v -d -u root
root     17442 30408  0 11:16 pts/0    00:00:00 grep repcached


#说明已经启动成功了

测试

[root@template memcached-1.2.8-repcached-2.2]# telnet localhost 11211
Trying 127.0.0.1...
Connected to template.zfyunat.com (127.0.0.1).
Escape character is '^]'.
set key1 0 0 5
hello
STORED
get key1
VALUE key1 0 5
hello
END
quit
Connection closed by foreign host.
 
[root@template memcached-1.2.8-repcached-2.2]# telnet localhost 11213
Trying 127.0.0.1...
Connected to template.zfyunat.com (127.0.0.1).
Escape character is '^]'.
get key1
VALUE key1 0 5
hello
END
quit
Connection closed by foreign host.


[root@template memcached-1.2.8-repcached-2.2]# telnet localhost 11213
Trying 127.0.0.1...
Connected to template.zfyunat.com (127.0.0.1).
Escape character is '^]'.
set key2 0 0 5
hello
STORED
get key2
VALUE key2 0 5
hello
END
quit
Connection closed by foreign host.
 
[root@template memcached-1.2.8-repcached-2.2]# telnet localhost 11211
Trying 127.0.0.1...
Connected to template.zfyunat.com (127.0.0.1).
Escape character is '^]'.
get key2
VALUE key2 0 5
hello
END
quit
Connection closed by foreign host.

使用实例(client使用xmemcached)

public static void main(String[] args) throws Exception {
		MemcachedClient client = null;
		MemcachedClientBuilder builder = null;
		try {
			// 地址是使用逗号分隔的,意义是:11211是master,11213是slave
			builder = new XMemcachedClientBuilder(AddrUtil.getAddressMap("10.200.187.221:11211,10.200.187.221:11213"));
			// 设置failure模式
			builder.setFailureMode(true);
			builder.setSessionLocator(new KetamaMemcachedSessionLocator());
			builder.setConnectionPoolSize(5);
			client = builder.build();
		} catch (IOException e) {
			e.printStackTrace();
		}
		List<Map<String, String>> list = new ArrayList<Map<String,String>>(0);
		Map<String, String> map = new HashMap<String, String>(0);
		map.put("key1", "key1");
		list.add(map);
		map = new HashMap<String, String>(0);
		map.put("key2", "key2");
		list.add(map);
		map = new HashMap<String, String>(0);
		map.put("key3", "key3");
		list.add(map);
		client.set("a", 60*60*12, list);
		final MemcachedClient cl = client;
		new Thread(new Runnable() {
			public void run() {
				try {
					for (int i = 0; i < 1000; i++) {
						System.out.println(cl.get("a") + "  ---------------------------");
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}


故障模拟

[root@template memcached-1.2.8-repcached-2.2]# ps -ef|grep repcached
root     17608     1  0 11:44 ?        00:00:00 /usr/local/bin/repcached -p 11211 -v -d -u root
root     17610     1  0 11:44 ?        00:00:00 /usr/local/bin/repcached -p 11213 -x 127.0.0.1 -v -d -u root
root     17612 30408  0 11:44 pts/0    00:00:00 grep repcached
 
[root@template memcached-1.2.8-repcached-2.2]# kill -9 17608
[root@template memcached-1.2.8-repcached-2.2]# replication: close
replication: listen

#此时,master宕机,slave监听到master宕机,slave接替了master的位置,担任起了master的角色。

#注意,如果我们要启动11211这个端口就必须以slave的角色启动,即:/usr/local/bin/repcached -p 11211 -v -d -u root -x 127.0.0.1

#也就是说,没有固定的master、slave角色,两者的角色是可以互换的。

#如果slave宕机,也是使用slave的角色启动。

缺点

只支持两个memcached的复制。




你可能感兴趣的:(repcached实现memcached主备)