本文内容脑图以下:git
文章共 533字,阅读大约须要 2分钟 !github
twemproxy(nutcracker) 是 Twitter开源的轻量级 memcached / redis 代理服务器,本质就是一个集群管理工具,主要用来弥补 Redis和 Memcached对集群管理的不足,其完成的最大功劳就是经过在后端减小同缓存服务器的链接数从而增长吞吐量。咱们将 Twemproxy当作一个老大哥,背后 Carry着一群 memcached / redis实例小弟,如此看来,某一程序上也相似于 memcached / redis 的HA。redis
本文先实践一波让 twemproxy 来 Carry一群 memcached小弟时的工做状况。vim
注: 本文首发于 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代理服务器 |
yum install memcached
复制代码
memcached -u root -p 11211 -m 64m -d
复制代码
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
复制代码
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
复制代码
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
复制代码
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)
复制代码
vim /usr/local/twemproxy/conf/nutcracker.yml
复制代码
修改配置文件 nutcracker.ymlbash
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
复制代码
nutcracker -d -c /usr/local/twemproxy/conf/nutcracker.yml
复制代码
[root@localhost ~]# netstat -nltp | grep nutcracker
tcp 0 0 0.0.0.0:22222 0.0.0.0:* LISTEN 12737/nutcracker
tcp 0 0 192.168.199.79:22121 0.0.0.0:* LISTEN 12737/nutcracker
复制代码
一连存入了 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 key1 0 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
复制代码
[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
复制代码
[root@localhost ~]# ps -aux | grep mem
root 634 0.0 0.0 326588 1960 ? Ssl 15:58 0:00 memcached -u root -p 11211 -m 64m -d
root 704 0.0 0.0 112676 984 pts/0 S+ 16:01 0:00 grep --color=auto mem
[root@localhost ~]# kill -9 634
复制代码
[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]#
复制代码
[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实例的下线tcp
而后再继续经过代理写数据:memcached
[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
复制代码
[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 组中从新使用
因为能力有限,如有错误或者不当之处,还请你们批评指正,一块儿学习交流!
可 长按 或 扫描 下面的 当心心 来订阅做者公众号 CodeSheep,获取更多 务实、能看懂、可复现的 原创文 ↓↓↓