1.简介
Twemproxy 也叫 nutcraker。是 Twtter 开源的一个 Redis 和 Memcache 代理服务器,主要用于管理 Redis 和 Memcached 集群,减少与Cache 服务器直接连接的数量。
Twemproxy特性:
- 轻量级、快速
- 保持长连接
- 减少了直接与缓存服务器连接的连接数量
- 使用 pipelining 处理请求和响应
- 支持代理到多台服务器上
- 同时支持多个服务器池
- 自动分片数据到多个服务器上
- 实现完整的 memcached 的 ASCII 和再分配协议
- 通过 yaml 文件配置服务器池
- 支持多个哈希模式,包括一致性哈希和分布
- 能够配置删除故障节点
- 可以通过端口监控状态
- 支持 linux, *bsd,os x 和 solaris
2.安装
安装的时候需要有一些依赖的包如下:automake、libtool、autoconf、twemproxy,并且也需要注意一下安装的顺序。(严格依次按以下的顺序解压文件,否则安装失败)
2.1卸载旧版本的autoconf
由于系统redhat6.3有自带autoconf, 需要的版本是2.6.4以上,所以需要卸载
yum remove autoconf
2.2安装包下载
卸载完成之后,下载以下的软件包automake、libtool、autoconf、twemproxy.
2.3安装autoconf-2.69.tar.gz
tar -xf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure
make && make install
2.4安装automake-1.12.1.tar.gz
tar -xfautomake-1.12.1.tar.gz
cdautomake-1.12.1
./configure
make&& make install
2.5安装libtool-2.2.4.tar.gz
tar -xflibtool-2.2.4.tar.gz
cdlibtool-2.2.4
./configure
make&& make install
2.6安装twemproxy-master.zip
unzip twemproxy-master.zip
cd twemproxy-master
autoreconf -ivf
./configure
make && make install
3.参数配置
打开twemproxy-master/conf/nutcracker.yml,参数主要在这里进行配置
参数详细说明:
- listen: The listening address and port (name:port or ip:port) for this server pool.
- hash: The name of the hash function. Possible values are:
· one_at_a_time
· md5
· crc16
· crc32 (crc32 implementation compatible with libmemcached)
· crc32a (correct crc32 implementation as per the spec)
· fnv1_64
· fnv1a_64
· fnv1_32
· fnv1a_32
· hsieh
· murmur
· jenkins
- hash_tag: A two character string that specifies the part of the key used for hashing. Eg “{}” or “$$”. Hash tag enable mapping different keys to the same server as long as the part of the key within the tag is the same.
- distribution: The key distribution mode. Possible values are:
· ketama
· modula
· random
- timeout: The timeout value in msec that we wait for to establish a connection to the server or receive a response from a server. By default, we wait indefinitely.
- backlog: The TCP backlog argument. Defaults to 512.
- preconnect: A boolean value that controls if nutcracker should preconnect to all the servers in this pool on process start. Defaults to false.
- redis: A boolean value that controls if a server pool speaks redis or memcached protocol. Defaults to false.
- server_connections: The maximum number of connections that can be opened to each server. By default, we open at most 1 server connection.
- auto_eject_hosts: A boolean value that controls if server should be ejected temporarily when it fails consecutively server_failure_limit times. See liveness recommendations for information. Defaults to false.
- server_retry_timeout: The timeout value in msec to wait for before retrying on a temporarily ejected server, when auto_eject_host is set to true. Defaults to 30000 msec.
- server_failure_limit: The number of consecutive failures on a server that would lead to it being temporarily ejected when auto_eject_host is set to true. Defaults to 2.
- servers: A list of server address, port and weight (name:port:weight or ip:port:weight) for this server pool.
- auto_eject_hosts:true,表明支持自动临时剔除不可用后端服务节点。
- server_retry_timeout: 10000,表明重试被剔除的服务节点是否恢复的时间间隔10秒。不宜设置的太小,否则会不断重试故障节点。
- server_failure_limit: 3, 如果后端服务节点连续访问失败3次就会被自动剔除。
4.启动配置
4.1启动参数说明
4.2常用参数
- -d 以守护进程的方式启动
- -o 可以log文件的路径
- -c 设置启动使用的配置文件路径
4.3启动命令
日志级别建议设置为6
nutcracker -d -c/opt/twemproxy-0.4.0/conf/nutcracker.yml -o /opt/logs/nutcracker/nutcracker.log-v 6
- -s 设置twemproxy的状态监控端口
4.4查看状态
默认可以通过telnet或nc命令查看twemproxy的状态,eg.
- -m 设置缓存块的大小,默认16k
在twemproxy中,所有请求和响应使用的内存都是在mbuf中分配的并且使用了零拷贝技术。大块的mbuf能够减少读请求或响应时的系统调用次数,但是当twemproxy处理大量并发连接时,每个连接将会使用一个大块的mbuf,这可能会成为一个问题,所以当处理大量并发客户端连接时,应该尽量把mbuf值设置的小一些,比如512字节。
5.启动redis
这样自动会代理的
重启
ps -ef | grep "nutcracker"
kill -9 进程号
nutcracker -d -c/opt/twemproxy-0.4.0/conf/nutcracker.yml -o /opt/logs/nutcracker/nutcracker.log-v 6
6.测试结论
测试环境单台Proxy节点,两台redis节点,结论如下:
1. 前端使用Twemproxy做代理,后端的redis数据能基本上根据key进行比较均衡的分布。
2. 如果需要新增一台redis的话,TwemProxy需要重启才能够生效
3. 如果后台redis节点挂掉后,如果程序中放置key,rehash到挂掉的节点后,会报连接错误,因为Twemproxy 能够自动摘除。恢复后,Twemproxy 能够自动识别、恢复并重新加入到 Redis 组中重新使用,后台redis恢复后,就可以正常使用了。
4. Reids挂掉后,redis中的数据是否丢失依据redis本身的策略配置。与Twemproxy基本无关。
5. 如同时部署多个 Twemproxy,配置文件一致(测试配置为distribution:ketama,modula),则可以从任意一个读取,都可以正确读取 key对应的值。
6. 多台 Twemproxy 配置一样,客户端分别连接多台 Twemproxy可以在一定条件下提高性能。根据 Server 数量,提高比例在 110-150%之间。
7. 如原来已经有 2 个节点 Redis,后续有增加 2 个 Redis,则数据分布计算与原来的 Redis 分布无关,现有数据如果需要分布均匀的话,需要人工单独处理。关于数据迁移,见下文。
8. 如果 Twemproxy 的后端节点数量发生变化,Twemproxy 相同算法的前提下,原来的数据必须重新处理分布,否则会存在找不到key值的情况。关于数据迁移见下文。
7.数据迁移
由于Twemproxy后端节点数量发生变化后,也就是redis扩容后,相同的算法,原来的数据必须重新处理,否则会存在找不到key值得情况,因此需要使用Redis 集群迁移工具 Redis-Migrate-Tool
安装
cd redis-migrate-tool
autoreconf -fvi
./configure
make
src/redis-migrate-tool –h
配置文件如
更详细的命令请参考:https://github.com/vipshop/redis-migrate-tool