﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-少年阿宾-随笔分类-twemproxy</title><link>http://www.blogjava.net/stevenjohn/category/54946.html</link><description>那些青春的岁月</description><language>zh-cn</language><lastBuildDate>Tue, 03 Nov 2015 11:52:28 GMT</lastBuildDate><pubDate>Tue, 03 Nov 2015 11:52:28 GMT</pubDate><ttl>60</ttl><item><title> Redis 代理服务Twemproxy </title><link>http://www.blogjava.net/stevenjohn/archive/2015/11/03/428041.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Tue, 03 Nov 2015 11:30:00 GMT</pubDate><guid>http://www.blogjava.net/stevenjohn/archive/2015/11/03/428041.html</guid><wfw:comment>http://www.blogjava.net/stevenjohn/comments/428041.html</wfw:comment><comments>http://www.blogjava.net/stevenjohn/archive/2015/11/03/428041.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/stevenjohn/comments/commentRss/428041.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/stevenjohn/services/trackbacks/428041.html</trackback:ping><description><![CDATA[<div><h2><span style="font-size:14px">1、twemproxy explore</span></h2> <p style="font-size:14px; font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px; margin:10px auto"> &nbsp; &nbsp; &nbsp;<span style="background-color:#ffffff">&nbsp;<span style="color:#333333; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px">当我们有大量 Redis 或 Memcached 的时候，通常只能通过客户端的一些数据分配算法（比如一致性哈希），</span><span style="color:#333333; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px">来实现集群存储的特性。</span><span style="color:#333333; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px">虽然</span><span style="color:#333333; font-size:14px; font-family:tahoma,宋体; line-height:22px">Redis  2.6版本已经发布Redis Cluster，但还不是很成熟适用正式生产环境。<span style="color:#333333; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px">&nbsp;Redis 的 Cluster 方案还没有正式推出之前，我们<span style="color:#333333; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px">通过  Proxy 的方式来实现集群<span style="color:#333333; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px">存储</span>。</span></span></span></span></p> <p style="font-size:14px; font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px; margin:10px auto"> </p> <p style="color:#333333; font-family:tahoma,宋体; font-size:14px; line-height:22px"> <span style="background-color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp;Twitter，世界最大的Redis集群之一部署在Twitter用于为用户提供时间轴数据。Twitter Open Source部门提供了Twemproxy。</span></p> <p style="font-size:14px; font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px; margin:10px auto"> <span style="background-color:#ffffff">&nbsp; &nbsp; &nbsp;Twemproxy,也叫nutcraker。是一个twtter开源的一个redis和memcache代理服务器。 redis作为一个高效的缓存服务器，非常具有应用价值。但是当使用比较多的时候，就希望可以通过某种方式 统一进</span>行管理。避免每个应用每个客户端管理连接的松散性。同时在一定程度上变得可以控制。</p> <p style="font-size:14px; font-family:tahoma,宋体; line-height:22px; color:#333333"> <span style="background-color:#efefef">&nbsp; &nbsp;</span><span style="background-color:#ffffff"> &nbsp; Twemproxy是一个快速的单线程代理程序，支持Memcached ASCII协议和更新的Redis协议：</span></p> <p style="font-size:14px; font-family:tahoma,宋体; line-height:22px; color:#333333"> <span style="background-color:#ffffff">&nbsp; &nbsp; &nbsp;它全部用C写成，使用Apache 2.0 License授权。项目在Linux上可以工作，而在OSX上无法编译，因为它依赖了epoll API.</span></p> <p style="font-size:14px; font-family:tahoma,宋体; line-height:22px; color:#333333"> <span style="background-color:#ffffff"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px; background-color:#f7f7f7">&nbsp; &nbsp; &nbsp; Twemproxy 通过引入一个代理层，可以将其后端的多台 Redis 或 Memcached 实例进行统一管理与分配，使应用程序只需要在  Twemproxy 上进行操作，而不用关心后面具体有多少个真实的 Redis 或 Memcached 存储。</span>&nbsp;</span></p> <p style="font-size:14px; color:#333333"><strong><span style="font-family:tahoma,宋体"><span style="line-height:22px">2、</span></span><span style="font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px">twemproxy特性：</span></strong></p> <ul style="font-size:14px; font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px; margin-left:2px"><ul style="list-style-type:disc; margin-left:10px"><li style="list-style:inherit"> <p style="margin:10px auto">支持失败节点自动删除</p> <ul style="list-style-type:disc; margin-left:10px"><li style="list-style:inherit">可以设置重新连接该节点的时间</li><li style="list-style:inherit">可以设置连接多少次之后删除该节点</li><li style="list-style:inherit">该方式适合作为cache存储</li></ul> </li><li style="list-style:inherit"> <p style="margin:10px auto">支持设置HashTag</p> <ul style="list-style-type:disc; margin-left:10px"><li style="list-style:inherit">通过HashTag可以自己设定将两个KEYhash到同一个实例上去。</li></ul> </li><li style="list-style:inherit"> <p style="margin:10px auto">减少与redis的直接连接数</p> <ul style="list-style-type:disc; margin-left:10px"><li style="list-style:inherit">保持与redis的长连接</li><li style="list-style:inherit">可设置代理与后台每个redis连接的数目</li></ul> </li><li style="list-style:inherit"> <p style="margin:10px auto">自动分片到后端多个redis实例上</p> <ul style="list-style-type:disc; margin-left:10px"><li style="list-style:inherit">多种hash算法：<span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25px">能够使用不同的策略和散列函数支持一致性hash。</span></li><li style="list-style:inherit">可以设置后端实例的权重</li></ul> </li><li style="list-style:inherit"> <p style="margin:10px auto">避免单点问题</p> <ul style="list-style-type:disc; margin-left:10px"><li style="list-style:inherit">可以平行部署多个代理层.client自动选择可用的一个</li></ul> </li><li style="list-style:inherit"> <p style="margin:10px auto">支持redis pipelining request</p> <p style="margin:10px auto"><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25px">&nbsp; &nbsp; &nbsp;支持请求的流式与批处理，降低来回的消耗</span><br /> </p> </li><li style="list-style:inherit"> <p style="margin:10px auto">支持状态监控</p> <ul style="list-style-type:disc; margin-left:10px"><li style="list-style:inherit">可设置状态监控ip和端口，访问ip和端口可以得到一个json格式的状态信息串</li><li style="list-style:inherit">可设置监控信息刷新间隔时间</li></ul> </li><li style="list-style:inherit"> <p style="margin:10px auto">高吞吐量</p> <ul style="list-style-type:disc; margin-left:10px"><li style="list-style:inherit">连接复用，内存复用。</li><li style="list-style:inherit">将多个连接请求，组成reids pipelining统一向redis请求。</li></ul> </li></ul></ul> <p style="font-size:14px; font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px; margin:10px auto"> &nbsp; &nbsp; &nbsp;另外可以修改redis的源代码，抽取出redis中的前半部分，作为一个中间代理层。最终都是通过linux下的epoll 事件机制提高并发效率，其中nutcraker本身也是使用epoll的事件机制。并且在性能测试上的表现非常出色。</p> <p style="font-size:14px; font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px; margin:10px auto"> </p> <h2><a name="t2"></a><span style="font-size:14px">3、twemproxy问题与不足</span></h2> <span style="font-size:14px; font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px; background-color:#ffffff"></span> <div id="cnblogs_post_body" style="font-size:14px; font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px; word-break:normal!important"> <span style="background-color:#ffffff"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px"><br /> </span></span></div> <span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px"><span style="line-height:25px">Twemproxy 由于其自身原理限制，有一些不足之处，如：&nbsp;</span></span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px" /> <ul style="font-size:14px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.200000762939453px; margin:0px 0px 1.5em; padding:0px"><li style="margin:0px 0px 0.25em 30px; padding:0px"><span style="background-color:#ffffff">不支持针对多个值的操作，比如取sets的子交并补等（MGET 和 DEL 除外）</span></li><li style="margin:0px 0px 0.25em 30px; padding:0px"><span style="background-color:#ffffff">不支持Redis的事务操作</span></li><li style="margin:0px 0px 0.25em 30px; padding:0px"><span style="background-color:#ffffff">出错提示还不够完善</span></li><li style="margin:0px 0px 0.25em 30px; padding:0px">也不支持select操作</li></ul> <p style="font-size:14px; font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px"> </p>  <div id="blog_post_info_block" style="margin-top:20px"> <div id="blog_post_info"> <div id="BlogPostCategory" style="font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px"> <h2><a name="t3"></a><span style="font-size:14px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.200000762939453px; background-color:#f7f7f7">4、安装与配置</span>&nbsp;</span></h2>  <div id="BlogPostCategory"><span style="font-size:14px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.200000762939453px; background-color:#f7f7f7">具体的安装步骤可用查看github：<a href="https://github.com/twitter/twemproxy" target="_blank">https://github.com/twitter/twemproxy</a></span></div> Twemproxy 的安装，主要命令如下：&nbsp;</div> <div id="BlogPostCategory"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif"></span><div>apt-get install automake &nbsp;<br />apt-get install libtool &nbsp;<br />git clone git://github.com/twitter/twemproxy.git &nbsp;<br />cd twemproxy &nbsp;<br />autoreconf -fvi &nbsp;<br />./configure --enable-debug=log &nbsp;<br />make &nbsp;<br />src/nutcracker -h</div> <div id="BlogPostCategory" style="font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px"> <span style="font-size:14px; font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.200000762939453px; background-color:#f7f7f7"><br /> </span></div> <span style="font-family:Verdana,Arial,Helvetica,sans-serif"><span style="line-height:25px">通过上面的命令就算安装好了，然后是具体的配置，下面是一个典型的配置&nbsp;</span></span></div> <div id="BlogPostCategory" style="font-family:Verdana,Arial,Helvetica,sans-serif; line-height:25px; font-size:14px"> <div>&nbsp;&nbsp;&nbsp; redis1: &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listen: 127.0.0.1:6379 #使用哪个端口启动Twemproxy &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; redis: true #是否是Redis的proxy &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hash: fnv1a_64 #指定具体的hash函数 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; distribution: ketama #具体的hash算法 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; auto_eject_hosts: true #是否在结点无法响应的时候临时摘除结点 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timeout: 400 #超时时间（毫秒） &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server_retry_timeout: 2000 #重试的时间（毫秒） &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server_failure_limit: 1 #结点故障多少次就算摘除掉 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; servers: #下面表示所有的Redis节点（IP:端口号:权重） &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 127.0.0.1:6380:1 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 127.0.0.1:6381:1 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 127.0.0.1:6382:1 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp;&nbsp; redis2: &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listen: 0.0.0.0:10000 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; redis: true &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hash: fnv1a_64 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; distribution: ketama &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; auto_eject_hosts: false &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timeout: 400 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; servers: &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 127.0.0.1:6379:1 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 127.0.0.1:6380:1 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 127.0.0.1:6381:1 &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - 127.0.0.1:6382:1&nbsp;</div><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px; background-color:#f7f7f7" /> <span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px; background-color:#f7f7f7">你可以同时开启多个 Twemproxy 实例，它们都可以进行读写，这样你的应用程序就可以完全避免所谓的单点故障。 <br /></span></div></div></div></div><br /><br /><div>http://blog.csdn.net/hguisu/article/details/9174459/</div><img src ="http://www.blogjava.net/stevenjohn/aggbug/428041.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/stevenjohn/" target="_blank">abin</a> 2015-11-03 19:30 <a href="http://www.blogjava.net/stevenjohn/archive/2015/11/03/428041.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>