试用memcached高可用repcached

repcached说是高可用,实际上也不过只是memcached的复制而已。

repcached并不在ubuntu的更新源里面,要自己手动编译,比较原始的linux程序发布方法。去http://repcached.lab.klab.org/下载最新版本,我这时候拿到的是memcached-1.2.8-repcached-2.2.1.tar.gz,其官网上有特别说明“repcached - add data replication feature to memcached 1.2.x”。事实证明,这段话很关键,你用memcached的其他版本是不能用repcached的。

安装repcached之前先要安装相关的编译工具

1 apt-get install build-essential
2 apt-get install libevent-dev

libevent-dev是一定需要安装的,否则会编译不过,然后就是解包,configure,make

tar zxvf memcached-1.2.8-repcached-2.2.1.tar.gz
cd memcached-1.2.8-repcached-2.2.1
./configure --enable-replication  --program-transform-name=s/memcached/repcached/
make
make install

如果之前没有安装libevent-dev的话,一定编译不过,自然也不能install,如果安装了的话,也不一定能编译过,呵呵。编译错误提示如下

memcached.c: 在函数‘add_iov’中:
memcached.c:697:30: 错误: ‘IOV_MAX’未声明(在此函数内第一次使用)
memcached.c:697:30: 附注: 每个未声明的标识符在其出现的函数内只报告一次

到了这份上,不去改代码的话就真的不能继续了,找到memcached.c中的IOV_MAX宏定义,

......
  55 /* FreeBSD 4.x doesn't have IOV_MAX exposed. */
  56 #ifndef IOV_MAX
  57 #if defined(__FreeBSD__) || defined(__APPLE__)
  58 # define IOV_MAX 1024
  59 #endif
  60 #endif
......

在代码的58行附近找到了这个宏定义,看起来原先代码的意思是对freebsd和apple系统启用这个宏,其他情况不定义(可能是其他系统本身就已经定义了)。注释掉第57和59行,让IOV_MAX在任何情况下都有定义。再次编译,这次编译过了,但是有很多warning,类似下面这样

memcached.c:1128:9: 警告: 格式 ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat]
memcached.c:1135:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c:1139:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c:1140:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c:1141:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c:1142:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c:1143:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c:1144:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c:1145:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c:1146:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c:1147:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘size_t’ [-Wformat]
memcached.c:1150:9: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c:1155:9: 警告: 格式 ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘long int’ [-Wformat]
memcached.c: 在函数‘process_get_command’中:
memcached.c:1334:19: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]
memcached.c: 在函数‘do_add_delta’中:
memcached.c:1576:5: 警告: 格式 ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘uint64_t’ [-Wformat]

原因也很明显,代码的移植性并没有考虑到在多个多个不同的系统上uint64_t这个类型的定义,我的系统是ubuntu 64位。当然,还有其他类型不匹配的地方,如果你有代码洁癖的话,可以逐个去改,并不麻烦,如果像我一样只是想试用一下功能而已的话呢,就当warning不存在,继续用吧。 

 下面是启动repcached,同样,很多参数用默认

repcached -p 11211 -d -v
repcached -p 11212 -x localhost -d -v

第一个命令是启动master服务,第二个是启动slave服务,测试一下从master输入,从slave获取

telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
set hello 0 0 3
123
STORED
telnet localhost 11212
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get hello
VALUE hello 0 3
123
END

也可以测试删除或者其他什么的,不贴了,基本和单机差不多。下面模拟一下其中一个repcached挂了,假设是master挂了,用kill干掉master的进程,连接master的telnet立刻就提示连接被关闭,这时候去看slave上的数据

get hello
VALUE hello 0 3
123
END

数据依然在,这时候再去启动master似乎恢复不了shutdown之前的状况,这是因为我们在启动的时候没有指定复制端口号,用-X选项

repcached -p 11211 -X 11111 -d -v
repcached -p 11212 -x localhost -X 11111 -d -v

这时候repcached之间使用端口11111来通信,即使master挂了,还可以重启恢复

repcached -p 11211 -x localhost -X 11111 -d -v

这时候再去连master,原先的数据又回来了

get hello
VALUE hello 0 3
123
END

至此,repcached的试用结束,虽然在某些程度上repcached已经实现了某种程度上的高可用,但是这种实现并不足够。比如,repcached只支持1.2.x版本的memcached,如果想用的话,还要把高版本的memcached删掉;再比如,repcached只支持两个进程互相传数据,再启一个进程就不能复制数据。这些在试用中都不能完成

原文地址:https://www.cnblogs.com/valleylord/p/3500614.html