輕量級 memcached快取代理 twemproxy實踐
在這個快取為王的時代…
本文內容腦圖如下:
文章共 533字,閱讀大約需要 2分鐘 !
概 述
ofollow,noindex"> twemproxy(nutcracker) 是 Twitter開源的輕量級 memcached / redis 代理伺服器,本質就是一個叢集管理工具,主要用來彌補 Redis和 Memcached對叢集管理的不足,其完成的最大功勞就是通過在後端減少同快取伺服器的連線數從而增加吞吐量。我們將 Twemproxy看成一個老大哥,背後 Carry著一群 memcached / redis例項小弟,如此看來,某一程式上也類似於 memcached / redis 的HA。
本文先實踐一波讓 twemproxy 來 Carry一群 memcached小弟時的工作情況。
注:本文首發於 My Personal Blog:CodeSheep·程式羊 ,歡迎光臨 小站
環境準備
準備三臺節點:
節點 | OS | 角色 |
---|---|---|
192.168.199.77 | CentOS 7.4 | 部署 memcached1例項 |
192.168.199.78 | CentOS 7.4 | 部署 memcached2例項 |
192.168.199.79 | CentOS 7.4 | 部署 twemproxy代理伺服器 |
memcached 部署
- 安裝
yum install memcached
- 作為後臺服務執行之
memcached -u root -p 11211 -m 64m -d
twemproxy 部署
- 安裝 m4工具
wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz tar -zvxf m4-1.4.9.tar.gz cd m4-1.4.9 ./configure make make install
- 安裝 autoconf 工具
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz tar zxvf autoconf-2.69.tar.gz cd autoconf-2.69 ./configure --prefix=/usr/ make && make install
- 安裝 twemproxy代理
wget https://github.com/twitter/twemproxy/archive/master.zip unzip master.zip mv twemproxy-master twemproxy mv twemproxy /usr/local/ cd /usr/local/ cd twemproxy/ autoreconf -fvi ./configure --enable-debug=full make make install
- 檢視 twemproxy幫助
nutcracker -h
[root@localhost ~]# nutcracker -h This is nutcracker-0.4.1 Usage: nutcracker [-?hVdDt] [-v verbosity level] [-o output file] [-c conf file] [-s stats port] [-a stats addr] [-i stats interval] [-p pid file] [-m mbuf size] Options: -h, --help: this help -V, --version: show version and exit -t, --test-conf: test configuration for syntax errors and exit -d, --daemonize: run as a daemon -D, --describe-stats: print stats description and exit -v, --verbose=N: set logging level (default: 5, min: 0, max: 11) -o, --output=S: set logging file (default: stderr) -c, --conf-file=S: set configuration file (default: conf/nutcracker.yml) -s, --stats-port=N: set stats monitoring port (default: 22222) -a, --stats-addr=S: set stats monitoring ip (default: 0.0.0.0) -i, --stats-interval=N : set stats aggregation interval in msec (default: 30000 msec) -p, --pid-file=S: set pid file (default: off) -m, --mbuf-size=N: set size of mbuf chunk in bytes (default: 16384 bytes)
- 準備 twemproxy配置檔案
vim /usr/local/twemproxy/conf/nutcracker.yml
修改配置檔案 nutcracker.yml
memcached: listen: 127.0.0.1:22121 hash: fnv1a_64 distribution: ketama timeout: 400 backlog: 1024 preconnect: true auto_eject_hosts: true server_retry_timeout: 30000 server_failure_limit: 3 servers: - 192.168.199.77:11211:1 - 192.168.199.78:11211:1
- 啟動 tewmproxy服務
nutcracker -d -c /usr/local/twemproxy/conf/nutcracker.yml
- 檢查啟動情況
[root@localhost ~]# netstat -nltp | grep nutcracker tcp00 0.0.0.0:222220.0.0.0:*LISTEN12737/nutcracker tcp00 192.168.199.79:221210.0.0.0:*LISTEN12737/nutcracker
資料讀/寫測試
- 首先通過 twemproxy代理來寫快取
一連存入了 6個key
[root@localhost conf]# telnet localhost 22121 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. set key10 0 1 1 STORED set key2 0 0 1 2 STORED set key3 0 0 1 3 STORED set key4 0 0 1 4 STORED set key5 0 0 1 5 STORED set key6 0 0 1 6 STORED
- 檢視發現所有快取都寫到了 memcached2中
[root@localhost ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. get key1 VALUE key1 0 1 1 END get key2 VALUE key2 0 1 2 END get key3 VALUE key3 0 1 3 END get key4 VALUE key4 0 1 4 END get key5 VALUE key5 0 1 5 END get key6 VALUE key6 0 1 6 END
- 接下來斷開 memcached2
[root@localhost ~]# ps -aux | grepmem root6340.00.0 3265881960 ?Ssl15:580:00 memcached -u root -p 11211 -m 64m -d root7040.00.0 112676984 pts/0S+16:010:00 grep --color=auto mem [root@localhost ~]# kill -9 634
- 繼續通過 twemproxy代理來寫快取
[root@localhost conf]# telnet localhost 22121 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. set key9 0 0 1 9 STORED [root@localhost conf]#
- 此時去memcached1檢視:
[root@localhost ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. get key9 VALUE key9 0 1 9 END
我們發現 memcached2斷開後,快取 key9寫到了 memcached1中,而對於使用者來說,由於是跟 twemproxy代理互動,因此並不能感覺到後端 memcached2例項的下線
- 我們再重新啟動 memcached2
然後再繼續通過代理寫資料:
[root@localhost conf]# telnet localhost 22121 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. set key10 0 0 1 x STORED set key11 0 0 1 y STORED
- 然後發現數據又寫到 memcached2中了
[root@localhost ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. get key10 VALUE key10 0 1 x END get key11 VALUE key11 0 1 y END
從上面這個實驗過程可以看出,一臺 memcached例項掛掉後,twemproxy 能自動移除之;而恢復後,twemproxy 能夠自動識別並重新加入到 memcached 組中重新使用
後 記
由於能力有限,若有錯誤或者不當之處,還請大家批評指正,一起學習交流!
- My Personal Blog:CodeSheep 程式羊
- 我的半年技術部落格之路
可 長按 或 掃描 下面的 小心心 來訂閱 CodeSheep ,獲取更多 務實、能看懂、可復現的 原創文 ↓↓↓
最後附上個人聯絡方式,歡迎大家交流學習:
個人部落格:www.codesheep.cn
簡 書: https://www.jianshu.com/u/d19536b0189b