实战!轻量级memcached缓存代理twemproxy

本文内容脑图如下:
实战!轻量级memcached缓存代理twemproxy文章插图
概述twemproxy(nutcracker)是Twitter开源的轻量级memcached / redis代理服务器 , 本质就是一个重置管理工具 , 主要用于Redis和Memcached对管理的不足 , 其完成的最大功劳就是通过在减少相同缓存中我们将Twemproxy看成一个老大哥 , 背后携带着一群memcached / redis实例小弟 , 如此看来 , 其中程序上也只能使用memcached / redis的HA 。
本文先实践一波让twemproxy来进行一群memcached小弟时的工作情况 。
环境准备准备三台议员:
官员 操作系统 角色 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 -dtwemproxy部署
  • 安装m4工具
wget tar -zvxf m4-1.4.9.tar.gzcd m4-1.4.9./configuremakemake install
  • 安装autoconf工具
wget tar zxvf autoconf-2.69.tar.gzcd autoconf-2.69./configure --prefix=/usr/make && make install
  • 安装twemproxy代理
wget unzip master.zipmv twemproxy-master twemproxymv twemproxy /usr/local/cd /usr/local/cd twemproxy/autoreconf -fvi./configure --enable-debug=fullmakemake install
  • 查看twemproxy帮助
nutcracker -h[root@localhost ~]# nutcracker -hThis is nutcracker-0.4.1Usage: 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:22121hash: fnv1a_64distribution: ketamatimeout: 400backlog: 1024preconnect: trueauto_eject_hosts: trueserver_retry_timeout: 30000server_failure_limit: 3servers:- 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 nutcrackertcp00 0.0.0.0:222220.0.0.0:*LISTEN12737/nutcrackertcp00 192.168.199.79:221210.0.0.0:*LISTEN12737/nutcracker数据读/写测试
  • 首先通过twemproxy代理来写缓存
一连存入了6个钥匙
[root@localhost conf]# telnet localhost 22121Trying ::1...telnet: connect to address ::1: Connection refusedTrying 127.0.0.1...Connected to localhost.Escape character is '^]'.set key10 0 11STOREDset key2 0 0 12STOREDset key3 0 0 13STOREDset key4 0 0 14STOREDset key5 0 0 15STOREDset key6 0 0 16STORED
  • 查看发现所有缓存都写到了memcached2中
[root@localhost ~]# telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.get key1VALUE key1 0 11ENDget key2VALUE key2 0 12ENDget key3VALUE key3 0 13ENDget key4VALUE key4 0 14ENDget key5VALUE key5 0 15ENDget key6VALUE key6 0 16END
  • 接下来下一步memcached2
[root@localhost ~]# ps -aux | grepmemroot6340.00.0 3265881960 ?Ssl15:580:00 memcached -u root -p 11211 -m 64m -droot7040.00.0 112676984 pts/0S+16:010:00 grep --color=auto mem[root@localhost ~]# kill -9 634
  • 继续通过twemproxy代理来写缓存
[root@localhost conf]# telnet localhost 22121Trying ::1...telnet: connect to address ::1: Connection refusedTrying 127.0.0.1...Connected to localhost.Escape character is '^]'.set key9 0 0 19STORED[root@localhost conf]#
  • 此时去memcached1查看:
[root@localhost ~]# telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.get key9VALUE key9 0 19END我们发现memcached2插入后 , 缓存key9写到了memcached1中 , 而对于用户来说 , 由于是跟twemproxy代理交互 , 因此并不能感觉到放入memcached2实例的下线