repcached實現memcached主備

介紹java

repcached是日本人開發的基於Memcached的一個patch,實現Memcached的複製功能,它支持多個Memcached之間相互複製(雙向複製,主備都是可讀可寫的),能夠解決Memcached的容災問題。咱們主要是用做Memcached的主備。shell


安裝socket

repcached 的安裝很是簡單,首先肯定是否已經安裝:ide

yum install libevent-devel -ymemcached

以後就開始安裝repcached :oop

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用戶不須要ui

參數說明:this

[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)

一、啓動masterspa

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

二、啓動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參數指定。

三、檢測是否啓動成功

[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的複製。

相關文章
相關標籤/搜索