Rootop 服务器运维与web架构

memcached+magent代理

| 暂无评论

之前说过通过repcached解决单点故障,还有一种方法是通过”代理”实现。这就是magent。
个人理解magent类似mysql proxy 。

最新版本        :http://code.google.com/p/memagent/
当前最新版本:http://memagent.googlecode.com/files/magent-0.6.tar.gz

系统信息:
memcached_s1      centos6.5_x64 ip:192.168.0.75
memcached_s2      centos6.5_x64 ip:192.168.0.76
memcached_bak   centos6.5_x64 ip:192.168.0.82
magent_proxy       centos6.5_x64 ip:192.168.0.77
selinux关闭,iptables关闭
memcached_s1、memcached_s2、memcached_bak配置安装memcached,过程略。
安装可参考:https://www.rootop.org/pages/1656.html

在magent_proxy上安装magent:

[root@localhost ~]# wget -c http://memagent.googlecode.com/files/magent-0.6.tar.gz
[root@localhost ~]# tar zxvf magent-0.6.tar.gz
[root@localhost ~]# mkdir magent
[root@localhost ~]# tar zxvf magent-0.6.tar.gz -C magent
[root@localhost ~]# cd magent

[root@localhost magent]# make
gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c
magent.c: 在函数‘writev_list’中:
magent.c:729: 错误:‘SSIZE_MAX’未声明(在此函数内第一次使用)
magent.c:729: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
magent.c:729: 错误:所在的函数内也只报告一次。)
make: *** [magent.o] 错误 1
在make时得到一个报错,解决方法:
修改 ketama.h 文件,在头部加入3行

#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif

继续make,又得到一个错误:
gcc: /usr/lib64/libm.a:没有那个文件或目录
解决方法:
[root@localhost magent]# yum install -y glibc-devel
[root@localhost magent]# cp /usr/lib64/libm.so /usr/lib64/libm.a

继续make,又得到一个错误:
(.text+0x439): undefined reference to `clock_gettime’
/usr/lib64/libevent.a(event.o): In function `event_base_new’:
(.text+0x6fa): undefined reference to `clock_gettime’
collect2: ld 返回 1
make: *** [magent] 错误 1

解决方法:
编辑Makefile

CFLAGS = -Wall -g -O2 -I/usr/local/include $(M64)
修改为
CFLAGS = -lrt -Wall -g -O2 -I/usr/local/include $(M64)

继续make
[root@localhost magent]# make
gcc -lrt -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o /usr/lib64/libevent.a /usr/lib64/libm.a
[root@localhost magent]# ll
总用量 336
-rw-rw-r–. 1 500 502 12822 4月 10 2010 ketama.c
-rw-rw-r–. 1 500 502 388 4月 30 09:26 ketama.h
-rw-r–r–. 1 root root 23616 4月 30 09:28 ketama.o
-rwxr-xr-x. 1 root root 121684 4月 30 09:35 magent
-rw-rw-r–. 1 500 502 54813 4月 15 2010 magent.c
-rw-r–r–. 1 root root 111544 4月 30 09:28 magent.o
-rw-rw-r–. 1 500 502 506 4月 30 09:35 Makefile
[root@localhost magent]# cp magent /usr/bin/
终于完成(至于这么多问题,是代码bug么)。

在三台memcached中启动memcached服务:
在magent中启动服务:
先看一下参数
[root@localhost ~]# magent
please provide -s “ip:port” argument

memcached agent v0.6 Build-Date: Apr 30 2014 09:28:13
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

[root@localhost ~]# magent -u root -n 65535 -p 11211 -s 192.168.0.75:11211 -s 192.168.0.76:11211 -b 192.168.0.82:11211
[root@localhost ~]# lsof -i:11211
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
magent 1832 root 3u IPv4 15131 0t0 TCP *:memcache (LISTEN)

测试:
连接magent,插入数据,根据算法数据会分别插入到s1、s2,全部写入到bak中,当s1和s2宕机时,数据从bak中查询。
C:\Users\Administrator>telnet 192.168.0.77 11211
stats
memcached agent v0.6
matrix 1 -> 192.168.0.75:11211, pool size 0
matrix 2 -> 192.168.0.76:11211, pool size 0
END
get key1
END
set key1 0 0 5
aaaaa
STORED
set key2 0 0 5
bbbbb
STORED

在s1上取数据(只能取一个):
get key2
VALUE key2 0 5
bbbbb
END
在s2上取数据(只能取一个):
get key1
VALUE key1 0 5
aaaaa
END

在bak上取数据(会取出所有):
get key1
VALUE key1 0 5
aaaaa
END
get key2
VALUE key2 0 5
bbbbb
END

在magent取数据(会取出所有):
get key1
VALUE key1 0 5
aaaaa
END
get key2
VALUE key2 0 5
bbbbb
END

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/2477.html

作者:Venus

服务器运维与性能优化

发表回复