关于session持久性连接,我们大多都使用memcached做为缓存服务器。但是众所周知memcached具有的是单点故障的风险。针对于这个问题上次公司更新代码的情况做一下简述:
凌晨开发执行了几条sql语句之后跟更新代码。在验证收款的时候测试遇到这样一个问题。就是当我们用户要登录的时候已经输入了验证码。百分百确认没有输错,但是就是连不上,过一会又好了。忙活了大半天。觉得是不是session的问题,原本是存在memcached的临时信息,但是memcached出现了问题。直接从后端数据库读取,又读取新的数据了。于是重新做一个memcached,验证就没有问题了。(但是我有一点想不明白就是加入说定义的增长因子不对导致,memcached不够存,按理说memcached应该是会满了自动清理的,除非增长因子过大,而且数据太高,但是就是这样解决了,想不明白思路希望大家指教)
所以我觉得memcached高可用性还是很有必要的,查看了文档之后构建了下面的环境:
一、环境说明(使用repcached对memcached值进行复制):
memcached主:192.168.63.129 centos64
memcached从:192.168.63.132 contos64
二、安装步骤:
1、一般基于事物的我们都会安装libevent:
[[email protected] libevent-1.4.14b-stable]# ./configure --prefix=/usr
[[email protected] libevent-1.4.14b-stable]# make && make install
2、安装memcached:
[[email protected] memcached-1.4.20]# ./configure --with-libevent=/usr
[[email protected] memcached-1.4.20]# make && make install
3、安装repcached:
[[email protected] src]
# wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
[[email protected] src]
# tar zxf memcached-1.2.8-repcached-2.2.tar.gz
[[email protected] src]
# cd memcached-1.2.8-repcached-2.2
[[email protected] memcached-1.2.8-repcached-2.2]
#
wget http://downloads.sourceforge.net/repcached/repcached-2.2-1.2.8.patch.gz
[[email protected] memcached-1.2.8-repcached-2.2]
# gzip -cd ../repcached-2.2-1.2.8.patch.gz | patch -p1
[[email protected] memcached-1.2.8-repcached-2.2]
# ./configure --enable-replication
[[email protected] memcached-1.2.8-repcached-2.2]
# make
[[email protected] memcached-1.2.8-repcached-2.2]
# make install
[[email protected] memcached-1.2.8-repcached-2.2]
# cd ..
#主和从的安装步骤是一样的,这里只在主节点操作:
四、启动设置主从:
1、启动master:
[[email protected] ~]# memcached -v -d -p 11211 -l 192.168.63.129 -u root -P /tmp/memcached1.pid
[[email protected] ~]# replication: listen
2、启动backup:
[[email protected] ~]# memcached -v -d -p 11211 -l 192.168.63.132 -u root -x 192.168.6
3.129 -P /tmp/memcached1.pid
[[email protected] ~]# replication: connect (peer=192.168.63.129:11212)
3、查看主是否连接成功:
[[email protected] ~]# replication: listen
replication: accept #accept已经是master
五、测试:
1、主节点插入数据:
[[email protected] ~]# telnet 192.168.63.129 11211
Trying 192.168.63.129...
Connected to 192.168.63.129.
Escape character is ‘^]‘.
set key 0 0 2
xi
STORED
quit
Connection closed by foreign host.
2、从节点查看:
[[email protected] ~]# telnet 192.168.63.132 11211
Trying 192.168.63.132...
Connected to 192.168.63.132.
Escape character is ‘^]‘.
get key
VALUE key 0 2
xi
END
3、断开主节点,从节点自动监听,然后上升为主节点。
[[email protected] ~]# ps -ef | grep memcached | awk ‘{print $2}‘ | xargs kill -9
kill 2222: No such process
从节点变成监听状态:
[[email protected] ~]# replication: close
replication: listen
4、登录节点测试:
[[email protected] ~]# telnet 192.168.63.132 11211
Trying 192.168.63.132...
Connected to 192.168.63.132.
Escape character is ‘^]‘.
get key
VALUE key 0 2
xi
END
数据完整复制过来。
5、原理主节点修复好之后,我们启动它。让他变成从节点*
[[email protected] ~]# memcached -v -d -p 11211 -l 192.168.63.129 -u root -x 192.168.63.132 -P /tmp/memcached.pid
[[email protected] ~]# replication: connect (peer=192.168.63.132:11212)
replication: marugoto copying
replication: start
现在129成了从节点。登录看一下数据复制过来了没有:
[[email protected] ~]# telnet 192.168.63.129 11211
Trying 192.168.63.129...
Connected to 192.168.63.129.
Escape character is ‘^]‘.
get key
VALUE key 0 2
xi
END
到这来可以看到数据又完整恢复了。可以看到这个是跟Mysql主从同步差不多一致的步骤。