您的位置:首页 > 数据库 > Memcache

magent + memcached 集群测试

2016-04-07 13:48 555 查看
memcached 介绍:
Memcached
是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据
库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon
)是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。

详细信息如下: http://www.runoob.com/memcached/memcached-get-data.html
环境部署:
本次安装需要用到4台虚拟机可以一台或者多台根据实验环境去做更改
192.168.3.150 安装 memcached 服务 +magent服务
192.168.3.152 安装 memcached 服务 +magent服务
192.168.3.153 安装 memcached 服务
192.168.3.154 安装 memcached 服务
创建软件安装目录
[root@localhost ~]# mkdir /home/softwares
时间同步
[root@localhost ~]# \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime -rf
echo '*/5 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn' >> /var/spool/cron/root
echo 'TIMEZONE="Asia/Shanghai"' > /etc/sysconfig/clock
/usr/sbin/ntpdate ntp.sjtu.edu.cn
关闭selinux
[root@localhost ~]# setenforce 0
关闭防火墙
[root@localhost ~]# service iptables stop

[root@localhost ~]# yum -y install libevent libevent-devel memcached

/etc/init.d/memcached start

[root@localhost ~]# memcached -u root -d -l 192.168.3.150 -p 11211 -m 1
[root@localhost ~]# ps -ef | grep memcached
498
2703 1 0 10:52 ? 00:00:00 memcached -d -p 11211 -u
memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid
root 2718 2646 0 10:53 pts/1 00:00:00 grep memcached
其中命令选项说明:
-d 是启动一个守护进程;
-m 是分配给Memcache使用的内存数量,单位是MB;
-u 是运行Memcache的用户;
-l 是监听的服务器IP地址,可以有多个地址;
-p 是设置Memcache监听的端口,,最好是1024以上的端口;
-c 是最大运行的并发连接数,默认是1024;
-P 是设置保存Memcache的pid文件。
其余的三台机器安装memcache的安装步骤和上面一样,自行安装
安装完成之后依次执行
memcached -u root -d -l 192.168.3.150 -p 11211 -m 1
memcached -u root -d -l 192.168.3.152 -p 11211 -m 1
memcached -u root -d -l 192.168.3.153 -p 11211 -m 1
memcached -u root -d -l 192.168.3.154 -p 11211 -m 1

安装:magent 在150 和151 上面安装
tar -zxvf magent-0.5.tar.gz -C /home/softwares/
在ketama.h
[root@localhost softwares]# vi ketama.h
里面添加如下内容不然报错
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
[root@localhost softwares]# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
[root@localhost softwares]# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
[root@localhost softwares]# make
gcc -Wall -O2 -g -c -o magent.o magent.c
gcc -Wall -O2 -g -c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm
[root@localhost softwares]# ls
ketama.c ketama.h ketama.o magent magent.c magent.o Makefile
[root@localhost softwares]# cp magent /usr/sbin/
另一台安装流程一样

启动magent
=========================================================================================
设置启动参数
-u 当然是启动账号
-n 是最大连接数,
-l 是magent监听的IP
-p 是magent监听的端口
-s IP:端口 -s 代表是主服务器 然后指定IP:端口的格式
-b IP:端口 -b 的意思是 备用服务器
==========================================================================================
格式如下
[root@localhost
softwares]# magent -u root -n 52100 -l 192.168.3.150 -p 12000 -s
192.168.3.152:11211 -s 192.168.3.150:11211 -b 192.168.3.153:11211 -b
192.168.3.154:11211
[root@localhost softwares]#
[root@localhost softwares]#
[root@localhost softwares]# ps -ef | grep mag
root
2890 1 0 12:51 ? 00:00:00 magent -u root -n 52100 -l
192.168.3.150 -p 12000 -s 192.168.3.152:11211 -s 192.168.3.150:11211 -b
192.168.3.153:11211 -b 192.168.3.154:11211
root 2892 2646 0 12:51 pts/1 00:00:00 grep mag
===============================3.152的机器================================================
[root@localhost
softwares]# magent -u root -n 52100 -l 192.168.3.152 -p 12000 -s
192.168.3.152:11211 -s 192.168.3.150:11211 -b 192.168.3.153:11211 -b
192.168.3.154:11211
[root@localhost softwares]# ps -ef | grep mag
root
2941 1 0 12:52 ? 00:00:00 magent -u root -n 52100 -l
192.168.3.152 -p 12000 -s 192.168.3.152:11211 -s 192.168.3.150:11211 -b
192.168.3.153:11211 -b 192.168.3.154:11211
root 2943 2684 0 12:52 pts/1 00:00:00 grep mag
测试:=================
[root@localhost softwares]# telnet 192.168.3.150 12000
Trying 192.168.3.150...
Connected to 192.168.3.150.
Escape character is '^]'.
set key1 0 0 1
1
STORED
set key2 0 0 1
2
STORED
set key3 0 0 1
3
STORED
set key4 0 0 1
5
STORED
set key5 0 0 1
1
STORED
get key1
VALUE key1 0 1
1
END
get key2
VALUE key2 0 1
2
END
get key3
VALUE key3 0 1
3
END
get key4
VALUE key4 0 1
5
END
get key5
VALUE key5 0 1
1
END
quit
Connection closed by foreign host.
================登录其他机器查看==============
[root@localhost softwares]# telnet 192.168.3.152 12000
Trying 192.168.3.152...
Connected to 192.168.3.152.
Escape character is '^]'.
set key1
UNSUPPORTED COMMAND
get key1
VALUE key1 0 1
1
END
get key2
VALUE key2 0 1
2
END
get key3
VALUE key3 0 1
3
END
get ket4
END
get key5
VALUE key5 0 1
1
END
quit
===================[root@localhost ~]# telnet 192.168.3.153 11211
Trying 192.168.3.153...
Connected to 192.168.3.153.
Escape character is '^]'.
get key1
END
get key2
VALUE key2 0 1
2
END
get key3
END
get key4
VALUE key4 0 1
5
END
get key5
END
quit
Connection closed by foreign host.
###由此看出magent + memcached 也是根据内部算法自行分配的
=================================================
重启其中一台机器后启动服务查看是否可以自动分发过来内存配置
==================总结=============================
通过以上测试可以得出结论:
1. 通过magent的连接池存放的值会分别存在magent代理的所有memcached上去。
2. 如果有一个memcached宕机通过magent代理方式还能取到值。
3.

如果memcached修复重启后通过magent代理方式取到的值就会为Null,这是由于memcache重启后里边的值随着memcache服务的
停止就消失了(因为在内存中),但是magent是通过key进行哈希计算分配到某台机器上的,memcache重启后会还从这台机器上取值,所有取到的
值就为空。

解决办法:
1. 在每次memcache宕机修复后可以写一个程序把集群中的其他memcache的所有信息全给拷贝到当前宕机修复后的memcache中。
2. 自己写代理,当从一个memcached服务上取到的值为Null时再去其他memcached上取值。

注意事项:
magent的调用方式同memcached一样,客户端可以不用改代码即可实现切换到magent模式下。
############################################################################################
安装及开启服务过程中经常遇到的问题:
在安装magent的过程中
执行到 make 的时候,
报错1:
gcc -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

解决方案:
vim ./ketama.h
#在开头加入
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif

附件:http://down.51cto.com/data/2367571
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息