memcache+magent的高可用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了memcache+magent的高可用相关的知识,希望对你有一定的参考价值。

memcache+magent的高可用


一、安装步骤:
  1、编译安装libevent:

wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz
tar zxvf libevent-1.4.9-stable.tar.gz
cd libevent-1.4.9-stable/
./configure --prefix=/usr
make && make install
cd ../


  2、编译安装Memcached:

wget http://danga.com/memcached/dist/memcached-1.2.6.tar.gz
tar zxvf memcached-1.2.6.tar.gz
cd memcached-1.2.6/
./configure --with-libevent=/usr
make && make install
cd ../


  3、编译安装magent:

mkdir magent
cd magent/
wget http://memagent.googlecode.com/files/magent-0.5.tar.gz(墙外下载)
tar zxvf magent-0.5.tar.gz
/sbin/ldconfig
sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
make
cp magent /usr/bin/magent
cd ../

报错处理:
make时报错

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解决:

vim ketama.h
在开头加入    
#ifndef SSIZE_MAX
#define SSIZE_MAX      32767
#endif再次make报错:

gcc: /usr/lib64/libevent.a: No such file or directory
gcc: /usr/lib64/libm.a: No such file or directory解决:

vi Makefile
找到 LIBS = /usr/lib64/libevent.a /usr/lib64/libm.a这一行
将libevent 的安装路径修改为/usr/local/lib/ libevent.a
将  
CFLAGS = -Wall -g -O2 -I/usr/local/include $(M64)
改为:    
CFLAGS = -lrt -Wall -g -O2 -I/usr/local/include $(M64)

特别注意的,如果没有lib64的话,可以自己mkdir一个,然后把需要的类库软链接到lib64

ln -s /usr/lib/x86_64-linux-gnu/libm.a /usr/lib64


  二、使用实例:

memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
memcached -m 1 -u root -d -l 127.0.0.1 -p 11213
magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 127.0.0.1:11211 -s 127.0.0.1:11212 -b 127.0.0.1:11213

  1、分别在11211、11212、11213端口启动3个Memcached进程,在12000端口开启magent代理程序;
  2、11211、11212端口为主Memcached,11213端口为备份Memcached;
  3、连接上12000的magent,set key1和set key2,根据哈希算法,key1被写入11212和11213端口的Memcached,key2被写入11212和11213端口的Memcached;
  4、当11211、11212端口的Memcached死掉,连接到12000端口的magent取数据,数据会从11213端口的Memcached取出;
  5、当11211、11212端口的Memcached重启复活,连接到12000端口,magent会从11211或11212端口的Memcached取数据,由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管11213端口的Memcached还有数据(此问题尚待改进)。


--------------------------------------------------------------------------------

  三、整个测试流程:

[[email protected] ~]# telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
stats
memcached agent v0.4
matrix 1 -> 127.0.0.1:11211, pool size 0
matrix 2 -> 127.0.0.1:11212, pool size 0
END
set key1 0 0 8
zhangyan
STORED
set key2 0 0 8
zhangyan
STORED
quit
Connection closed by foreign host.


[[email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
get key1
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.


[[email protected] ~]# telnet 127.0.0.1 11212
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
get key1
VALUE key1 0 8
zhangyan
END
get key2
END
quit
Connection closed by foreign host.


[[email protected] ~]# telnet 127.0.0.1 11213
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
get key1
VALUE key1 0 8
zhangyan
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.


模拟11211、11212端口的Memcached死掉
[[email protected] ~]# ps -ef | grep memcached
root      6589     1  0 01:25 ?        00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
root      6591     1  0 01:25 ?        00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
root      6593     1  0 01:25 ?        00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11213
root      6609  6509  0 01:44 pts/0    00:00:00 grep memcached
[[email protected] ~]# kill -9 6589
[[email protected] ~]# kill -9 6591
[[email protected] ~]# telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
get key1
VALUE key1 0 8
zhangyan
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.


模拟11211、11212端口的Memcached重启复活
[[email protected] ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
[[email protected] ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
[[email protected] ~]# telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
get key1
END
get key2
END

quit
Connection closed by foreign host.

查看memcached的数据用stats items

以上是关于memcache+magent的高可用的主要内容,如果未能解决你的问题,请参考以下文章

Memcached+keepalived+magent实现主从复制和高可用

Memcached + Magent + keepalived高可用集群

memcached+magent组成高可用

memcached+magent+keepalived实现高可用群集

Memcached+magent实现主从同步 +keepalived高可用群集

Magent+keepalived+Memcached缓存高可用群集