twemproxy完整部署文档v1.0

 

一:基础信息
1:系统:CentOS_6.6_x64
2:twemproxy 管理端口:9999
3:A quick checklist:
Use newer version of gcc (older version of gcc has problems)
autoreconf -fvi && ./configure needs automake and libtool to be installed

二:安装依赖库
yum install libtool -y
wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtools-2.repo
yum install devtoolset-2-gcc devtoolset-2-binutils devtoolset-2-gcc-c++ -y
ln -s /opt/rh/devtoolset-2/root/usr/bin/* /usr/local/bin >> /dev/null 2>&1

wget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
tar zvfx autoconf-latest.tar.gz
cd autoconf-2.69/
./configure
make
make install
cd ..

三:安装twemproxy
git clone https://github.com/twitter/twemproxy.git
cd twemproxy
autoreconf -fvi
./configure --prefix=/usr/local/twemproxy \
--enable-debug=full
make
make install
mkdir -p /usr/local/twemproxy/{run,conf}
#cp conf/nutcracker.yml /usr/local/twemproxy/conf/

#添加管理脚本
vim /usr/local/twemproxy/twemproxy.sh
#--------------------------------------------------------------------------------------------------------------
#!/bin/bash
#date:2015-07-12
#created by zhangluya

#Source function library.
. /etc/rc.d/init.d/functions

#Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
exit 0
fi

INSTALL="/usr/local/twemproxy"
LOG="/data/logs/"
RETVAL=0
prog="nutcracker"

start(){
echo -n $"Starting $prog: "
#Test the config before start.
${INSTALL}/sbin/nutcracker -t -c ${INSTALL}/conf/nutcracker.yml
RETVAL=$?
if [ $RETVAL -ne 0 ] ; then
action "Config check fail......" /bin/false
exit 1
fi

${INSTALL}/sbin/nutcracker -d -c ${INSTALL}/conf/nutcracker.yml -s 9999 -p ${INSTALL}/run/twemproxy.pid -o ${LOG}/twemproxy.log
RETVAL=$?
[ $RETVAL -eq 0 ] && action "start twemproxy ok......" /bin/true
}

stop(){
echo -n $"Stopping $prog: "
kill -9 `cat ${INSTALL}/run/twemproxy.pid`
RETVAL=$?
if [ $RETVAL -eq 0 ] ; then
> ${INSTALL}/run/twemproxy.pid
action "stop twemproxy ok......" /bin/true
else
action "stop twemproxy fail......" /bin/false
fi
}

restart(){
stop
start
}

#See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
#--------------------------------------------------------------------------------------------------------------
chmod a+x twemproxy.sh
cd ..

四:配置twemproxy

vim /usr/local/twemproxy/conf/nutcracker.yml
#--------------------------------------------------------------------------------------------------------------
cache:
listen: 0.0.0.0:22122
hash: fnv1a_64
distribution: ketama
timeout: 400
backlog: 1024
preconnect: true
auto_eject_hosts: true
server_retry_timeout: 2000
server_failure_limit: 1
servers:
- 192.168.1.126:11212:1
- 192.168.1.126:11213:1
#--------------------------------------------------------------------------------------------------------------

添加开机启动:
echo "/usr/local/twemproxy/twemproxy.sh start" >> /etc/rc.local

 

配置详解:
cache:
listen: 127.0.0.1:22121 #nutcracker服务器池监听端口和地址
hash_tag: "{}" #"{}"表示计算hash值时 只取key中包含在{}里面的那部分来计算
timeout: 400 #超时时间(毫秒)
backlog: 1024 #The TCP backlog argument. Defaults to 512.
hash: fnv1a_64 #hash函数 支持crc16、crc32、md5等10多种
distribution: ketama #指定具体的hash算法
auto_eject_hosts: true #是否在节点无法响应的时候临时摘除结点
redis: true #是否是做redis的proxy
server_retry_timeout: 2000 #2000ms后 剔除的server才会被包含到哈希环中 这会导致原来分布到剔除server的key被分布到幸存的server
如果想要确保请求总是成功 则客户端需要进行重试 重试次数必须大于server_retyr_timeout.
server_failure_limit: 1 #在连续sever_failure_limit次失败后 backend redis server会被剔除
servers:
- 127.0.0.1:6379:1


参数详解:
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)

mbuf
mbuf可以配置 512-65k缺省16k --mbuf-size=N,并发连接数取决于mbuf大小;
mbuf小允许的连接数多 mbuf大则允许从内核socket buffer读写更多数据;
需要处理的并发连接多,则配512或1k。

redis优点:
这是一个轻量级的 Redis和memcached代理。使用它可以减少缓存服务器的连接数,并且利用它来作分片。
这个代理的速度是相当快的,网上查到会有20%的性 能损耗,但用redis-benchmark做了测试,发现性能
几乎是无损的,甚至有时更快。后来找到英文原文,作者是说最差情况下,性能损耗不会多 于20% 觉得这
个很了不起,按道理说,有了一层代理,怎么说都得折损一部分性能,但是他切能使得访问更快。看了源码
原来是用了pipeline. 首先redis是支持使用pipeline批处理的。twemproxy与每个redis服务器都会建立一个
连接,每个连接实现了两个FIFO的队列,通 过这两个队列实现对redis的pipeline访问。将多个客户端的访
问合并到一个连接,这样既减少了redis服务器的连接数,又提高了访问性能。

 

posted @ 2016-02-25 15:50  study-notes  阅读(195)  评论(0编辑  收藏  举报