memcache集群服务:memagent配置使用
2011-04-04 15:14:24| 分类: | 标签: |字号 订阅
安装magent到/usr/local/下
cd /usr/local mkdir magent cd magent/ wget tar zxvf magent-0.5.tar.gz /sbin/ldconfig sed -i "s/LIBS = -levent/LIBS = -levent -lm/g" Makefile makemagent命令参数:
-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, max keep alive connections for one memcached server, default is 20 -v verbose启动magent服务
magent -u root -n 4096 -l 127.0.0.1 -p 12000 -s 127.0.0.1:8086 -s 127.0.0.2:8086 -b 127.0.0.1:11213magent的hash算法
magent采用的是:Consistent Hashing原理,Consistent Hashing如下所示:首先求出memcached服务器(节点)的哈希值, 并将其配置到0~232的圆(continuum)上。 然后用同样的方法求出存储数据的键的哈希值,并映射到圆上。 然后从数据映射到的位置开始顺时针查找,将数据保存到找到的第一个服务器上。 如果超过232仍然找不到服务器,就会保存到第一台memcached服务器上。 从上图的状态中添加一台memcached服务器。余数分布式算法由于保存键的服务器会发生巨大变化 而影响缓存的命中率,但Consistent Hashing中,只有在continuum上增加服务器的地点逆时针方向的 第一台服务器上的键会受到影响。利用magent实现对memecache的分布式管理,搭建一套memcache集群服务
1、前端php对magent的访问跟对memcache访问相同,不需要做任何更改,对于插入的key,magent会把值散列到各个memcache服务上,只操作magent,不用关心后端处理 2、公司项目应用:南北各10台前端,南北各部署一套magent服务,主要是考虑电信网通的跨网访问 以北方为例,每个前端安装memcached服务(大内存机器可以启动多个服务),每个前端都安装magent服务,后端挂载全部机器的 memcached服务,启动参数:magent -p 12000 -s 127.0.0.1:8086 -s 127.0.0.2:8086 -s 127.0.0.3:8086.......-s 127.0.0.10:8086
,所有前端配置都是相同的,任何一个前端只需访问本地端口的magent,这样的memcache集群对应用带来很大便利.
比如项目的基本配置信息,早期策略只能在中控机生成配置文件,同步到各个前端,没有办法把配置信息放到缓存中,因为各个前端的memcache是不共享 的,一台机器缓存更新,其它机器是不更新的,用程序去控制更新,还是存在不稳定因素,而且随着服务增多,也不便于管理,部署了magent后,就可以解决 这个问题,任何一个前端更新数据=全局更新 这种部署还可以解决的应用:session共享 项目中多处已经实际应用,magent对memcache的均衡和稳定性都非常不错,推荐使用from:http://hi.baidu.com/zhizhesky/blog/item/277886b118cf1b5a082302f6.html
服务器A 服务器B 启动两个memcached进程,端口分别为11211和11212: 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 启动两个magent进程,端口分别为10000和11000: magent -u root -n 51200 -l 127.0.0.1 -p 10000 -s 127.0.0.1:11211 -b 127.0.0.1:11212 magent -u root -n 51200 -l 127.0.0.1 -p 11000 -s 127.0.0.1:11212 -b 127.0.0.1:11211 -s 为要写入的memcached, -b 为备份用的memcached。 说明:测试环境用magent和memached的不同端口来实现,在生产环境中可以将magent和memached作为一组放到两台服务器上。 也就是说通过magent能够写入两个memcached。 [root@odb ~]# telnet 127.0.0.1 10000 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. set key 0 0 8 <---在10000端口设置key的值 88888888 STORED quit Connection closed by foreign host. [root@odb ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. get key <---在11211端口获取key的值成功 VALUE key 0 8 88888888 END quit Connection closed by foreign host. [root@odb ~]# telnet 127.0.0.1 11212 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. get key <---在11212端口获取key的值成功 VALUE key 0 8 88888888 END quit Connection closed by foreign host. 高可用性测试: [root@odb ~]# ps aux |grep -v grep |grep memcached root 23455 0.0 0.0 5012 1796 ? Ss 09:22 0:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11212 root 24950 0.0 0.0 4120 1800 ? Ss 10:58 0:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11211 [root@odb ~]# ps aux |grep -v grep |grep 'magent -u' root 25919 0.0 0.0 2176 484 ? Ss 12:00 0:00 magent -u root -n 51200 -l 127.0.0.1 -p 10000 -s 127.0.0.1:11211 -b 127.0.0.1:11212 root 25925 0.0 0.0 3004 484 ? Ss 12:00 0:00 magent -u root -n 51200 -l 127.0.0.1 -p 11000 -s 127.0.0.1:11212 -b 127.0.0.1:11211 [root@odb ~]# telnet 127.0.0.1 10000 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. set stone 0 0 6 <---在10000端口设置stone的值 123456 STORED quit Connection closed by foreign host. [root@odb ~]# telnet 127.0.0.1 11000 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. set shidl 0 0 6 <---在11000端口设置shidl的值 666666 STORED get stone <---在11000端口获取stone的值成功 VALUE stone 0 6 123456 END incr stone 2 <---在11000端口修改stone的值成功 123458 get stone VALUE stone 0 6 <---在11000端口验证stone的值,证明上面的修改成功 123458 END get shidl <---在11000端口获取shidl的值成功 VALUE shidl 0 6 666666 END quit <---退出11000端口 Connection closed by foreign host. [root@odb ~]# telnet 127.0.0.1 10000 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. get stone <---在10000端口获取stone的值,已被修改 VALUE stone 0 6 123458 END get shidl <---在10000端口获取shidl的值成功 VALUE shidl 0 6 666666 END delete shidl <---在10000端口删除shidl DELETED get shidl <---在10000端口删除shidl生效 END quit Connection closed by foreign host. [root@odb ~]# telnet 127.0.0.1 11000 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. get shidl <---在11000端口验证删除shidl生效 END get stone <---在11000端口获取stone的值成功 VALUE stone 0 6 123458 END quit Connection closed by foreign host. Down机模拟测试: Down掉11211端口的memcached: [root@odb ~]# kill -9 24950 [root@odb ~]# telnet 127.0.0.1 10000 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. get stone <---在10000依然可以获取stone的值 VALUE stone 0 6 123458 END quit Connection closed by foreign host. [root@odb ~]# telnet 127.0.0.1 11000 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. get stone <---在11000依然可以获取stone的值 VALUE stone 0 6 123458 END quit Connection closed by foreign host. Down掉11000端口的magent: [root@odb ~]# kill -9 25925 [root@odb ~]# telnet 127.0.0.1 10000 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. get stone <---在10000依然可以获取stone的值 VALUE stone 0 6 123458 END quit Connection closed by foreign host
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
magent使用
一、Linux环境下安装magent:
1、安装libevent: wget http://monkey.org/~provos/libevent-1.4.13-stable.tar.gz tar -xzvf libevent-1.4.13-stable.tar.gz cd libevent-1.4.13-stable ./configure --prefix=/usr/local/libevent make make install |
wget http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz tar -xzvf memcached-1.4.4.tar.gz cd memcached-1.4.4 ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent make make install ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /usr/lib/ |
mkdir magent cp magent-0.5.tar.gz magent cd magent tar -xzvf magent-0.5.tar.gz /sbin/ldconfig sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile vi magent.c 添加 #include <limits.h> make |
memcached -m 1 -u root -d -l 192.168.1.219 -p 11211 memcached -m 1 -u root -d -l 192.168.1.219 -p 11212 memcached -m 1 -u root -d -l 192.168.1.219 -p 11213 magent -u root -n 51200 -l 192.168.1.219 -p 12000 -s 192.168.1.219:11211 -s 192.168.1.219:11212 -b 192.168.1.219:11213
|
# telnet 192.168.1.219 12000 Trying 1192.168.1.219... Connected to 192.168.1。219. Escape character is '^]'. stats memcached agent v0.4 matrix 1 -> 192.168.1.219:11211, pool size 0 matrix 2 -> 192.168.1.219:11212, pool size 0 END set key1 0 0 5 reesun STORED set key2 0 0 6 reesun1 STORED quit Connection closed by foreign host. # telnet 192.168.1.219 11211 Trying 192.168.1.219... Connected to 192.168.1.219. Escape character is '^]'. get key1 END get key2 VALUE key2 0 6 reesun1 END quit Connection closed by foreign host. # telnet 192.168.1.219 11212 Trying 192.168.1.219... Connected to 1192.168.1.219. Escape character is '^]'. get key1 VALUE key1 0 5 reesun END get key2 END quit Connection closed by foreign host. # telnet 192.168.1.219 11213 Trying 192.168.1.219... Connected to 1192.168.1.219. Escape character is '^]'. get key1 VALUE key1 0 5 reesun END get key2 VALUE key2 0 6 reesun1 END quit Connection closed by foreign host. |
@@@@@@@@@@@@@@@@@@@@@@@@@
文章分类:
- # mkdir magent
- # cp magent-0.6.tar.gz ./magent
- # cd magent
- # tar -zvxf magent-0.6.tar.gz
- # /sbin/ldconfig
- # sed -i "s/LIBS = -levent/LIBS = -levent -lm/g" Makefile
- # make
- # cp magent /usr/bin/magent
# cd /usr/bin
# magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 10.1.1.1:11211 -s 10.1.1.2:11211
-b 10.1.1.3:11211
- -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 do not go to background
- -k use ketama key allocation algorithm
- -f file, unix socket path to listen on. default is off
- -i number, max keep alive connections for one memcached server, default is 20
- -v verbose
- 或者
- # netstat -tln --看12000端口是否被监听