repcached memcachedプライマリ・プロビジョニングの実現

6912 ワード

紹介する
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、マスターを起動する
/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を起動するには、/usr/local/bin/repcached-p 11211-v-d-u root-x 127.0.0.1というslaveの役割で起動する必要があります.
#つまり、master、slaveのキャラクターは固定されておらず、両者のキャラクターは交換可能です.
#slaveがダウンタイムした場合も、slaveのロールを使用して起動します.
欠点
2つのmemcachedのレプリケーションのみがサポートされています.