关于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:
[root@localhost libevent-1.4.14b-stable]# ./configure --prefix=/usr
[root@localhost libevent-1.4.14b-stable]# make && make install
2、安装memcached:
[root@localhost memcached-1.4.20]# ./configure --with-libevent=/usr
[root@localhost memcached-1.4.20]# make && make install
3、安装repcached:
[root@master src]
# wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
[root@master src]
# tar zxf memcached-1.2.8-repcached-2.2.tar.gz
[root@master src]
# cd memcached-1.2.8-repcached-2.2
[root@master memcached-1.2.8-repcached-2.2]
#
wget http://downloads.sourceforge.net/repcached/repcached-2.2-1.2.8.patch.gz
[root@master memcached-1.2.8-repcached-2.2]
# gzip -cd ../repcached-2.2-1.2.8.patch.gz | patch -p1
[root@master memcached-1.2.8-repcached-2.2]
# ./configure --enable-replication
[root@master memcached-1.2.8-repcached-2.2]
# make
[root@master memcached-1.2.8-repcached-2.2]
# make install
[root@master memcached-1.2.8-repcached-2.2]
# cd ..
#主和从的安装步骤是一样的,这里只在主节点操作:
四、启动设置主从:
1、启动master:
[root@localhost ~]# memcached -v -d -p 11211 -l 192.168.63.129 -u root -P /tmp/memcached1.pid
[root@localhost ~]# replication: listen
2、启动backup:
[root@mysql ~]# memcached -v -d -p 11211 -l 192.168.63.132 -u root -x 192.168.6
3.129 -P /tmp/memcached1.pid
[root@mysql ~]# replication: connect (peer=192.168.63.129:11212)
3、查看主是否连接成功:
[root@localhost ~]# replication: listen
replication: accept #accept已经是master
五、测试:
1、主节点插入数据:
[root@localhost ~]# 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、从节点查看:
[root@mysql ~]# 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、断开主节点,从节点自动监听,然后上升为主节点。
[root@localhost ~]# ps -ef | grep memcached | awk ‘{print $2}‘ | xargs kill -9
kill 2222: No such process
从节点变成监听状态:
[root@mysql ~]# replication: close
replication: listen
4、登录节点测试:
[root@mysql ~]# 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、原理主节点修复好之后,我们启动它。让他变成从节点*
[root@localhost ~]# memcached -v -d -p 11211 -l 192.168.63.129 -u root -x 192.168.63.132 -P /tmp/memcached.pid
[root@localhost ~]# replication: connect (peer=192.168.63.132:11212)
replication: marugoto copying
replication: start
现在129成了从节点。登录看一下数据复制过来了没有:
[root@localhost ~]# 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主从同步差不多一致的步骤。
本文出自 “小罗” 博客,请务必保留此出处http://xiaoluoge.blog.51cto.com/9141967/1595971
原文地址:http://xiaoluoge.blog.51cto.com/9141967/1595971