memcached集群负载均衡

memcached是针对数据库的缓存软件 能有效降低数据库的负载
下面是何如将多个memcached做成集群负载均衡,让memcached变的更高可用

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

1
2
3
4
tar zxvf libevent-1.4.9-stable.tar.gz
cd libevent-1.4.9-stable/
./configure --prefix=/usr
make &amp;&amp; make install<span id="more-105"></span>

2、编译安装Memcached:

1
2
3
4
tar zxvf memcached-1.2.6.tar.gz
cd memcached-1.2.6/
./configure --with-libevent=/usr
make &amp;&amp; make install

3、编译安装magent:

1
2
3
4
5
6
7
mkdir magent
cd magent/
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

——————————————————————————–

二、实例:

1
2
3
4
memcached -m 1 -u root -d -l 10.1.94.100 -p 11211
memcached -m 1 -u root -d -l 10.1.94.101 -p 11211
memcached -m 1 -u root -d -l 10.1.94.102 -p 11211
magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 10.1.94.100:11211 -s 10.1.94.101:11211 -b 10.1.94.102:11211

1、分别在10.1.94.100 – 10.1.94.102 3台机器的11211端口启动3个Memcached进程,在12000端口开启magent代理程序;(以此类推可在不同机器上启动memcached)

2、10.1.94.100和101为主Memcached,10.1.94.102为备份Memcached;

3、连接上12000的magent,set key1和set key2,根据哈希算法,key1被写入10.1.94.100和10.1.94.102的Memcached,key2被写入
   10.1.94.101和10.1.94.102的Memcached;

4、当10.1.94.100和101的Memcached死掉,连接到12000端口的magent取数据,数据会从10.1.94.102的Memcached取出;

5、当10.1.94.100和101的Memcached重启复活,连接到12000端口,magent会从10.1.94.100或10.1.94.101的Memcached取数据,
   由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管10.1.94.102的Memcached还有数据(此问题尚待改进)。

 

 

三、整个测试流程:

先在magent中添加1个key 如下:

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
wangqi
STORED
set key2 0 0 8
wangqi
STORED
quit
Connection closed by foreign host.

然后在2个轮询的memcached中get刚才添加的key是否返回正确:

telnet 10.1.94.100 11211
Trying 10.1.94.100…
Connected to 10.1.94.100.
Escape character is ‘^]’.
get key1
END
get key2
VALUE key2 0 8
wangqi
END
quit
Connection closed by foreign host.
telnet 10.1.94.101 11211
Trying 10.1.94.101…
Connected to 10.1.94.101.
Escape character is ‘^]’.
get key1
VALUE key1 0 8
wangqi
END
get key2
END
quit
Connection closed by foreign host.
telnet 10.1.94.103 11211
Trying 10.1.94.103…
Connected to 10.1.94.103.
Escape character is ‘^]’.
get key1
VALUE key1 0 8
wangqi
END
get key2
VALUE key2 0 8
wangqi
END
quit
Connection closed by foreign host.
模拟10.1.94.100和101的Memcached死掉
直接kill进程然后:

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
wangqi
END
get key2
VALUE key2 0 8
wangqi
END
quit
Connection closed by foreign host.
模拟10.1.94.100和101的Memcached重启复活
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.
magent -u root(启动用户uid) -n 51200(最大连接数) -l 127.0.0.1(ip地址) -p 12000(监听端口) -s 127.0.0.1:11211(memcached的ip) -s 127.0.0.1:11212(memcached的ip) -b 127.0.0.1:11213(备份memcached的ip)

如果不需要轮询hash的话 只有一主一备就可以了 但是在主当掉后备份memcached接手 然后在主服务恢复后数据会根据主服务器走….

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值