memcached单点

一.Repcached (memcached同步补丁)

下载地址:http://sourceforge.net/projects/repcached/files/repcached/2.2.1-1.2.8/

版本号:memcached 1.2.8  repcached 2.2.1

1、repcached介绍

repcached是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步,如果 master坏掉, slave侦测到连接断了,它会自动 listen而成为 master;而如果 slave坏掉, master也会侦测到连接断,它就会重新 listen等待新的 slave加入

2、安装

在两台服务器上分别安装memcached服务,另注本文libevent的版本为:libevent-1.4.13,下载地址: http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz (安装省略)

repcached有两种方式:

方式一、下载对应的repcached版本

·········10········20········30········40········50········60········
1.
#wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
2.
#tar zxf memcached-1.2.8-repcached-2.2.tar.gz
3.
#cd memcached-1.2.8-repcached-2.2

方式二、下载对应patch版本

·········10········20········30········40········50········60········
1.
#wget http://downloads.sourceforge.net/repcached/repcached-2.2-1.2.8.patch.gz
2.
#gzip -cd ../repcached-2.2-1.2.8.patch.gz | patch -p1】
3.
#./configure –enable-replication
4.
# make
5.
# make install

3、启动:

启动master

·········10········20········30········40········50········60········
1.
# ./memcached -v -u root -d -l 127.0.0.1 -p 11211 -x 127.0.0.1 -X 11111   (11211:master端口;11111:同步端口)
2.
replication: connect (peer=127.0.0.1:11111)
3.
replication: accept
4.
replication: marugoto copying
5.
replication: start

启动salve

·········10········20········30········40········50········60········
1.
# ./memcached -v -u root -d -l 127.0.0.1 -p 11212 -x 127.0.0.1 -X 11111   (11212:slave端口;11111:同步端口)
2.
replication: connect (peer=127.0.0.1:11111)
3.
replication: marugoto copying
4.
replication: close
5.
replication: listen

4、测试:

操作master

·········10········20········30········40········50········60········
01.
[root@localhost bin]# telnet 127.1 11211
02.
Trying 127.0.0.1...
03.
Connected to localhost.localdomain (127.0.0.1)...
04.
Escape character is '^]'.
05.
get key1
06.
END
07.
set key1 0 0 2
08.
aa
09.
STORED
10.
quit
11.
Connection closed by foreign host.

查看slave

·········10········20········30········40········50········60········
01.
[root@localhost bin]# telnet 127.1 11212
02.
Trying 127.0.0.1...
03.
Connected to localhost.localdomain (127.0.0.1)...
04.
Escape character is '^]'.
05.
get key1
06.
VALUE key1 0 2
07.
aa
08.
END
09.
quit
10.
Connection closed by foreign host.

注意:如果master down机,slave接管并成为master,这时down机的master只能启用slave,他们之间互换角色,才能保持复制功能。换句话说,master没有抢占功能。而且,同步间隔时间未知。

优点:

  1. 数据冗余
  2. 两台memcached都可以进行读写操作

缺点:

  1. 只支持单对单
  2. 只支持memcached 1.2.x版本

二.Magent (memcached的代理)

原文地址:http://blog.s135.com/post/393/

magent是一款开源的Memcached代理服务器软件,其项目网址为:

http://code.google.com/p/memagent/

1、安装步骤:

a、编译安装libevent:

·········10········20········30········40········50········60········
1.
wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz
2.
tar zxvf libevent-1.4.9-stable.tar.gz
3.
cd libevent-1.4.9-stable/
4.
./configure --prefix=/usr
5.
make && make install
6.
cd ../

b、编译安装Memcached:

·········10········20········30········40········50········60········
1.
wget http://danga.com/memcached/dist/memcached-1.2.6.tar.gz
2.
tar zxvf memcached-1.2.6.tar.gz
3.
cd memcached-1.2.6/
4.
./configure --with-libevent=/usr
5.
make && make install
6.
cd ../

c、编译安装magent:

·········10········20········30········40········50········60········
01.
mkdir magent
02.
cd magent/
03.
wget http://memagent.googlecode.com/files/magent-0.5.tar.gz
04.
tar zxvf magent-0.5.tar.gz
05.
/sbin/ldconfig
06.
sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
07.
make
08.
cp magent /usr/bin/magent
09.
cd ../

2、使用实例:

·········10········20········30········40········50········60········
1.
memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
2.
memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
3.
memcached -m 1 -u root -d -l 127.0.0.1 -p 11213
4.
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还有数据(此问题尚待改进)。

3、整个测试流程:

·········10········20········30········40········50········60········
01.
[root@centos52 ~]# telnet 127.0.0.1 12000
02.
Trying 127.0.0.1...
03.
Connected to 127.0.0.1.
04.
Escape character is '^]'.
05.
stats
06.
memcached agent v0.4
07.
matrix 1 -> 127.0.0.1:11211, pool size 0
08.
matrix 2 -> 127.0.0.1:11212, pool size 0
09.
END
10.
set key1 0 0 8
11.
zhangyan
12.
STORED
13.
set key2 0 0 8
14.
zhangyan
15.
STORED
16.
quit
17.
Connection closed by foreign host.
18.
 
19.
[root@centos52 ~]# telnet 127.0.0.1 11211
20.
Trying 127.0.0.1...
21.
Connected to 127.0.0.1.
22.
Escape character is '^]'.
23.
get key1
24.
END
25.
get key2
26.
VALUE key2 0 8
27.
zhangyan
28.
END
29.
quit
30.
Connection closed by foreign host.
31.
 
32.
[root@centos52 ~]# telnet 127.0.0.1 11212
33.
Trying 127.0.0.1...
34.
Connected to 127.0.0.1.
35.
Escape character is '^]'.
36.
get key1
37.
VALUE key1 0 8
38.
zhangyan
39.
END
40.
get key2
41.
END
42.
quit
43.
Connection closed by foreign host.
44.
 
45.
[root@centos52 ~]# telnet 127.0.0.1 11213
46.
Trying 127.0.0.1...
47.
Connected to 127.0.0.1.
48.
Escape character is '^]'.
49.
get key1
50.
VALUE key1 0 8
51.
zhangyan
52.
END
53.
get key2
54.
VALUE key2 0 8
55.
zhangyan
56.
END
57.
quit
58.
Connection closed by foreign host.

模拟11211、11212端口的Memcached死掉

·········10········20········30········40········50········60········
01.
[root@centos52 ~]# ps -ef | grep memcached
02.
root      6589     1  0 01:25 ?        00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
03.
root      6591     1  0 01:25 ?        00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
04.
root      6593     1  0 01:25 ?        00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11213
05.
root      6609  6509  0 01:44 pts/0    00:00:00 grep memcached
06.
[root@centos52 ~]# kill -9 6589
07.
[root@centos52 ~]# kill -9 6591
08.
[root@centos52 ~]# telnet 127.0.0.1 12000
09.
Trying 127.0.0.1...
10.
Connected to 127.0.0.1.
11.
Escape character is '^]'.
12.
get key1
13.
VALUE key1 0 8
14.
zhangyan
15.
END
16.
get key2
17.
VALUE key2 0 8
18.
zhangyan
19.
END
20.
quit
21.
Connection closed by foreign host.

模拟11211、11212端口的Memcached重启复活

·········10········20········30········40········50········60········
01.
[root@centos52 ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
02.
[root@centos52 ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
03.
[root@centos52 ~]# telnet 127.0.0.1 12000
04.
Trying 127.0.0.1...
05.
Connected to 127.0.0.1.
06.
Escape character is '^]'.
07.
get key1
08.
END
09.
get key2
10.
END
11.
quit
12.
Connection closed by foreign host.

 

优点:

  1. 可以做多主或多从

缺点:

  1. 当master宕掉又恢复后,取不到数据。
    原文作者:memcached
    原文地址: https://www.cnblogs.com/sunwubin/p/3548943.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞