Memcached 高可用方案 Repcached

Repcached说明
    Repcached
是一个单master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步。
如果 master down掉, slave侦测到连接断了,它会自动 listen而成为 master;而如果 slave坏掉, master也会侦测到连接断,它就会重新 listen等待新的 slave加入。 

Repcached 安装:
1:安装条件(root用户执行)

apt-get install libevent-dev
wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz

2:解压&进入目录编译

root@m2:~/nosql# tar zxvf 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 

注意:make的时候报错

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

需要修改 memcached.c 文件:

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

改成:

/* FreeBSD 4.x doesn't have IOV_MAX exposed. */
#ifndef IOV_MAX
# define IOV_MAX 1024
#endif

再make&&make install,编译之后退出root用户。

开启repcached:

启动master:[必须是11211端口]

1:
zhoujy@m2:~$ repcached -d -m 64 -p 11211 -u nobody -l 127.0.0.1 -v zhoujy@m2:~$ replication: listen
或则:
2:
zhoujy@m2:~$ repcached -d -m 64 -p 11211 -u nobody -l 127.0.0.1 -x 127.0.0.1 -X 11111 -v #(11211:master端口;11111:同步端口)
zhoujy@m2:~$ replication: connect (peer=127.0.0.1:11111)
replication: marugoto copying
replication: close
replication: listen
启动slave: -x:同行memcached的地址。
1:
zhoujy@m2:~$ repcached -d -m 64 -p 11212 -u nobody -l 127.0.0.1 -x 127.0.0.1 -v zhoujy@m2:~$ replication: connect (peer=127.0.0.1:11212) replication: marugoto copying
或则:
2:
zhoujy@m2:~$ repcached -d -m 64 -p 11212 -u nobody -l 127.0.0.1 -x 127.0.0.1 -X 11111 -v #(11212:slave端口;11111:同步端口)
zhoujy@m2:~$ replication: connect (peer=127.0.0.1:11111)
replication: marugoto copying
replication: accept
replication: start

复制:11211的数据复制到11212中
zhoujy@m2:~$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set a 0 0 3
abc
STORED
set aa 0 0 5
abcde
STORED
quit
Connection closed by foreign host.
zhoujy@m2:
~$ telnet 127.0.0.1 11212 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. get a VALUE a 0 3 abc END get aa VALUE aa 0 5 abcde END

反复制:11212 同步到了11211

set b 0 0 3
ABC
STORED
set bb 0 0 5
ABCDE
STORED

quit
Connection closed by foreign host.
zhoujy@m2:~$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get b
VALUE b 0 3
ABC
END
get bb
VALUE bb 0 5
ABCDE
END

主从相互删除等操作都相互影响:

zhoujy@m2:~$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
delete aa
DELETED
delete bb
DELETED
quit
Connection closed by foreign host.
zhoujy@m2:~$ telnet 127.0.0.1 11212
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get aa
END
get bb
END
delete a
DELETED
get a
END
quit
Connection closed by foreign host.
zhoujy@m2:~$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get a
END
get b
VALUE b 0 3
ABC
END

至此,上面的同步已经完成。

优点:
数据冗余
两台memcached都可以进行读写操作

缺点:
只支持单对单
只支持memcached 1.2.x版本
 
 
 
posted @ 2013-04-23 00:11  jyzhou  阅读(3484)  评论(0编辑  收藏  举报