轻量级 memcached缓存代理 twemproxy实践
本文内容脑图如下:
文章共 533字,阅读大约需要 2分钟 !
概 述
twemproxy(nutcracker) 是 Twitter开源的轻量级 memcached / redis 代理服务器,本质就是一个集群管理工具,主要用来弥补 Redis和 Memcached对集群管理的不足,其完成的最大功劳就是通过在后端减少同缓存服务器的连接数从而增加吞吐量。我们将 Twemproxy看成一个老大哥,背后 Carry着一群 memcached / redis实例小弟,如此看来,某一程序上也类似于 memcached / redis 的HA。
本文先实践一波让 twemproxy 来 Carry一群 memcached小弟时的工作情况。
注: 本文首发于 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代理服务器 |
memcached 部署
- 安装
yum install memcached
- 作为后台服务运行之
memcached -u root -p 11211 -m 64m -d
twemproxy 部署
- 安装 m4工具
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
- 安装 autoconf 工具
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
- 安装 twemproxy代理
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
- 查看 twemproxy帮助
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)
- 准备 twemproxy配置文件
vim /usr/local/twemproxy/conf/nutcracker.yml
修改配置文件 nutcracker.yml
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
- 启动 tewmproxy服务
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
数据读/写测试
- 首先通过 twemproxy代理来写缓存
一连存入了 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
- 查看发现所有缓存都写到了 memcached2中
[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
- 接下来断开 memcached2
[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
- 继续通过 twemproxy代理来写缓存
[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]#
- 此时去memcached1查看:
[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实例的下线
- 我们再重新启动 memcached2
然后再继续通过代理写数据:
[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
- 然后发现数据又写到 memcached2中了
[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 组中重新使用
后 记
由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!
- My Personal Blog:CodeSheep 程序羊
- 我的半年技术博客之路

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Logback中如何自定义灵活的日志过滤规则
当我们需要对日志的打印要做一些范围的控制的时候,通常都是通过为各个Appender设置不同的Filter配置来实现。在Logback中自带了两个过滤器实现:ch.qos.logback.classic.filter.LevelFilter和ch.qos.logback.classic.filter.ThresholdFilter,用户可以根据需要来配置一些简单的过滤规则,下面先简单介绍一下这两个原生的基础过滤器。 ch.qos.logback.classic.filter.LevelFilter过滤器的作用是通过比较日志级别来控制日志输出。下面是一个只记录日志级别为ERROR的例子: <appender name="ERROR_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>logs/error.log</file>ds <filter class="ch.qos.logback.classic.filter.LevelFilter"> &l...
-
下一篇
webpack4配置详解之常用插件分享
前言 继上一次webpack的基础配置分享之后,本次将分享一些工作中项目常用的配置插件、也会包含一些自己了解过觉得不错的插件,如有分析不到位的,欢迎纠错,嗯,这些东西文档都有,大佬可绕过。 Wepack4之后废弃了很多的插件,这里只会讲解webpack4还支持的(包含4之前插件),已经废弃的将不再阐述。 上一次的分享之后,有部分掘金网友留言质疑:骗小白的赞、是否原创、是否是抄别人等等,当然也有很多的网友支持和鼓励,不管褒贬,苏南都非常的感谢,是你们让我认识到自己的不足与优劣。 大家的留言,让我想起了自己刚入门前端初期的心酸,基本靠自己自学,没有人带,遇到问题像无头的苍蝇,到处乱撞网上一顿搜索,百度不曾欺我,在点了一个又一个的广告之后,翻过十页八页之后终于找到了问题的解决方案。 执着于对前端的热爱,常常一个问题卡到深夜,初入前端的我曾一度感叹在编辑器写一些东西,在网页上就能跑,甚至感叹 js 写上一个 alert hello world,浏览器就会自动弹出一个窗口,感觉全世界都在向你招手,当时的兴奋是难以形容的,甚至幻想着未来有一天,可能有千万、亿万的用户,在用你写的东西。 这几天一直在...
相关文章
文章评论
共有0条评论来说两句吧...