环境: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测试,测试流程如下:
[root@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.
[root@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.
[root@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.
[root@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.
模拟11212、11213端口的memcache死掉
[root@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
[root@localhost ~]# kill -9 5126
[root@localhost ~]# kill -9 5134
[root@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重启复活
[root@localhost ~]# memcached -m 1 -u root -d -l 192.168.1.118 -p 11212
[root@localhost ~]# memcached -m 1 -u root -d -l 192.168.1.118 -p 11213
[root@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种
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.51cto.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客户端官网。