轻量级 memcached 缓存代理 twemproxy 实践

2018-11-19 06:35:01 +08:00
 hansonwang99

本文内容脑图如下:

文章共 533 字,阅读大约需要 2 分钟 !


概 述

twemproxy ( nutcracker ) 是 Twitter 开源的轻量级 memcached / redis 代理服务器,本质就是一个集群管理工具,主要用来弥补 Redis 和 Memcached 对集群管理的不足,其完成的最大功劳就是通过在后端减少同缓存服务器的连接数从而增加吞吐量。我们将 Twemproxy 看成一个老大哥,背后 Carry 着一群 memcached / redis 实例小弟,如此看来,某一程序上也类似于 memcached / redis 的 HA。

本文先实践一波让 twemproxy 来 Carry 一群 memcached 小弟时的工作情况。

注: 本文首发于 My Personal Blog:CodeSheep·程序羊,欢迎光临 小站


环境准备

准备三台节点:


memcached 部署

yum install memcached
memcached -u root -p 11211 -m 64m -d

twemproxy 部署

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
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
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
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)
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
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

数据读 /写测试

一连存入了 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
[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
[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
[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]# 
[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 实例的下线

然后再继续通过代理写数据:

[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
[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 组中重新使用


后 记

由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!



2615 次点击
所在节点    程序员
9 条回复
ChinaSyc
2018-11-19 09:00:38 +08:00
很有用的教程,谢谢楼主。
用于 Redis 也同样适用,很有帮助。
hansonwang99
2018-11-19 09:41:28 +08:00
@ChinaSyc 多谢支持
liuxu
2018-11-19 10:11:27 +08:00
你确定你要复制粘贴源 github 带--enable-debug=full 的编译配置么?

#ifdef NC_ASSERT_PANIC

#define ASSERT(_x) do { \
if (!(_x)) { \
nc_assert(#_x, __FILE__, __LINE__, 1); \
} \
} while (0)

void
nc_assert(const char *cond, const char *file, int line, int panic)
{
log_error("assert '%s' failed @ (%s, %d)", cond, file, line);
if (panic) {
nc_stacktrace(1);
abort();
}
}


我知道推广个人博客不易,可你这往“程序员”节点推广的太多了吧。。就我平时随便点击就看到你的有 3 贴以上了,翻一翻历史记录全是推广,先是简书,然后是你的个人站

v2ex 个人博客现在已经有正规新节点了:/go/blogger

liuxu:nutcracker-0.4.1$ dig +noall +answer www.codesheep.cn
www.codesheep.cn. 45 IN CNAME pages.coding.me.
pages.coding.me. 45 IN A 103.210.21.11
liuxu:nutcracker-0.4.1$ besttrace -q1 www.codesheep.cn
traceroute to www.codesheep.cn (103.210.21.11), 30 hops max, 60 byte packets
1 localhost (192.168.1.1) 0.49 ms * 局域网
2 123.117.16.1 21.61 ms AS4808 中国 北京 联通
3 *
4 *
5 124.65.194.117 10.98 ms AS4808 中国 北京 联通
6 219.158.112.26 36.26 ms AS4837 中国 广东 广州 联通
7 219.158.8.118 45.42 ms AS4837 中国 广东 广州 联通
8 219.158.96.209 72.92 ms AS4837 中国 广东 广州 联通
9 219.158.97.246 45.82 ms AS4837 中国 香港 联通
10 202.77.23.26 106.78 ms AS10099 中国 香港 联通
11 43.252.86.66 106.23 ms AS10099 中国 香港 联通
12 103.1.67.6 81.02 ms AS10099 中国 香港 联通
13 43.252.86.210 116.19 ms AS10099 中国 香港 联通
14 *
15 *
16 *
17 107.150.122.59 94.00 ms AS135377 中国 香港 zenlayer.com
18 *
19 *
20 *
21 *
22 *
23 *
24 *
25 *
26 *
27 *
28 *
29 *
30 103.210.21.11 2875.46 ms AS135377 中国 香港 zenlayer.com


说实在的,正规做站就应该备个案,香港线路天天炸,开你的博客主资源 TTFB 要 3 秒,整个页面 Load:1.3min

推广就推广,coding 连个 https 都舍不得给么,都 2018 年都快过完了,博客连个 https 都没有,羞不羞,还好意思贴出来
hansonwang99
2018-11-19 11:34:31 +08:00
@liuxu 多谢指教,打这么多字辛苦了
julyclyde
2018-11-19 14:20:27 +08:00
看到连 m4 和 autoconf 都自己编译,就看不下去了……
fredcc
2018-11-19 17:11:23 +08:00
proxy 的 ha 怎么做的
AstroProfundis
2018-11-19 17:37:14 +08:00
有编译 autoconf / m4 的功夫不如换个新点的系统
milzero
2018-11-19 18:42:39 +08:00
洋洋洒洒一大篇,有几个字不是翻译的文档呢,看看文档就完全能明白的东西,值得在抄一遍,这种只能增加互联网垃圾,增加检索难道.
xiaojinmaolove
2018-11-19 19:05:53 +08:00
@yilelu0509 又是个疯狗咬人系列,看过楼主的其他文章,还是比较务实的,就算是翻译官方文档又有什么问题呢,你觉得别人文章不行你自己写啊,我真服了。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/509092

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX