memcached是针对数据库的缓存软件 能有效降低数据库的负载
下面是何如将多个memcached做成集群负载均衡,让memcached变的更高可用
一、安装步骤:
1、编译安装libevent:
2、编译安装Memcached:
3、编译安装magent:
——————————————————————————–
二、实例:
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接手 然后在主服务恢复后数据会根据主服务器走….