Memcached实战之复制----基于repcached的主从
? ? ? ?由于 Memcached 自己沒有防止單點的措施,因為為了保障 Memcached 服務的高可用,我們需要借助外部的工具來實現高可用的功能。本文引入 Repcached 這個工具,通過使用該工具我們可以完成 Memcached 服務的主從功能。
? ? ? ?Repcached 它是由日本人開發的,用來實現 Memcached 復制功能的一個工具。它所構建的主從方案是一個單主單從的方案,不支持多主多從。但是,它的特點是,主從兩個節點可以互相讀寫,從而可以達到互相同步的效果。
? ? ? ?假設主節點壞掉,從節點會很快偵測到連接斷開,然后它會自動切換到監聽狀態( listen)從而成為主節點,并且等待新的從節點加入。
? ? ? ?假設原來掛掉的主節點恢復之后,我們只能人工手動以從節點的方式去啟動。原來的主節點并不能搶占成為新的主節點,除非新的主節點掛掉。這也就意味著,基于 Repcached 實現的 Memcached 主從,針對主節點并不具備搶占功能。
? ? ? ?假設從節點壞掉,主節點也會很快偵測到連接斷開,然后它就會重新切換到監聽狀態(listen),并且等待新的從節點加入。
? ? ? ?假設主從節點都掛掉,則數據就丟失了!因此,這是 Repcached 的一個短板,不過后期我們可以通過結合其它的工具來彌補這個缺點。
? ? ? ?OK,簡單介紹到這里。下面我們通過實驗來看,基于 Repcached 的 Memcached 主從架構是如何部署,以后如何測試和管理的。
? ??
| 1 2 3 4 5 6 7 | 環境: CentOS?6.5?x86_64位?采用最小化安裝,系統經過了基本優化 selinux?為關閉狀態,iptables?為無限制模式 主機名:nolinux 源碼包存放位置:/usr/local/src libevent版本:2.0.21 memcached版本:1.4.20 |
一、基礎環境準備
| 1 | [root@master?~]#?yum?-y?install?gcc?gcc-c++ |
二、Memcached安裝
1、安裝libevent
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | [root@master?~]#cd?/usr/local/src [root@master?src]#?wget?http://code.taobao.org/p/nolinux/src/memcached/src/libevent-2.0.21-stable.tar.gz?orig [root@master?src]#?tar?zxvf?libevent-2.0.21-stable.tar.gz [root@master?src]#?cd?libevent-2.0.21-stable [root@master?libevent-2.0.21-stable]#??./configure?--prefix=/usr [root@master?libevent-2.0.21-stable]#??make [root@master?libevent-2.0.21-stable]#??make?install [root@master?libevent-2.0.21-stable]#?ll?/usr/lib/libevent*??#?libevent安裝完后,會在/usr/lib目錄下出現如下內容 lrwxrwxrwx?1?root?root?21?Aug?11?13:49?/usr/lib/libevent-2.0.so.5?->?libevent-2.0.so.5.1.9? -rwxr-xr-x?1?root?root?968690?Aug?11?13:49?/usr/lib/libevent-2.0.so.5.1.9? -rw-r--r--?1?root?root?1571802?Aug?11?13:49?/usr/lib/libevent.a? lrwxrwxrwx?1?root?root?26?Aug?11?13:49?/usr/lib/libevent_core-2.0.so.5?->?libevent_core-2.0.so.5.1.9? -rwxr-xr-x?1?root?root?585225?Aug?11?13:49?/usr/lib/libevent_core-2.0.so.5.1.9? -rw-r--r--?1?root?root?978482?Aug?11?13:49?/usr/lib/libevent_core.a? -rwxr-xr-x?1?root?root?970?Aug?11?13:49?/usr/lib/libevent_core.la? lrwxrwxrwx?1?root?root?26?Aug?11?13:49?/usr/lib/libevent_core.so?->?libevent_core-2.0.so.5.1.9? lrwxrwxrwx?1?root?root?27?Aug?11?13:49?/usr/lib/libevent_extra-2.0.so.5?->?libevent_extra-2.0.so.5.1.9? -rwxr-xr-x?1?root?root?404852?Aug?11?13:49?/usr/lib/libevent_extra-2.0.so.5.1.9? -rw-r--r--?1?root?root?593392?Aug?11?13:49?/usr/lib/libevent_extra.a? -rwxr-xr-x?1?root?root?977?Aug?11?13:49?/usr/lib/libevent_extra.la? lrwxrwxrwx?1?root?root?27?Aug?11?13:49?/usr/lib/libevent_extra.so?->?libevent_extra-2.0.so.5.1.9? -rwxr-xr-x?1?root?root?935?Aug?11?13:49?/usr/lib/libevent.la? lrwxrwxrwx?1?root?root?30?Aug?11?13:49?/usr/lib/libevent_pthreads-2.0.so.5?->?libevent_pthreads-2.0.so.5.1.9? -rwxr-xr-x?1?root?root?18430?Aug?11?13:49?/usr/lib/libevent_pthreads-2.0.so.5.1.9? -rw-r--r--?1?root?root?18670?Aug?11?13:49?/usr/lib/libevent_pthreads.a? -rwxr-xr-x?1?root?root?998?Aug?11?13:49?/usr/lib/libevent_pthreads.la? lrwxrwxrwx?1?root?root?30?Aug?11?13:49?/usr/lib/libevent_pthreads.so?->?libevent_pthreads-2.0.so.5.1.9? lrwxrwxrwx?1?root?root?21?Aug?11?13:49?/usr/lib/libevent.so?->?libevent-2.0.so.5.1.9 [root@master?libevent-2.0.21-stable]#?cd?.. |
2、安裝memcached
| 1 2 3 4 5 6 7 8 9 | [root@master?src]#?tar?zxvf?memcached-1.4.20.tar.gz [root@master?src]#?cd?memcached-1.4.20 [root@master?memcached-1.4.20]#?./configure?--with-libevent=/usr [root@master?memcached-1.4.20]#?wget?http://code.taobao.org/p/nolinux/src/memcached/src/memcached-1.4.20.tar.gz?orig [root@master?memcached-1.4.20]#?make [root@master?memcached-1.4.20]#?make?install [root@master?memcached-1.4.20]#?cd?.. [root@master?src]#?ll?/usr/local/bin/memcached????#?安裝完成后會把memcached?放到?/usr/local/bin/memcached? -rwxr-xr-x?1?root?root?341907?Aug?11?13:52?/usr/local/bin/memcached |
注意:如果中間出現報錯,請仔細檢查錯誤信息,按照錯誤信息來配置或者增加相應的庫或者路徑
?
三、repcached安裝
方式一:使用repcached版本
| 1 2 3 | [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 |
方式二:使用patch版本
| 1 2 3 4 5 6 | [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
| 1 2 3 4 | [root@master?~]#?memcached?-v?-d?-p?11211?-l?192.168.0.102?-u?root?-P?/tmp/memcached1.pid? [root@master?~]#?replication:?listen? [root@master?~]#?replication:?accept |
2、啟動salve
| 1 2 3 4 5 6 | [root@slave?src]#?memcached?-v?-d?-p?11211?-l?192.168.0.103?-u?root?-x?192.168.0.102?-P?/tmp/memcached1.pid? [root@slave?src]#?replication:?connect?(peer=192.168.0.102:11212)? replication:?marugoto?copying? replication:?start? [root@slave?src]# |
3、回到master節點
| 1 | [root@master?~]#?replication:?accept??#?啟動正常后,master?將?accept |
五、測試
? ? ? ?由于我們主節點和從節點的memcached服務都騎起來了,并且監聽也都正常,所以以下的測試操作全部放到master節點進行。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | [root@master?~]#?telnet?192.168.0.102?11211????#?連接到我們主節點,添加一個記錄 Trying?192.168.0.102...? Connected?to?192.168.0.102.? Escape?character?is?'^]'.? set?key?0?0?6? sunsky? STORED? quit? Connection?closed?by?foreign?host.? [root@master?~]#?telnet?192.168.0.103?11211????#?連接到我們的從節點,查看主節點的記錄是否同步過來 Trying?192.168.0.103...? Connected?to?192.168.0.103.? Escape?character?is?'^]'.? get?key? VALUE?key?0?6? sunsky? END? quit? Connection?closed?by?foreign?host.? [root@master?~]#?pkill?memcached?????#?現在,殺掉我們主節點的memcached進程 replication:?cleanup?start? replication:?close? replication:?cleanup?complete [root@slave?src]#?replication:?close??#?備節點此時變為監聽狀態,即變成了主節點 replication:?listen [root@master?~]#?telnet?192.168.0.103?11211???#?查看從節點上面的數據是否還存在 Trying?192.168.0.103...? Connected?to?192.168.0.103.? Escape?character?is?'^]'.? get?key? VALUE?key?0?6? sunsky? END? quit? Connection?closed?by?foreign?host.? [root@master?~]#?memcached?-v?-d?-p?11211?-l?192.168.0.102?-u?root?-x?192.168.0.103?-P?/tmp/memcached.pid???#??由于memcached的主/從沒有搶占功能,因此主恢復之后,只能作為現有主節點的從節點[root@master?~]#?replication:?connect?(peer=192.168.0.103:11212)? replication:?marugoto?copying? replication:?start? [root@slave?src]#?replication:?accept????#?在上面加入之后,下面之前的從節點就會蹦出如下輸入,表示開啟同步 replication:?marugoto?start? replication:?marugoto?1? replication:?marugoto?owari [root@master?~]#?telnet?192.168.0.102?11211???#?我們連接到剛剛恢復的節點,可以看到數據又回來了 Trying?192.168.0.102...? Connected?to?192.168.0.102.? Escape?character?is?'^]'.? get?key? VALUE?key?0?6? sunsky? END? quit? Connection?closed?by?foreign?host. |
? ? ? ?以上就是我們做的關于memcached基于repcached的主從復制實驗了。通過實驗,我們可以看出,通過他我們實現了主從中任何一個宕機,都不會影響另外一臺機器上的數據。
在文章最后,我們再來總結以下基于 Repcached 的 Memcached 主從的優缺點:
| 1 2 3 4 5 | 優點: 1、能夠實現?cache?的冗余功能 2、主從之間可以互相讀寫 缺點: 1、盡可以一主一從,單對單 |
本文轉自 aaao 51CTO博客,原文鏈接:http://blog.51cto.com/nolinux/1544931,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Memcached实战之复制----基于repcached的主从的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 六:Dubbo与Zookeeper、Sp
- 下一篇: 开源分布式平台-mooon系统结构