由于 Memcached 自己没有防止单点的措施,因为为了保障 Memcached 服务的高可用,我们需要借助外部的工具来实现高可用的功能。本文引入 Repcached 这个工具,通过使用该工具我们可以完成 Memcached 服务的主从功能。

Repcached 它是由日本人开发的,用来实现 Memcached 复制功能的一个工具。它所构建的主从方案是一个单主单从的方案,不支持多主多从。但是,它的特点是,主从两个节点可以互相读写,从而可以达到互相同步的效果。

假设主节点坏掉,从节点会很快侦测到连接断开,然后它会自动切换到监听状态( listen)从而成为主节点,并且等待新的从节点加入。

假设原来挂掉的主节点恢复之后,我们只能人工手动以从节点的方式去启动。原来的主节点并不能抢占成为新的主节点,除非新的主节点挂掉。这也就意味着,基于 Repcached 实现的 Memcached 主从,针对主节点并不具备抢占功能。

假设从节点坏掉,主节点也会很快侦测到连接断开,然后它就会重新切换到监听状态(listen),并且等待新的从节点加入。

假设主从节点都挂掉,则数据就丢失了!因此,这是 Repcached 的一个短板,不过后期我们可以通过结合其它的工具来弥补这个缺点。

OK,简单介绍到这里。下面我们通过实验来看,基于 Repcached 的 Memcached 主从架构是如何部署,以后如何测试和管理的。

1
2
3
4
5
6
7
环境:
CentOS 6.5 x86_64位 采用最小化安装,系统经过了基本优化
selinux 为关闭状态,iptables 为无限制模式
主机名:nolinux
源码包存放位置:/usr/local/src
libevent版本:2.0.21
memcached版本:1.4.20

一、基础环境准备

1
[root@master ~]# yum -y install gcc gcc-c++

二、Memcached安装

1、安装libevent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@master ~]#cd /usr/local/src
[root@master src]# wget http://code.taobao.org/p/nolinux/src/memcached/src/libevent-2.0.21-stable.tar.gz?orig
[root@master src]# tar zxvf libevent-2.0.21-stable.tar.gz
[root@master src]# cd libevent-2.0.21-stable
[root@master libevent-2.0.21-stable]#  ./configure --prefix=/usr
[root@master libevent-2.0.21-stable]#  make
[root@master libevent-2.0.21-stable]#  make install
[root@master libevent-2.0.21-stable]# ll /usr/lib/libevent*  # libevent安装完后,会在/usr/lib目录下出现如下内容
lrwxrwxrwx 1 root root 21 Aug 11 13:49 /usr/lib/libevent-2.0.so.5 -> libevent-2.0.so.5.1.9 
-rwxr-xr-x 1 root root 968690 Aug 11 13:49 /usr/lib/libevent-2.0.so.5.1.9 
-rw-r--r-- 1 root root 1571802 Aug 11 13:49 /usr/lib/libevent.a 
lrwxrwxrwx 1 root root 26 Aug 11 13:49 /usr/lib/libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9 
-rwxr-xr-x 1 root root 585225 Aug 11 13:49 /usr/lib/libevent_core-2.0.so.5.1.9 
-rw-r--r-- 1 root root 978482 Aug 11 13:49 /usr/lib/libevent_core.a 
-rwxr-xr-x 1 root root 970 Aug 11 13:49 /usr/lib/libevent_core.la 
lrwxrwxrwx 1 root root 26 Aug 11 13:49 /usr/lib/libevent_core.so -> libevent_core-2.0.so.5.1.9 
lrwxrwxrwx 1 root root 27 Aug 11 13:49 /usr/lib/libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9 
-rwxr-xr-x 1 root root 404852 Aug 11 13:49 /usr/lib/libevent_extra-2.0.so.5.1.9 
-rw-r--r-- 1 root root 593392 Aug 11 13:49 /usr/lib/libevent_extra.a 
-rwxr-xr-x 1 root root 977 Aug 11 13:49 /usr/lib/libevent_extra.la 
lrwxrwxrwx 1 root root 27 Aug 11 13:49 /usr/lib/libevent_extra.so -> libevent_extra-2.0.so.5.1.9 
-rwxr-xr-x 1 root root 935 Aug 11 13:49 /usr/lib/libevent.la 
lrwxrwxrwx 1 root root 30 Aug 11 13:49 /usr/lib/libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9 
-rwxr-xr-x 1 root root 18430 Aug 11 13:49 /usr/lib/libevent_pthreads-2.0.so.5.1.9 
-rw-r--r-- 1 root root 18670 Aug 11 13:49 /usr/lib/libevent_pthreads.a 
-rwxr-xr-x 1 root root 998 Aug 11 13:49 /usr/lib/libevent_pthreads.la 
lrwxrwxrwx 1 root root 30 Aug 11 13:49 /usr/lib/libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9 
lrwxrwxrwx 1 root root 21 Aug 11 13:49 /usr/lib/libevent.so -> libevent-2.0.so.5.1.9
[root@master libevent-2.0.21-stable]# cd ..

2、安装memcached

1
2
3
4
5
6
7
8
9
[root@master src]# tar zxvf memcached-1.4.20.tar.gz
[root@master src]# cd memcached-1.4.20
[root@master memcached-1.4.20]# ./configure --with-libevent=/usr
[root@master memcached-1.4.20]# wget http://code.taobao.org/p/nolinux/src/memcached/src/memcached-1.4.20.tar.gz?orig
[root@master memcached-1.4.20]# make
[root@master memcached-1.4.20]# make install
[root@master memcached-1.4.20]# cd ..
[root@master src]# ll /usr/local/bin/memcached    # 安装完成后会把memcached 放到 /usr/local/bin/memcached 
-rwxr-xr-x 1 root root 341907 Aug 11 13:52 /usr/local/bin/memcached

注意:如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径

三、repcached安装

方式一:使用repcached版本

1
2
3
[root@master src]# wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
[root@master src]# tar zxf memcached-1.2.8-repcached-2.2.tar.gz
[root@master src]# cd memcached-1.2.8-repcached-2.2

方式二:使用patch版本

1
2
3
4
5
6
[root@master memcached-1.2.8-repcached-2.2]# wget http://downloads.sourceforge.net/repcached/repcached-2.2-1.2.8.patch.gz
[root@master memcached-1.2.8-repcached-2.2]# gzip -cd ../repcached-2.2-1.2.8.patch.gz | patch -p1
[root@master memcached-1.2.8-repcached-2.2]# ./configure --enable-replication
[root@master memcached-1.2.8-repcached-2.2]#  make
[root@master memcached-1.2.8-repcached-2.2]#  make install
[root@master memcached-1.2.8-repcached-2.2]# cd ..

以上操作,我们需要针对主节点和备节点都操作!这里我仅仅以主节点的部署为例!切记!


四、启动配置

1、启动master

1
2
3
4
[root@master ~]# memcached -v -d -p 11211 -l 192.168.0.102 -u root -P /tmp/memcached1.pid 
[root@master ~]# replication: listen 
[root@master ~]# replication: accept

2、启动salve

1
2
3
4
5
6
[root@slave src]# memcached -v -d -p 11211 -l 192.168.0.103 -u root -x 192.168.0.102 -P /tmp/memcached1.pid 
[root@slave src]# replication: connect (peer=192.168.0.102:11212) 
replication: marugoto copying 
replication: start 
[root@slave src]#

3、回到master节点

1
[root@master ~]# replication: accept  # 启动正常后,master 将 accept

五、测试

由于我们主节点和从节点的memcached服务都骑起来了,并且监听也都正常,所以以下的测试操作全部放到master节点进行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
[root@master ~]# telnet 192.168.0.102 11211    # 连接到我们主节点,添加一个记录
Trying 192.168.0.102... 
Connected to 192.168.0.102. 
Escape character is '^]'
set key 0 0 6 
sunsky 
STORED 
quit 
Connection closed by foreign host. 
[root@master ~]# telnet 192.168.0.103 11211    # 连接到我们的从节点,查看主节点的记录是否同步过来
Trying 192.168.0.103... 
Connected to 192.168.0.103. 
Escape character is '^]'
get key 
VALUE key 0 6 
sunsky 
END 
quit 
Connection closed by foreign host. 
[root@master ~]# pkill memcached     # 现在,杀掉我们主节点的memcached进程
replication: cleanup start 
replication: close 
replication: cleanup complete
[root@slave src]# replication: close  # 备节点此时变为监听状态,即变成了主节点
replication: listen
[root@master ~]# telnet 192.168.0.103 11211   # 查看从节点上面的数据是否还存在
Trying 192.168.0.103... 
Connected to 192.168.0.103. 
Escape character is '^]'
get key 
VALUE key 0 6 
sunsky 
END 
quit 
Connection closed by foreign host. 
[root@master ~]# memcached -v -d -p 11211 -l 192.168.0.102 -u root -x 192.168.0.103 -P /tmp/memcached.pid   #  由于memcached的主/从没有抢占功能,因此主恢复之后,只能作为现有主节点的从节点[root@master ~]# replication: connect (peer=192.168.0.103:11212) 
replication: marugoto copying 
replication: start 
[root@slave src]# replication: accept    # 在上面加入之后,下面之前的从节点就会蹦出如下输入,表示开启同步
replication: marugoto start 
replication: marugoto 1 
replication: marugoto owari
[root@master ~]# telnet 192.168.0.102 11211   # 我们连接到刚刚恢复的节点,可以看到数据又回来了
Trying 192.168.0.102... 
Connected to 192.168.0.102. 
Escape character is '^]'
get key 
VALUE key 0 6 
sunsky 
END 
quit 
Connection closed by foreign host.

以上就是我们做的关于memcached基于repcached的主从复制实验了。通过实验,我们可以看出,通过他我们实现了主从中任何一个宕机,都不会影响另外一台机器上的数据。

在文章最后,我们再来总结以下基于 Repcached 的 Memcached 主从的优缺点:

1
2
3
4
5
优点:
1、能够实现 cache 的冗余功能
2、主从之间可以互相读写
缺点:
1、尽可以一主一从,单对单

本文转自 aaao 51CTO博客,原文链接:http://blog.51cto.com/nolinux/1544931,如需转载请自行联系原作者

Memcached实战之复制----基于repcached的主从相关推荐

  1. mysql主从复制、基于GTID的主从、半同步

    使用的mysql版本5.7.17 一.主从复制 原理: 主从复制一共有三个进程,从库生成两个线程,一个I/O线程,一个SQL线程: i/o线程去请求主库的binlog,并将得到的binlog日志写到r ...

  2. pandas基于日期信息(time or date)生成季度信息(quarter)实战:pandas基于日期信息列生成季度信息列、dt.quarter生成季度信息、dt.to_period生成季度信息

    pandas基于日期信息(time or date)生成季度信息(quarter)实战:pandas基于日期信息列生成季度信息列.dt.quarter生成季度信息.dt.to_period生成季度信息 ...

  3. 《STM32库开发实战指南:基于STM32F103(第2版)》——2.1节仿真器简介

    本节书摘来自华章社区<STM32库开发实战指南:基于STM32F103(第2版)>一书中的第2章,第2.1节仿真器简介,作者刘火良 杨森,更多章节内容可以访问云栖社区"华章社区& ...

  4. memcached 双主复制

    memcached 双主复制,配置步骤见本人笔记: http://note.youdao.com/noteshare?id=a7bec93d324a1fe20999861164318879&s ...

  5. php 单例类 mysql pdo_PHP实战:PHP基于单例模式编写PDO类的方法

    <PHP实战:PHP基于单例模式编写PDO类的方法>要点: 本文介绍了PHP实战:PHP基于单例模式编写PDO类的方法,希望对您有用.如果有疑问,可以联系我们. 一.单例模式简介 简单的说 ...

  6. 【项目实战课】基于Pytorch的SRGAN图像超分辨实战

    欢迎大家来到我们的项目实战课,本期内容是<基于Pytorch的SRGAN图像超分辨实战>.所谓项目实战课,就是以简单的原理回顾+详细的项目实战的模式,针对具体的某一个主题,进行代码级的实战 ...

  7. 【项目实战课】基于Pytorch的Pix2Pix黑白图片上色实战

    欢迎大家来到我们的项目实战课,本期内容是<基于Pytorch的黑白图像上色实战>.所谓项目实战课,就是以简单的原理回顾+详细的项目实战的模式,针对具体的某一个主题,进行代码级的实战讲解. ...

  8. 【项目实战课】基于Pytorch的StyleGAN人脸属性(表情、年龄、性别)编辑实战

    欢迎大家来到我们的项目实战课,本期内容是<基于Pytorch的StyleGAN人脸属性编辑实战>.所谓项目实战课,就是以简单的原理回顾+详细的项目实战的模式,针对具体的某一个主题,进行代码 ...

  9. 【项目实战课】基于Pytorch的StyleGAN v1人脸图像生成实战

    欢迎大家来到我们的项目实战课,本期内容是<基于Pytorch的StyleGAN v1人脸图像生成实战>. 所谓项目实战课,就是以简单的原理回顾+详细的项目实战的模式,针对具体的某一个主题, ...

最新文章

  1. 挣值管理不是搞数字游戏
  2. python isinstance type区别
  3. hdu 1203 I NEED A OFFER!
  4. scala集合同时支持不可变集合和可变集合
  5. pg_config executable not found
  6. linux view查看日志命令,【Linux】linux查看日志文件内容命令tail、cat、tac、head、echo...
  7. Linux下Rails3 + Lighttpd + fcgi部署研究
  8. 网络工程师常用英文单词和缩写翻译
  9. SQL 常用语句大全
  10. springboot禁止使用事务_springboot 中使用事务
  11. 单点登录(java)
  12. 东方联盟郭盛华获100亿战略入股,到底谁支持他?网友:不敢猜
  13. 国务院关于积极推进“互联网+”行动的指导意见
  14. ice 的 Nonmutating 和 Idempotent
  15. 〖Python 数据库开发实战 - Python与MySQL交互篇⑭〗- 项目实战 - 实现新闻管理 - 审批新闻 功能
  16. CodeForces - 727E Games on a CD 字符串Hash
  17. 麟龙指标通达信指标公式源码_通达信麟龙指标套二主图+副图指标 贴图
  18. 一篇文章说完Flutter页面路由导航及传参
  19. 基于Matlab的BiLSTM实现
  20. PHP执行Shell脚本或Bash脚本文件并返回命令输出详情

热门文章

  1. 关于UIWebView与js交互的问题
  2. PetShop的系统架构设计[转]
  3. asp.net(C#)套用模板操作Excel。
  4. 德国市占率第一的科沃斯携最新扫地机器人亮相IFA展
  5. SQL Server镜像自动生成脚本
  6. hdu 1872(稳定排序)
  7. dos删除文件与文件夹
  8. ldifde 神奇功效,对付英文系统下显示中文乱码哦
  9. knllgobjinfo: MISSING Streams multi-version data dictionary!!! 的一次处理 (二)
  10. 搭建WEB服务详解(二)