memcached双主复制搭建工作【转】

基于 magent 的部署架构:
该部署方式依赖于 magent 实现高可用,应用端通过负载服务器连接到 magent,然后再由 magent代理用户应用请求到 memcached 处理,底层的 memcached为双主结构会自动同步数据,本部署方式存在 magent 单点问题因此需要两个 magent 做高可用。
 
Repcached 实现原理:
在 master 上可以通过 -X 指定 replication port,在 slave 上通过 -x/-X 找到 master 并 connect 上去,事实上,如果同时指定了 -x/-X, repcached 一定会尝试连接,但如果连接失败,它就会用 -X 参数来自己 listen(成为 master);如果 master坏掉,slave侦测到连接断了,它会自动 listen而成为 master;
而如果 slave 坏掉, master 也会侦测到连接断,它就会重新 listen 等待新的 slave 加入。从这方案的技术实现来看,其实它是一个单 master 单 slave 的方案,但它的 master/slave 都是可读写的,而且可以相互同步,所以从功能上看,也可以认为它是双机 master-master 方案。
 
简化后的部署架构:
但 magent 已经有很长时间没有更新,因此可以不再使用 magent,直接通过负载均衡连接memcached,任然有两台 memcached 做高可用,memcached 会自动同步数据保持数据一致性,即使一台 memcached 故障也不影响业务正常运行,故障的 memcached 修复上线后再自动从另外一台同步数据即可保持数据一致性
 
 
部署 repcached
 
准备两台主机
172.18.142.3
172.18.141.24
 
两台主机上分别下载memcached安装包并配置:
memcached下载地址:http://repcached.sourceforge.net/

#yum install gcc libevent libevent-devel
#cd /usr/local/src/
#wget https://sourceforge.net/projects/repcached/files/repcached/2.2.1-1.2.8/memcached-1.2.8-repcached-2.2.1.tar.gz
#tar xvf memcached-1.2.8-repcached-2.2.1.tar.gz 
#cd memcached-1.2.8-repcached-2.2.1
#./configure  --prefix=/usr/local/repcached --enable-replication

 

 
 
执行make命令时报错:
#make   
make  all-recursive
make[1]: Entering directory `/usr/local/src/memcached-1.2.8-repcached-2.2.1'
Making all in doc
make[2]: Entering directory `/usr/local/src/memcached-1.2.8-repcached-2.2.1/doc'
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/usr/local/src/memcached-1.2.8-repcached-2.2.1/doc'
make[2]: Entering directory `/usr/local/src/memcached-1.2.8-repcached-2.2.1'
gcc -DHAVE_CONFIG_H -I.  -DNDEBUG   -g -O2 -MT memcached-memcached.o -MD -MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f 'memcached.c' || echo './'`memcached.c
memcached.c: In function ‘add_iov’:
memcached.c:697:30: error: ‘IOV_MAX’ undeclared (first use in this function)
         if (m->msg_iovlen == IOV_MAX ||
                              ^
memcached.c:697:30: note: each undeclared identifier is reported only once for each function it appears in
make[2]: *** [memcached-memcached.o] Error 1
make[2]: Leaving directory `/usr/local/src/memcached-1.2.8-repcached-2.2.1'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/src/memcached-1.2.8-repcached-2.2.1'
make: *** [all] Error 2

 
解决方法:

[root@centos7 memcached-1.2.8-repcached-2.2.1]#vim memcached.c
#ifndef IOV_MAX
#if defined(__FreeBSD__) || defined(__APPLE__)
# define IOV_MAX 1024
#endif
#endif

改为如下内容:

/* FreeBSD 4.x doesn't have IOV_MAX exposed. */
#ifndef IOV_MAX
# define IOV_MAX 1024
#endif

 

 
再次编译安装:
# make && make install

 

 
验证是否可执行:
#/usr/local/repcached/bin/memcached -h
memcached 1.2.8
repcached 2.2.1
-p <num>      TCP port number to listen on (default: 11211)
-U <num>      UDP port number to listen on (default: 11211, 0 is off)
-s <file>     unix socket path to listen on (disables network support)
-a <mask>     access mask for unix socket, in octal (default 0700)
-l <ip_addr>  interface to listen on, default is INDRR_ANY
-d            run as a daemon
-r            maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num>      max memory to use for items in megabytes, default is 64 MB
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections, default is 1024
-k            lock down all paged memory.  Note that there is a
              limit on how much memory you may lock.  Trying to
              allocate more than that would fail, so be sure you
              set the limit correctly for the user you started
              the daemon with (not for -u <username> user;
              under sh this is done with 'ulimit -S -l NUM_KB').
-v            verbose (print errors/warnings while in event loop)
-vv           very verbose (also print client commands/reponses)
-h            print this help and exit
-i            print memcached and libevent license
-P <file>     save PID in <file>, only used with -d option
-f <factor>   chunk size growth factor, default 1.25
-n <bytes>    minimum space allocated for key+value+flags, default 48
-R            Maximum number of requests per event
              limits the number of requests process for a given con nection
              to prevent starvation.  default 20
-b            Set the backlog queue limit (default 1024)
-x <ip_addr>  hostname or IP address of peer repcached
-X <num:num>  TCP port number for replication. <listen:connect> (default: 11212)

 

 
 
启动 memcache:
通过 repcached 安装的 memcached 命令启动 memcache 服务并实现 memcache 主备结构,其中-x 为对方即主 memcache 的 IP,-X 为本地启动的用数据同步的端口:
 
在172.18.142.3上启动服务
#/usr/local/repcached/bin/memcached -d -m 2048 -p 11211 -u root -c 65536 -x 172.18.141.24 -X 18000
 
#ss -tnl
State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
LISTEN      0      128                              *:11211                                        *:*
LISTEN      0      128                              *:22                                           *:*
LISTEN      0      100                      127.0.0.1:25                                           *:*
LISTEN      0      128                             :::11211                                       :::*

 

 
 
在172.18.141.24上启动服务
#/usr/local/repcached/bin/memcached -d -m 2048 -p 11211 -u root -c 65536 -x 172.18.142.3 -X 18000
 
#ss -tnl
State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
LISTEN      0      128                              *:11211                                        *:*
LISTEN      0      128                              *:22                                           *:*
LISTEN      0      100                      127.0.0.1:25                                           *:*
LISTEN      0      128                             :::11211                                       :::*

 

 
 
测试:连接到 memcache 验证数据
# telnet 172.18.141.24 11211
Trying 172.18.141.24...
Connected to 172.18.141.24.
Escape character is '^]'.

 

set name 0 0 4
jack
STORED
get name
VALUE name 0 4
jack
END
quit
Connection closed by foreign host.

 
 
# telnet 172.18.142.3 11211
Trying 172.18.142.3...
Connected to 172.18.142.3.
Escape character is '^]'.

 

get name
VALUE name 0 4
jack
END
quit
Connection closed by foreign host.
测试成功: 在172.18.141.24上写数据,在172.18.142.3上数据同步成功!

 

转自

memcached 双主复制_weixin_34040079的博客-CSDN博客 https://blog.csdn.net/weixin_34040079/article/details/91605661

posted @ 2020-01-03 15:17  paul_hch  阅读(382)  评论(0编辑  收藏  举报