1、编译安装libevent: wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz
wget http://danga.com/memcached/dist/memcached-1.2.6.tar.gz
mkdir magent
环境说明: centeros 5.4(虚拟机) IP:192.168.128.133 测试客户端:xp 先在133上面开5个memcached端口 依次执行 memcached -m 1 -u root -d -l 192.168.128.133 -p 10001 memcached -m 1 -u root -d -l 192.168.128.133 -p 10002 memcached -m 1 -u root -d -l 192.168.128.133 -p 10003 memcached -m 1 -u root -d -l 192.168.128.133 -p 10004 memcached -m 1 -u root -d -l 192.168.128.133 -p 10005 先看看是否启动监听端口成功 ps -ef | grep memcached 发现结果6行,最后一行是执行这个查看命令的 说明成功 接着启动magent 设置启动参数 -u当然是启动账号 -n是最大连接数,-l是magent监听的IP -p是magent监听的端口 后面的格式依次是 -s IP:端口 代表是主服务器 然后指定IP:端口的格式 -b IP:端口的意思是 备用服务器, 这里我设置了10001,10002,10003 为主Memcached服务器 10004,10005 为memcached备份服务器 magent -u root -n 51200 -l 192.168.128.133 -p 12000 -s 192.168.128.133:10001 -s 192.168.128.133:10002 -s 192.168.128.133:10003 -b 192.168.128.133:10004 -b 192.168.128.133:10005 执行命令后 可以先查看是否启动成功 ps -ef | grep magent 出现2行 OK,继续 在XP客户端上测试 直接连接magent 端口 12000 telnet 192.168.128.133 12000 执行stats
说明magent 里面已经负载了3个memcached 端口 接着我们继续set set key1 0 0 1
好了 依次设置了3个key ,这里说明下参数 <command name> <key> <flags> <exptime> <bytes>\r\n 详细的解释大家请看:http://blog.zol.com.cn/737/article_736087.html 既然设置了3个key,我们来读取看 执行命令get key1 get key2 get key3 依次返回了 到此,一个缓存写入已经存取的过程就完成了,但是我们要了解其中的过程呀,哈哈 然后我们直接登陆memcached的端口get数据看 它是怎么样保存的 先telnet 192.168.128.133 10001 执行stats STAT pid 7496
说明10001只缓存了1个key 我们看是存的哪一个 get key1 OK存的key1 再telnet 192.168.128.133 10002 直接 get key1 说明10002上缓存的key2啊, 这样不难看出10003上应该是缓存的key3了 试试 telnet 192.168.128.133 10003 然后 get key1 到此 知道了magent是根据自己的算法 把要缓存的内容依次分发到后端的memcached的,至于具体的算法就不知道了,我个人认为应该是依次分发的吧,当然此memcached存活的前提下,
知道了主服务器是分发负载的,不知道备用服务器是不是这样, 进telnet 192.168.128.133 10004 依次 get key1 发现 备用服务10004 并没有全部缓存3个key,就是之前的key1 key2 key3 再进 telnet 192.168.128.133 10005 get key1 VALUE key1 0 1
然后我把主服务器的10001 10002 10003的进程全部kill了 root 7496 1 0 21:01 ? 00:00:00 memcached -m 1 -u root -d -l 192.168.128.133 -p 10001 kill 7496 7498 7500 再在magent的端口上get key1
并不影响取值。 不过如果备用服务器也死了的话,就无力了, 到此测试结束咯
magent编译安装及常见错误安装magent到/usr/local/下
cd /usr/local mkdir magent cd magent/ wget http://memagent.googlecode.com/files/magent-0.6.tar.gz tar zxvf magent-0.6.tar.gz /sbin/ldconfig sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile make 错误1: gcc -lrt -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c magent.c: In function ‘writev_list’: magent.c:729: error: ‘SSIZE_MAX’ undeclared (first use in this function) magent.c:729: error: (Each undeclared identifier is reported only once magent.c:729: error: for each function it appears in.) make: *** [magent.o] Error 1 解决办法: vi ketama.h
在开头加入
#ifndef SSIZE_MAX # define SSIZE_MAX 32767 #endif 错误2: gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o ketama.o ketama.c gcc -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o /usr/lib64/libevent.a /usr/lib64/libm.a /usr/lib64/libevent.a(event.o): In function `gettime’: (.text+0×449): undefined reference to `clock_gettime’ /usr/lib64/libevent.a(event.o): In function `event_base_new’: (.text+0x72a): undefined reference to `clock_gettime’ collect2: ld returned 1 exit status make: *** [magent] Error 1 解决办法 vim Makefile
CFLAGS = -Wall -g -O2 -I/usr/local/include $(M64) 改为: CFLAGS = -lrt -Wall -g -O2 -I/usr/local/include $(M64)
[root@test magent]# make gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o ketama.o ketama.c gcc -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o /usr/lib64/libevent.a /usr/lib64/libm.a gcc: /usr/lib64/libm.a:没有那个文件或目录 make: *** [magent] 错误 1 解决办法 ln -s /usr/lib64/libm.so /usr/lib64/libm.a
注:有可能还会报错 gcc: /usr/lib64/libevent.a: 没有那个文件或目录 如果有,可执行 vi Makefile 找到 LIBS = /usr/lib64/libevent.a /usr/lib64/libm.a 修改 LIBS = /usr/libevent 的安装路径/libevent.a /usr/lib64/libm.a 例: LIBS = /usr/lib/libevent.a /usr/lib64/libm.a 查看magent是否安装成功 cd usr/bin/magent ./magent please provide -s "ip:port" argument
memcached agent v0.6 Build-Date: Oct 15 2012 16:12:30 Usage: -h this message -u uid -g gid -p port, default is 11211. (0 to disable tcp support) -s ip:port, set memcached server ip and port -b ip:port, set backup memcached server ip and port -l ip, local bind ip address, default is 0.0.0.0 -n number, set max connections, default is 4096 -D don't go to background -k use ketama key allocation algorithm -f file, unix socket path to listen on. default is off -i number, set max keep alive connections for one memcached server, default is 20 -v verbose MemCached 过期时间的设置: 过期时间有两种格式: 1. 相对时间:多长时间,给出过期的时间长度 2. 绝对时间:到期时间,给出过期的最后期限
服务端的处理 时间处理源代码【memcached.c】如下: #define REALTIME_MAXDELTA 60*60*24*30 // 定义30天的秒数 static rel_time_t realtime(const time_t exptime) { if (exptime == 0) return 0; if (exptime > REALTIME_MAXDELTA) { // 超过30天,是绝对时间 if (exptime <= process_started) // 小于进程启动日期 return (rel_time_t)1; // return (rel_time_t)(exptime - process_started); // 返回进程启动之后的时间差 } else { // 不超过30天,是相对时间 return (rel_time_t)(exptime + current_time); // exptime + (tvsec - process_started) } }
相对时间时,返回的值是:服务器当前时间之后的exptime - process_started秒 绝对时间时,返回的值是:服务器当前时间之后的(exptime -服务器当前时间) - process_started秒
可以看到,如果Client和Server时间不一致,使用绝对时间很容易导致缓存过期。 所以使用相对时间是比较安全的做法。 |
magent + memcached 集群测试
最新推荐文章于 2020-05-09 00:17:13 发布