Repcached实现memcached复制

1、介绍
    repcached是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的master/slave都是可读写的,而且可以相互同步,如果 master坏掉, slave侦测到连接断了,它会自动 listen而成为 master;而如果 slave坏掉, master也会侦测到连接断,它就会重新 listen等待新的 slave加入 
    repcached的安装有两种方式,一种是直接对应的repcached版本,其包含repcached和memcached两个包。直接安装该版本, 即完成了repcached和memcached的安装,不用再独立安装memcached。另一种是一个独立的repcached补丁包,将其作为 memcached的补丁,需要单独安装memcached,然后再为其打上该补丁。在多数情况下,都采用第一种安装方法。
 
2、实验环境:
两台测试机,ip分别为192.168.1.105和192.168.1.106,在其上安装repcached,并设置两台服务器互为主从。
 
使用整合了repcached和memcached的版本安装:
安装libevent:
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar zxf libevent-2.0.21-stable.tar.gz cd libevent-2.0.21-stable ./configure --prefix=/usr/local/libevent && make && make install

安装repcached:

wget https://github.com/usecide/repcached/archive/master.zip

unzip memcached-repcached.zip

cd memcached-repcached

./autogensh

./configure --prefix=/usr/local/repcached --enable-64bit --enable-replication --with-libevent=/usr/local/libevent

make && make install

 

使用repcached补丁包安装的方法:
下载补丁包,目前补丁包最高对应的memcached是1.2.8
wget http://sourceforge.net/projects/repcached/files/repcached/2.2.1-1.2.8/repcached-2.2.1-1.2.8.patch.gz/download

下载对应版本的memcached

wget https://memcached.googlecode.com/files/memcached-1.2.8.tar.gz



tar xf memcached-1.2.8.tar.gz

gzip -d repcached-2.2.1-1.2.8.patch.gz

cd memcached-1.2.8

patch -p1 < ../repcached-2.2.1-1.2.8.patch

./configure --prefix=/usr/local/memcached --enable-64bit --enable-replication --with-libevent=/usr/local/libevent

make && make install

如果在编译时出现如下错误:

memcached.c: Infunction ‘add_iov’:

memcached.c:697:error: ‘IOV_MAX’ undeclared (first use in this function)

memcached.c:697:error: (Each undeclared identifier is reported only once

memcached.c:697:error: for each function it appears in.)

make[2]: ***[memcached-memcached.o] Error 1

make[2]: Leavingdirectory `/root/memcached/memcached-1.2.8-repcached-2.2.1'

make[1]: ***[all-recursive] Error 1

make[1]: Leavingdirectory `/root/memcached/memcached-1.2.8-repcached-2.2.1'

make: *** [all]Error 2

则需要手动修改源文件memcached.c

将

 55 /* FreeBSD 4.x doesn't have IOV_MAXexposed. */

 56 #ifndef IOV_MAX

 57 #if defined(__FreeBSD__) ||defined(__APPLE__)

 58 # define IOV_MAX 1024

 59 #endif

 60 #endif

修改为:

 55 /* FreeBSD 4.x doesn't have IOV_MAXexposed. */

 56 #ifndef IOV_MAX

 57 /*#if defined(__FreeBSD__) ||defined(__APPLE__)*/

 58 # define IOV_MAX 1024

 59 #endif

 60 /*#endif*/
启动:
分别在两台机上启动repcached如下:
105:
/usr/local/repcached/bin/memcached -u memcached -l 192.168.1.105 -p 11211 -m 64 -x 192.168.1.106 -X 11212 -d

106:

/usr/local/repcached/bin/memcached -u memcached -l 192.168.1.106 -p 11211 -m 64 -x 192.168.1.105 -X 11212 -d
参数简单说明:
-u:指定启动memcached的用户,该用户需要事先创建

-l:指定memcached监听的本地ip地址

-p:指定memcached监听的本地的tcp端口,默认为11211

-m:指定memcached启动后占用的内存空间大小,单位为M

-x:指定repcached的master的ip地址,需要从master同步数据

-X:指定从master同步数据时的端口,默认为11212

-d:将其作为守护进程放入后台运行
测试:
在105上进行如下操作:
telnet 192.168.1.105 11211

set key1 0 60 5

hello

在106上查看:

telnet 192.168.1.106 11211

get key1

当看到显示

VALUE key1 0 5

hello

即代表数据成功实现同步,反之测试亦然。

 

你可能感兴趣的:(memcached)