關于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
資料完整複制過來。
[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
[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