twemproxy优缺点介绍
优点
这是一个轻量级的Redis和memcached代理。使用它可以减少缓存服务器的连接数,并且利用它来作分片。这个代理的速度是相当快的,在网上查到会有20%的性能损耗,但上面的redis-benchmark做了测试,发现性能更快。后来找到英文原文,作者是说最差情况下,性能损耗不会多于20%。twemproxy用了pipeline. 首先redis是支持使用pipeline批处理的。twemproxy与每个redis服务器都会建立一个连接,每个连接实现了两个FIFO的队列,通 过这两个队列实现对redis的pipeline访问。将多个客户端的访问合并到一个连接,这样既减少了redis服务器的连接数,又提高了访问性能。
缺点
- 虽然可以动态移除节点,但该移除节点的数据就丢失了。
redis集群动态增加节点的时候,twemproxy不会对已有数据做重分布.maillist里面作者说这个需要自己写个脚本实现- 性能上的损耗
下载twemproxy
git clone https://github.com/twitter/twemproxy.git
运行测试autoreconf
[root@spg twemproxy]# autoreconf
configure.ac:8: error: Autoconf version 2.64 or higher is required
configure.ac:8: the top level
autom4te: /usr/bin/m4 failed with exit status: 63
aclocal: autom4te failed with exit status: 63
autoreconf: aclocal failed with exit status: 63
此处可能有多种情况
1:如上,则需要更新autoreconf
[root@spg twemproxy] wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
[root@spg twemproxy] tar zxvf autoconf-2.69.tar.gz
cd autoconf
[root@spg twemproxy]# ./configure --prefix=/usr
[root@spg twemproxy]# make
[root@spg twemproxy]# make install
2:提示未安装,则同样可按上面步骤安装
完成后继续运行autoreconf
可能提示如下错误:
A:未安装automake包错误
autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force -I m4
Can't exec "automake": No such file or directory at /usr/local/share/autoconf/Autom4te/FileUtils.pm line 326, <GEN2> line 7.
autoreconf: failed to run automake: No such file or directory
可直接yum安装
yum install automake
如果其他系统,则下载安装
automake-1.12.1.tar.gz 包下载地址:http://ftp.gnu.org/gnu/automake/
[root@spg twemproxy]# tar -xf automake-1.12.1.tar.gz
[root@spg twemproxy]# ./configure
[root@spg twemproxy]# make && make install
B:未安装libtool错误:
configure.ac:36: error: possibly undefined macro: AC_PROG_LIBTOOL
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
autoreconf: /usr/bin/autoconf failed with exit status: 1
同上,使用yum方式安装libtoo,或直接下载安装
libtool-2.2.4.tar.gz 包下载地址:http://ftp.gnu.org/gnu/libtool/
[root@spg twemproxy]# tar -xf libtool-2.2.4.tar.gz
[root@spg twemproxy]# ./configure
[root@spg twemproxy]# make && make install
完成后继续安装twemproxy:
[root@spg twemproxy]# CFLAGS="-ggdb3 -O0" autoreconf -fvi && ./configure --prefix=/usr/local/twemproxy --enable-debug=log
[root@spg twemproxy]# make
[root@spg twemproxy]# make install
添加twemproxy配置文件
[root@spg conf]# cd /usr/local/twemproxy/
[root@spg twemproxy]# ll
总用量 0
drwxr-xr-x. 2 root root 23 3月 21 22:50 sbin
drwxr-xr-x. 3 root root 16 3月 21 22:50 share
[root@spg twemproxy]# mkdir conf run
[root@spg twemproxy]# ll
总用量 0
drwxr-xr-x. 2 root root 6 3月 21 22:52 conf
drwxr-xr-x. 2 root root 6 3月 21 22:52 run
drwxr-xr-x. 2 root root 23 3月 21 22:50 sbin
drwxr-xr-x. 3 root root 16 3月 21 22:50 shar
[root@spg twemproxy]# cd conf
[root@spg twemproxy]# vim nutcracker.yml
添加如下内容:
redis:
listen: 0.0.0.0:22122 #使用哪个端口启动Twemproxy
hash: fnv1a_64 #key值hash算法,默认fnv1a_64
hash_tag: "{}"
distribution: ketama #分布算法 ketama一致性hash算法;modula非常简 random随机分布
auto_eject_hosts: false #摘除后端故障节点
timeout: 400 #代理与后端超时时间,毫秒
redis: true #是否是redis缓存,默认是false
server_failure_limit: 1 #故障多少次摘除
servers:
- 127.0.0.1:6380:1 server1
- 127.0.0.1:6381:1 server2
- 127.0.0.1:6382:1 server3
3.启动twemproxy (nutcracker)
启动twemproxy之前先进行配置测试,测试如下:
[root@spg sbin]# ./nutcracker -t
nutcracker: configuration file 'conf/nutcracker.yml' syntax is ok
如果syntax is ok,则说明成功,否则需要检查错误。
此时,则可以启动twemproxy
[root@spg sbin]# ./nutcracker -d -c /usr/local/twemproxy/conf/nutcracker.yml -p /usr/local/twemproxy/run/redisproxy.pid -o /usr/local/twemproxy/run/redisproxy.log
完成后查看线程是否已经启动成功
[root@spg twemproxy]# ps -ef|grep nutcracker
root 5096 1 0 22:52 ? 00:00:00 ./sbin/nutcracker -d -c /usr/local/twemproxy/conf/nutcracker.yml -p /usr/local/twemproxy/run/redisproxy.pid -o /usr/local/twemproxy/run/redisproxy.log
root 5099 3249 0 22:52 pts/0 00:00:00 grep --color=auto nutcracker
可以看到已经成功启动twemproxy,此时可以连接其进行redis操作,注意,此处只需要连接代理(即twemproxy)即可,连接过程如连接redis客户端一样。
[root@spg twemproxy]# redis-cli -p 22122
127.0.0.1:22122>
127.0.0.1:22122>
127.0.0.1:22122>
127.0.0.1:22122> set name spg
OK
127.0.0.1:22122> get name
"spg"
127.0.0.1:22122>
4.twemproxy配置文件说明
[root@spg twemproxy]# ./sbin/nutcracker --help
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 : 查看帮助文档,显示命令选项
-V, –version : 查看nutcracker版本
-t, –test-conf : 测试配置脚本的正确性
-d, –daemonize : 以守护进程运行
-D, –describe-stats : 打印状态描述
-v, –verbosity=N : 设置日志级别 (default: 5, min: 0, max: 11)
-o, –output=S : 设置日志输出路径,默认为标准错误输出 (default: stderr)
-c, –conf-file=S : 指定配置文件路径 (default: conf/nutcracker.yml)
-s, –stats-port=N : 设置状态监控端口,默认22222 (default: 22222)
-a, –stats-addr=S : 设置状态监控IP,默认0.0.0.0 (default: 0.0.0.0)
-i, –stats-interval=N : 设置状态聚合间隔 (default: 30000 msec)
-p, –pid-file=S : 指定进程pid文件路径,默认关闭 (default: off)
-m, –mbuf-size=N : 设置mbuf块大小,以bytes单位 (default: 16384 bytes)
参考:
http://record.blog.51cto.com/3300006/1599746
http://www.xuchanggang.cn/archives/993.html