设为首页 收藏本站
查看: 1685|回复: 0

[经验分享] memcache集群 demo

[复制链接]

尚未签到

发表于 2017-4-15 07:23:54 | 显示全部楼层 |阅读模式
  环境:Memcache1.4.x+Magent0.6+CentOS5.x(32bit)+Maven3.x
  概述:本教程包含Memcache安装,Libevent安装,集群管理者Magent安装,Telnet测试,Java测试。
  1、Libevent安装
  由于Memcache依赖libevent,所以需要先安装libevent.
  采用yum安装:yum install libevent libevent-devel
  2、Memcache安装
  采用yum安装。
  直接用yum install memcached
  注:如果提示no package.
  那么需要补充一下Yum的仓库
  可以用以下的仓库:

# rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
  3、Magent安装
  这个不好用yum安装。
  到http://code.google.com/p/memagent/这里下载magent0.6.tar.gz
  3.1 拷贝到/usr/local目录下
  3.2 解压:tar zxvf magent-0.6.tar.gz 
  3.3 会产生4个文件
  ketama.c
  ketama.h
  magent.c
  Makefile
  3.4 输入如下命令:

/sbin/ldconfig
  3.5 输入如下命令:

sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
  3.6 输入如下命令:

Make
  4 配置memcache服务:

memcached -m 1 -u root -d -l 192.168.0.107 -p 11212
memcached -m 1 -u root -d -l 192.168.0.107 -p 11213
memcached -m 1 -u root -d -l 192.168.0.107 -p 11214
  5 启动magent服务:

./magent -u root -n 4096 -l 192.168.0.107 -p 12000 -s 192.168.0.107:11212 -s 192.168.0.107:11213 -b 192.168.1.118:11214
  其中,12000是magent的端口,11212、11213是memcached的主进程,11214是备用进程。
  6.用windows的telnet测试,测试流程如下:
[iyunv@localhost ~]# telnet 192.168.1.118 12000
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
stats
memcached agent v0.6
matrix 1 -> 192.168.1.118:11212, pool size 0
matrix 2 -> 192.168.1.118:11213, pool size 1
END
set key1 0 0 5
hello
STORED
set key2 0 0 5
world
STORED
quit
Connection closed by foreign host.
 
[iyunv@localhost ~]# telnet 192.168.1.118 11212
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
get key1
END
get key2
VALUE key2 0 5
world
END
quit
Connection closed by foreign host.
 
[iyunv@localhost ~]# telnet 192.168.1.118 11213
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
get key1
VALUE key1 0 5
hello
END
get key2
END
quit
Connection closed by foreign host.
 
[iyunv@localhost ~]# telnet 192.168.1.118 11214
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
get key1
VALUE key1 0 5
hello
END
get key2
VALUE key2 0 5
world
END
quit
Connection closed by foreign host.
 模拟1121211213端口的memcache死掉
[iyunv@localhost ~]# ps -ef | grep memcached
root      5126     1  0 19:14 ?        00:00:00 memcached -m 1 -u root -d -l 192.168.1.118 -p 11212
root      5134     1  0 19:14 ?        00:00:00 memcached -m 1 -u root -d -l 192.168.1.118 -p 11213
root      5142     1  0 19:14 ?        00:00:00 memcached -m 1 -u root -d -l 192.168.1.118 -p 11214
root      5976  5526  0 20:11 pts/1    00:00:00 grep memcached
[iyunv@localhost ~]# kill -9 5126
[iyunv@localhost ~]# kill -9 5134
[iyunv@localhost ~]# telnet 192.168.1.118 12000
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
get key1
VALUE key1 0 5
hello
END
get key2
VALUE key2 0 5
world
END
quit
Connection closed by foreign host.
 
模拟11212、11213端口的memcache重启复活
[iyunv@localhost ~]# memcached -m 1 -u root -d -l 192.168.1.118 -p 11212
[iyunv@localhost ~]# memcached -m 1 -u root -d -l 192.168.1.118 -p 11213
[iyunv@localhost ~]# telnet 192.168.1.118 12000
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
get key1
END
get key2
END
quit
Connection closed by foreign host.
  7. magent的语法较难理解,可以参考如下语法:

Storage Commands

Parameters :
<key> : the key of the data stored
<flags> : 32-bit unsigned integer that the server store with the data (provided by the user), and return along the data when the item is retrieved
<exptime> : expiration time in seconds, 0 mean no delay, if exptime is superior to 30 day, Memcached will use it as a UNIX timestamps for expiration
<bytes> : number of bytes in the data block
<cas unique> : unique 64-bit value of an existing entry (retrieved with gets command) to use with cas command
[noreply] : optional parameter that inform the server to not send the reply
These commands can return :
STORED to indicate success
NOT_STORED indicate that the data was not stored because condition for "add" or "replace" command wasn't met, or the item is in a delete queue
EXISTS indicate that the item you are trying to store with "cas" command has been modified since last fetch
NOT_FOUND indicate that the item did not exist or has been deleted 



Set

Store key/value pair in Memcached
Telnet command : set <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

set key 0 900 4
data
STORED
  8.Java客户端,常规的有3种



  •    memcached client for java

  •    spymemcached

  •    xmemcached
  9.yum源分为以下几种,RPMForge, EPEL, REMI, ATrpms, Webtatic,我用的是EPEL
  10.注意到:memcached client如果没办法直接下载Jar的话,可以连接到gitHub后,下载它的源码打开POM文件,看它的maven坐标是多少。
  ----------------------------------------------------------------------------------
  参考资料:
  https://daxingplay.me/study/centos/centos-yum-install-memcached.html    yum安装memcached
  http://www.liquidweb.com/kb/install-memcached-on-centos-5/  yum安装lib event
  http://ljh0721.iteye.com/blog/1812376  测试流程讲得非常详细。
  http://blog.elijaa.org/?post/2010/05/21/Memcached-telnet-command-summary  telnet memcached语法详解
  http://jony-hwong.iteye.com/blog/1174985 telnet memcached语法讲解,没有上面那个清晰。
  http://code.google.com/p/memagent/  agent官网,语法讲解
  http://freeloda.blog.iyunv.com/2033581/1260824 Centos5 yum EPEL源详解
  http://tecadmin.net/top-5-yum-repositories-for-centos-rhel-systems/  yum所有源讲解
  http://www.vpsyou.com/2010/04/24/centos5-optimization-under-memcache-installation.html libevent安装成功测试、memcached安装成功测试
  http://www.yourhowto.net/install-memcached-on-centos-5-and-6/  memcache安装后的一些配置,这个是我第一个参考的教程,不错。
  http://blog.csdn.net/hjm4702192/article/details/7894080 完整的用Java Memcache Client的例子。
  https://github.com/gwhalin/Memcached-Java-Client  Memcached客户端官网。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-364961-1-1.html 上篇帖子: hibernate 用memcache 下篇帖子: spring 整合memcache
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表