Memcached双主模型之repcached

  在前边的tomcat session server msm的那篇博客我们用memcached做tomcat session服务器,默认官方memcached是不支持主从同步的,为了解决memcached的高可用,我们是在客户端实现双写和调度,把一份session 多次写入后端的session服务器上,这样使得多台memcached服务器之间的数据有了冗余备份,即便集群中某一台memcached宕机以后,也不会丢失session;在用户访问上,我们在客户端上实现把调度自动切换到没有宕机的memcached上,这样又保证了线上业务的正常使用;memcached 它没有redis那种持久化的特点,它的数据上保存到内存中,一旦服务器掉电,或者重启服务都会导致数据丢失;

  memcached单机部署

  一、使用yum安装memcached

  查看memcached包简介信息

[root@slave01 ~]# yum info memcached
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Available Packages
Name        : memcached
Arch        : x86_64
Version     : 1.4.15
Release     : 10.el7_3.1
Size        : 85 k
Repo        : base/7/x86_64
Summary     : High Performance, Distributed Memory Object Cache
URL         : http://www.memcached.org/
License     : BSD
Description : memcached is a high-performance, distributed memory object caching
            : system, generic in nature, but intended for use in speeding up dynamic
            : web applications by alleviating database load.

[root@slave01 ~]# 

  提示:base仓库收录memcached的版本是1.4.15;

  安装memcached

[root@slave01 ~]# yum install -y memcached
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                       | 3.6 kB  00:00:00     
docker-ce-stable                                                                                           | 3.5 kB  00:00:00     
epel                                                                                                       | 4.7 kB  00:00:00     
extras                                                                                                     | 2.9 kB  00:00:00     
mariadb                                                                                                    | 2.9 kB  00:00:00     
updates                                                                                                    | 2.9 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db                                                                          | 206 kB  00:00:00     
(2/4): updates/7/x86_64/primary_db                                                                         | 3.8 MB  00:00:00     
(3/4): epel/x86_64/primary_db                                                                              | 6.9 MB  00:00:01     
(4/4): epel/x86_64/updateinfo                                                                              | 1.0 MB  00:00:05     
Resolving Dependencies
--> Running transaction check
---> Package memcached.x86_64 0:1.4.15-10.el7_3.1 will be installed
--> Processing Dependency: libevent-2.0.so.5()(64bit) for package: memcached-1.4.15-10.el7_3.1.x86_64
--> Running transaction check
---> Package libevent.x86_64 0:2.0.21-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================
 Package                       Arch                       Version                                  Repository                Size
==================================================================================================================================
Installing:
 memcached                     x86_64                     1.4.15-10.el7_3.1                        base                      85 k
Installing for dependencies:
 libevent                      x86_64                     2.0.21-4.el7                             base                     214 k

Transaction Summary
==================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 299 k
Installed size: 901 k
Downloading packages:
(1/2): libevent-2.0.21-4.el7.x86_64.rpm                                                                    | 214 kB  00:00:00     
(2/2): memcached-1.4.15-10.el7_3.1.x86_64.rpm                                                              |  85 kB  00:00:00     
----------------------------------------------------------------------------------------------------------------------------------
Total                                                                                             782 kB/s | 299 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libevent-2.0.21-4.el7.x86_64                                                                                   1/2 
  Installing : memcached-1.4.15-10.el7_3.1.x86_64                                                                             2/2 
  Verifying  : memcached-1.4.15-10.el7_3.1.x86_64                                                                             1/2 
  Verifying  : libevent-2.0.21-4.el7.x86_64                                                                                   2/2 

Installed:
  memcached.x86_64 0:1.4.15-10.el7_3.1                                                                                            

Dependency Installed:
  libevent.x86_64 0:2.0.21-4.el7                                                                                                  

Complete!
[root@slave01 ~]#

  提示:从上面的安装信息可以看到memcached依赖libevent这个包,原因是Memcache借助了操作系统的 libevent 工具做高效的读写。libevent 是个程序库,它将 Linux 的 epoll、BSD 类操作系统的 kqueue 等事件处理功能封装成统一的接口。即使对服务器的连接数增加,也能发挥高性能。memcached使用这个libevent库,因此能在 Linux、BSD、Solaris 等操作系统上发挥其高性能;

  查看memcached程序环境

[root@slave01 ~]# rpm -ql memcached
/etc/sysconfig/memcached
/usr/bin/memcached
/usr/bin/memcached-tool
/usr/lib/systemd/system/memcached.service
/usr/share/doc/memcached-1.4.15
/usr/share/doc/memcached-1.4.15/AUTHORS
/usr/share/doc/memcached-1.4.15/CONTRIBUTORS
/usr/share/doc/memcached-1.4.15/COPYING
/usr/share/doc/memcached-1.4.15/ChangeLog
/usr/share/doc/memcached-1.4.15/NEWS
/usr/share/doc/memcached-1.4.15/README.md
/usr/share/doc/memcached-1.4.15/protocol.txt
/usr/share/doc/memcached-1.4.15/readme.txt
/usr/share/doc/memcached-1.4.15/threads.txt
/usr/share/man/man1/memcached-tool.1.gz
/usr/share/man/man1/memcached.1.gz
[root@slave01 ~]# 

  提示:/etc/sysconfig/memcached这个文件是memcached的配置文件,/usr/bin/memcached是可执行程序;/usr/lib/systemd/system/memcached.service是memcached的unit file文件;

  查看配置文件

[root@slave01 ~]# cat /etc/sysconfig/memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[root@slave01 ~]# 

  提示:PORT用于指定memcached的监听端口,默认是11211,USER用于指定启动memcached的用户,默认是memcached,MAXCONN用于指定最大连接数,默认1024.CACHESIZE用于指定最大内存,默认是64M,OPTIONS用于指定其他选项的;

  查看memcached帮助

[root@slave01 ~]# memcached -h
memcached 1.4.15
-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 <addr>     interface to listen on (default: INADDR_ANY, all addresses)
              <addr> may be specified as host:port. If you don't specify
              a port number, the value you specified with -p or -U is
              used. You may specify multiple addresses separated by comma
              or by using -l multiple times
-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: 64 MB)
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections (default: 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)
-vvv          extremely verbose (also print internal state transitions)
-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)
-L            Try to use large memory pages (if available). Increasing
              the memory page size could reduce the number of TLB misses
              and improve the performance. In order to get large pages
              from the OS, memcached will allocate the total item-cache
              in one large chunk.
-D <char>     Use <char> as the delimiter between key prefixes and IDs.
              This is used for per-prefix stats reporting. The default is
              ":" (colon). If this option is specified, stats collection
              is turned on automatically; if not, then it may be turned on
              by sending the "stats detail on" command to the server.
-t <num>      number of threads to use (default: 4)
-R            Maximum number of requests per event, limits the number of
              requests process for a given connection to prevent 
              starvation (default: 20)
-C            Disable use of CAS
-b <num>      Set the backlog queue limit (default: 1024)
-B            Binding protocol - one of ascii, binary, or auto (default)
-I            Override the size of each slab page. Adjusts max item size
              (default: 1mb, min: 1k, max: 128m)
-S            Turn on Sasl authentication
-o            Comma separated list of extended or experimental options
              - (EXPERIMENTAL) maxconns_fast: immediately close new
                connections if over maxconns limit
              - hashpower: An integer multiplier for how large the hash
                table should be. Can be grown at runtime if not big enough.
                Set this based on "STAT hash_power_level" before a 
                restart.
[root@slave01 ~]# 

  提示:-P用于指定监听TCP端口,默认11211;-U用于指定监听UDP端口,默认也是11211,如果指定是0表示关闭UDP监听;-s用于指定unix socket文件;-a用于指定unix socket文件的权限,默认是0700的权限;-l用于指定监听的ip地址信息,默认是监听本机所有可用地址;-d 用于指定memcached运行为守护进程;-r用于限定core file的大小;-u用于指定运行memcached的用户名称;-m用于指定分配给memcached使用的内存,单位是MB;-M用于指定内存耗尽时返回错误,而不是删除;-c指定最大连接数,默认数1024;-k用于指定锁定所有分页内存。-v -vv -vvv 打印详细信息,v的数量越多,表示打印的信息就越详细;-h打印帮助信息;-i打印memcached和libevent的license信息;-p用于指定pid文件;-f用于指定chunk 增长因子,默认数1.25;-n用于指定为缓存数据项的key、value、flag设置最小分配字节数,默认是48;-L用于指定尝试使用大内存分页;-D用于指定统计报告中Key前缀和ID之间的分隔符,默认是冒号“:”;-t用于指定处理线程的数量,默认数4;-R用于指定对连续达到的客户端请求数设置一个限额,如果超过该设置,会选择另一个连接来处理请求,默认为20;-C用于指定关闭CAS;-b用户指定backlog队列长度,默认是1024;-B用于指定使用的协议,默认行为是自动协商(autonegotiate),可能使用的选项有auto、ascii、binary。-I用于指定覆盖默认的STAB页大小,默认是1M;-S用于指定打开Sasl身份验证;-o用于指定号分隔的选项,一般用于用于扩展或实验性质的选项;

  启动memecached

  提示:启动memcached可以把启动的选项写到配置文件,然后使用systemctl方式启动,也可以直接在命令行使用memcached命令指定选项启动;

  到此,单机memcached就安装启动完成;

  二、memcached编译安装

  安装编译环境

root@slave01 ~]# yum groupinstall "development tools" -y 
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package autoconf.noarch 0:2.69-11.el7 will be installed
--> Processing Dependency: m4 >= 1.4.14 for package: autoconf-2.69-11.el7.noarch
---> Package automake.noarch 0:1.13.4-3.el7 will be installed
--> Processing Dependency: perl(Thread::Queue) for package: automake-1.13.4-3.el7.noarch
--> Processing Dependency: perl(TAP::Parser) for package: automake-1.13.4-3.el7.noarch
---> Package bison.x86_64 0:3.0.4-2.el7 will be installed
---> Package byacc.x86_64 0:1.9.20130304-3.el7 will be installed
……省略部分内容……
Installed:
  autoconf.noarch 0:2.69-11.el7                           automake.noarch 0:1.13.4-3.el7            
  bison.x86_64 0:3.0.4-2.el7                              byacc.x86_64 0:1.9.20130304-3.el7         
  cscope.x86_64 0:15.8-10.el7                             ctags.x86_64 0:5.8-13.el7                 
  diffstat.x86_64 0:1.57-4.el7                            doxygen.x86_64 1:1.8.5-4.el7              
  elfutils.x86_64 0:0.176-4.el7                           flex.x86_64 0:2.5.37-6.el7                
  gcc.x86_64 0:4.8.5-39.el7                               gcc-c++.x86_64 0:4.8.5-39.el7             
  gcc-gfortran.x86_64 0:4.8.5-39.el7                      git.x86_64 0:1.8.3.1-23.el7_8             
  indent.x86_64 0:2.2.11-13.el7                           intltool.noarch 0:0.50.2-7.el7            
  libtool.x86_64 0:2.4.2-22.el7_3                         patch.x86_64 0:2.7.1-12.el7_7             
  patchutils.x86_64 0:0.3.3-4.el7                         rcs.x86_64 0:5.9.0-7.el7                  
  redhat-rpm-config.noarch 0:9.1.0-88.el7.centos          rpm-build.x86_64 0:4.11.3-43.el7          
  rpm-sign.x86_64 0:4.11.3-43.el7                         subversion.x86_64 0:1.7.14-14.el7         
  swig.x86_64 0:2.0.10-5.el7                              systemtap.x86_64 0:4.0-11.el7             

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7                        apr-util.x86_64 0:1.5.2-6.el7                      
  boost-date-time.x86_64 0:1.53.0-28.el7          boost-system.x86_64 0:1.53.0-28.el7                
  boost-thread.x86_64 0:1.53.0-28.el7             bzip2.x86_64 0:1.0.6-13.el7                        
  cpp.x86_64 0:4.8.5-39.el7                       dwz.x86_64 0:0.11-3.el7                            
  dyninst.x86_64 0:9.3.1-3.el7                    efivar-libs.x86_64 0:36-12.el7                     
  emacs-filesystem.noarch 1:24.3-23.el7           gdb.x86_64 0:7.6.1-119.el7                         
  gettext-common-devel.noarch 0:0.19.8.1-3.el7    gettext-devel.x86_64 0:0.19.8.1-3.el7              
  glibc-devel.x86_64 0:2.17-307.el7.1             glibc-headers.x86_64 0:2.17-307.el7.1              
  gnutls.x86_64 0:3.3.29-9.el7_6                  kernel-debug-devel.x86_64 0:3.10.0-1127.18.2.el7   
  kernel-headers.x86_64 0:3.10.0-1127.18.2.el7    libdwarf.x86_64 0:20130207-4.el7                   
  libgfortran.x86_64 0:4.8.5-39.el7               libmodman.x86_64 0:2.0.1-8.el7                     
  libmpc.x86_64 0:1.0.1-3.el7                     libproxy.x86_64 0:0.4.11-11.el7                    
  libquadmath.x86_64 0:4.8.5-39.el7               libquadmath-devel.x86_64 0:4.8.5-39.el7            
  libstdc++-devel.x86_64 0:4.8.5-39.el7           m4.x86_64 0:1.4.16-10.el7                          
  mokutil.x86_64 0:15-8.el7                       mpfr.x86_64 0:3.1.1-4.el7                          
  neon.x86_64 0:0.30.0-4.el7                      nettle.x86_64 0:2.7.1-8.el7                        
  pakchois.x86_64 0:0.4-10.el7                    perl-Error.noarch 1:0.17020-2.el7                  
  perl-Git.noarch 0:1.8.3.1-23.el7_8              perl-TermReadKey.x86_64 0:2.30-20.el7              
  perl-Test-Harness.noarch 0:3.28-3.el7           perl-Thread-Queue.noarch 0:3.02-2.el7              
  perl-XML-Parser.x86_64 0:2.41-10.el7            perl-srpm-macros.noarch 0:1-8.el7                  
  python-srpm-macros.noarch 0:3-32.el7            subversion-libs.x86_64 0:1.7.14-14.el7             
  systemtap-client.x86_64 0:4.0-11.el7            systemtap-devel.x86_64 0:4.0-11.el7                
  systemtap-runtime.x86_64 0:4.0-11.el7           trousers.x86_64 0:0.3.14-2.el7                     
  unzip.x86_64 0:6.0-21.el7                       zip.x86_64 0:3.0-11.el7                            

Complete!
[root@slave01 ~]# 

  安装依赖包

[root@slave01 ~]# yum install -y libevent-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package libevent-devel.x86_64 0:2.0.21-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================
 Package                     Arch                Version                     Repository         Size
=====================================================================================================
Installing:
 libevent-devel              x86_64              2.0.21-4.el7                base               85 k

Transaction Summary
=====================================================================================================
Install  1 Package

Total download size: 85 k
Installed size: 357 k
Downloading packages:
libevent-devel-2.0.21-4.el7.x86_64.rpm                                        |  85 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libevent-devel-2.0.21-4.el7.x86_64                                                1/1 
  Verifying  : libevent-devel-2.0.21-4.el7.x86_64                                                1/1 

Installed:
  libevent-devel.x86_64 0:2.0.21-4.el7                                                               

Complete!
[root@slave01 ~]# 

  下载源码文件

[root@slave01 ~]# wget  http://memcached.org/files/memcached-1.6.6.tar.gz
--2020-08-11 10:35:25--  http://memcached.org/files/memcached-1.6.6.tar.gz
Resolving memcached.org (memcached.org)... 107.170.231.145
Connecting to memcached.org (memcached.org)|107.170.231.145|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 543661 (531K) [application/octet-stream]
Saving to: ‘memcached-1.6.6.tar.gz’

100%[===========================================================>] 543,661     19.9KB/s   in 17s    

2020-08-11 10:35:43 (30.5 KB/s) - ‘memcached-1.6.6.tar.gz’ saved [543661/543661]

[root@slave01 ~]# ls
memcached-1.6.6.tar.gz
[root@slave01 ~]# 

  解压源码包,检查编译环境

[root@slave01 ~]# tar xf memcached-1.6.6.tar.gz 
[root@slave01 ~]# cd memcached-1.6.6/
[root@slave01 memcached-1.6.6]# ls
aclocal.m4    COPYING         itoa_ljust.h        NEWS                      stats_prefix.c
assoc.c       crawler.c       jenkins_hash.c      openbsd_priv.c            stats_prefix.h
assoc.h       crawler.h       jenkins_hash.h      protocol_binary.h         storage.c
authfile.c    crc32c.c        LICENSE.bipbuffer   README.md                 storage.h
authfile.h    crc32c.h        linux_priv.c        restart.c                 t
AUTHORS       daemon.c        logger.c            restart.h                 testapp.c
bipbuffer.c   depcomp         logger.h            sasl_defs.c               thread.c
bipbuffer.h   doc             m4                  sasl_defs.h               timedrun.c
cache.c       extstore.c      Makefile.am         scripts                   tls.c
cache.h       extstore.h      Makefile.in         sizes.c                   tls.h
ChangeLog     freebsd_priv.c  memcached.c         slab_automove.c           trace.h
compile       hash.c          memcached_dtrace.d  slab_automove_extstore.c  util.c
config.guess  hash.h          memcached.h         slab_automove_extstore.h  util.h
config.h.in   install-sh      memcached.spec      slab_automove.h           version.m4
config.sub    items.c         missing             slabs.c
configure     items.h         murmur3_hash.c      slabs.h
configure.ac  itoa_ljust.c    murmur3_hash.h      solaris_priv.c
[root@slave01 memcached-1.6.6]# ./configure --prefix=/usr/locat/memcached
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
……省略部分内容……
checking for xsltproc... /usr/bin/xsltproc
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating doc/Makefile
config.status: creating config.h
config.status: executing depfiles commands
[root@slave01 memcached-1.6.6]#

  编译安装memcached

[root@slave01 memcached-1.6.6]# make
make  all-recursive
make[1]: Entering directory `/root/memcached-1.6.6'
Making all in doc
make[2]: Entering directory `/root/memcached-1.6.6/doc'
make  all-am
make[3]: Entering directory `/root/memcached-1.6.6/doc'
make[3]: Nothing to be done for `all-am'.
make[3]: Leaving directory `/root/memcached-1.6.6/doc'
make[2]: Leaving directory `/root/memcached-1.6.6/doc'
make[2]: Entering directory `/root/memcached-1.6.6'
gcc -std=gnu99 -DHAVE_CONFIG_H -I.  -DNDEBUG   -g -O2 -pthread -pthread -Wall -Werror -pedantic -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -MT memcached-memcached.o -MD -MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f 'memcached.c' || echo './'`memcached.c
mv -f .deps/memcached-memcached.Tpo .deps/memcached-memcached.Po
gcc -std=gnu99 -DHAVE_CONFIG_H -I.  -DNDEBUG   -g -O2 -pthread -pthread -Wall -Werror -pedantic -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -MT memcached-hash.o -MD -MP -MF .deps/memcached-hash.Tpo -c -o memcached-hash.o `test -f 'hash.c' || echo './'`hash.c
mv -f .deps/memcached-hash.Tpo .deps/memcached-hash.Po
……省略部分内容……
gcc -std=gnu99 -DHAVE_CONFIG_H -I.     -g -O2 -pthread -pthread -Wall -Werror -pedantic -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -MT timedrun.o -MD -MP -MF .deps/timedrun.Tpo -c -o timedrun.o timedrun.c
mv -f .deps/timedrun.Tpo .deps/timedrun.Po
gcc -std=gnu99  -g -O2 -pthread -pthread -Wall -Werror -pedantic -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls   -o timedrun timedrun.o  -levent 
make[2]: Leaving directory `/root/memcached-1.6.6'
make[1]: Leaving directory `/root/memcached-1.6.6'
[root@slave01 memcached-1.6.6]# make install
make  install-recursive
make[1]: Entering directory `/root/memcached-1.6.6'
Making install in doc
make[2]: Entering directory `/root/memcached-1.6.6/doc'
make  install-am
make[3]: Entering directory `/root/memcached-1.6.6/doc'
make[4]: Entering directory `/root/memcached-1.6.6/doc'
make[4]: Nothing to be done for `install-exec-am'.
 /usr/bin/mkdir -p '/usr/locat/memcached/share/man/man1'
 /usr/bin/install -c -m 644 memcached.1 '/usr/locat/memcached/share/man/man1'
make[4]: Leaving directory `/root/memcached-1.6.6/doc'
make[3]: Leaving directory `/root/memcached-1.6.6/doc'
make[2]: Leaving directory `/root/memcached-1.6.6/doc'
make[2]: Entering directory `/root/memcached-1.6.6'
make[3]: Entering directory `/root/memcached-1.6.6'
 /usr/bin/mkdir -p '/usr/locat/memcached/bin'
  /usr/bin/install -c memcached '/usr/locat/memcached/bin'
 /usr/bin/mkdir -p '/usr/locat/memcached/include/memcached'
 /usr/bin/install -c -m 644 protocol_binary.h '/usr/locat/memcached/include/memcached'
make[3]: Leaving directory `/root/memcached-1.6.6'
make[2]: Leaving directory `/root/memcached-1.6.6'
make[1]: Leaving directory `/root/memcached-1.6.6'
[root@slave01 memcached-1.6.6]# ll /usr/locat/memcached/
total 0
drwxr-xr-x 2 root root 23 Aug 11 10:39 bin
drwxr-xr-x 3 root root 23 Aug 11 10:39 include
drwxr-xr-x 3 root root 17 Aug 11 10:39 share
[root@slave01 memcached-1.6.6]# 

  启动memcached

  到此memcached的编译安装就完成了

  三、使用python连接memcached写入数据

[root@slave01 ~]# cat test.py 
import memcache
m = memcache.Client(['127.0.0.1:11211'], debug=True)
for i in range(100):
    m.set("key%d" % i,"v%d" % i)
#    ret = m.get('key%d' % i)
#    print ret
[root@slave01 ~]#

  提示:以上脚本主要作用是连接到memcache后循环写入100个key;

  运行脚本,然后查看memcached上是否存储的有我们写入的key数据?

[root@slave01 ~]# python test.py
[root@slave01 ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 2
v1
END
get k12
END
get key12
VALUE key12 0 3
v12
END
quit
Connection closed by foreign host.
[root@slave01 ~]# 

  提示:可以看到运行脚本以后,连接到memcached里是可以取出我们写入到key对应的数据;

  部署 repcached
  实验环境说明
 
名称 IP地址 端口
master01 192.168.16.196 11211
master02 192.168.16.197

11211

   
 
  
  
  
  
  
  
  
  在master01上编译安装repcached
[root@master01 ~]# ls
memcached-1.2.8-repcached-2.2.tar.gz
[root@master01 ~]# tar xf memcached-1.2.8-repcached-2.2.tar.gz 
[root@master01 ~]# ls
memcached-1.2.8-repcached-2.2  memcached-1.2.8-repcached-2.2.tar.gz
[root@master01 ~]# cd memcached-1.2.8-repcached-2.2/
[root@master01 memcached-1.2.8-repcached-2.2]# ./configure --prefix=/usr/local/repcached --enable-replication
checking build system type... Invalid configuration `x86_64-unknown-linux-': machine `x86_64-unknown-linux' not recognized
configure: error: /bin/sh ./config.sub x86_64-unknown-linux- failed
[root@master01 memcached-1.2.8-repcached-2.2]# 

  提示:遇到以上错误,上因为没有安装gcc编译导致的,通常在Linux上做编译操作,我们需要先安装好编译环境;

  安装编译环境

[root@master01 memcached-1.2.8-repcached-2.2]# yum groupinstall "development tools" -y 

  再次检查编译环境

[root@master01 memcached-1.2.8-repcached-2.2]# ./configure --prefix=/usr/local/repcached --enable-replication
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking whether gcc and cc understand -c and -o together... yes
checking for a BSD-compatible install... /usr/bin/install -c
checking for libevent directory... configure: error: libevent is required.  You can get it from http://www.monkey.org/~provos/libevent/

      If it's already installed, specify its path using --with-libevent=/dir/

[root@master01 memcached-1.2.8-repcached-2.2]# 

  提示:上述错误是没有检查到libevent,解决办法安装依赖包libevent-devel包

  安装依赖包

[root@master01 memcached-1.2.8-repcached-2.2]# yum install libevent-devel -y

  再次检查编译环境

[root@master01 memcached-1.2.8-repcached-2.2]# ./configure --prefix=/usr/local/repcached --enable-replication
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking whether gcc and cc understand -c and -o together... yes
checking for a BSD-compatible install... /usr/bin/install -c
checking for libevent directory... (system)
checking for library containing socket... none required
checking for library containing gethostbyname... none required
checking for library containing mallinfo... none required
checking for daemon... yes
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for stdbool.h that conforms to C99... yes
checking for _Bool... yes
checking for an ANSI C-conforming const... yes
checking malloc.h usability... yes
checking malloc.h presence... yes
checking for malloc.h... yes
checking for struct mallinfo.arena... yes
checking for socklen_t... yes
checking for endianness... little
checking for mlockall... yes
checking for getpagesizes... no
checking for memcntl... no
configure: creating ./config.status
config.status: creating Makefile
config.status: creating doc/Makefile
config.status: creating config.h
config.status: executing depfiles commands
[root@master01 memcached-1.2.8-repcached-2.2]# 

  提示:--enable-replication选项表示开启复制功能;检查编译环境没有问题后,就可以执行编译和安装了;

  编译安装

[root@master01 memcached-1.2.8-repcached-2.2]# make && make install 
make  all-recursive
make[1]: Entering directory `/root/memcached-1.2.8-repcached-2.2'
Making all in doc
make[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2/doc'
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2/doc'
make[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2'
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:696:30: error: ‘IOV_MAX’ undeclared (first use in this function)
         if (m->msg_iovlen == IOV_MAX ||
                              ^
memcached.c:696: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 `/root/memcached-1.2.8-repcached-2.2'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'
make: *** [all] Error 2
[root@master01 memcached-1.2.8-repcached-2.2]# 

  提示:这个错误是memcached.c中的宏定义问题,处理办法就是把memcached.c中的#if defined(__FreeBSD__) || defined(__APPLE__) 和#endif注释掉即可如下所示

  提示:修改了上面的memcached.c 以后,再次编译就没有问题了

  再次编译

[root@master01 memcached-1.2.8-repcached-2.2]# make && make install
make  all-recursive
make[1]: Entering directory `/root/memcached-1.2.8-repcached-2.2'
Making all in doc
make[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2/doc'
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2/doc'
make[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2'
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
mv -f .deps/memcached-memcached.Tpo .deps/memcached-memcached.Po
gcc -DHAVE_CONFIG_H -I.  -DNDEBUG   -g -O2 -MT memcached-slabs.o -MD -MP -MF .deps/memcached-slabs.Tpo -c -o memcached-slabs.o `test -f 'slabs.c' || echo './'`slabs.c
mv -f .deps/memcached-slabs.Tpo .deps/memcached-slabs.Po
……省略部分内容……
mv -f .deps/replication.Tpo .deps/replication.Po
gcc  -g -O2   -o memcached-debug memcached.o slabs.o items.o assoc.o thread.o stats.o replication.o  -levent
make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'
make[1]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'
Making install in doc
make[1]: Entering directory `/root/memcached-1.2.8-repcached-2.2/doc'
make[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2/doc'
make[2]: Nothing to be done for `install-exec-am'.
test -z "/usr/local/repcached/share/man/man1" || /usr/bin/mkdir -p "/usr/local/repcached/share/man/man1"
 /usr/bin/install -c -m 644 './memcached.1' '/usr/local/repcached/share/man/man1/memcached.1'
make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2/doc'
make[1]: Leaving directory `/root/memcached-1.2.8-repcached-2.2/doc'
make[1]: Entering directory `/root/memcached-1.2.8-repcached-2.2'
make[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2'
test -z "/usr/local/repcached/bin" || /usr/bin/mkdir -p "/usr/local/repcached/bin"
  /usr/bin/install -c 'memcached' '/usr/local/repcached/bin/memcached'
  /usr/bin/install -c 'memcached-debug' '/usr/local/repcached/bin/memcached-debug'
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'
make[1]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'
[root@master01 memcached-1.2.8-repcached-2.2]#  ll /usr/local/repcached/
total 0
drwxr-xr-x 2 root root 46 Aug 10 23:26 bin
drwxr-xr-x 3 root root 17 Aug 10 23:26 share
[root@master01 memcached-1.2.8-repcached-2.2]# ll /usr/local/repcached/bin/
total 472
-rwxr-xr-x 1 root root 233096 Aug 10 23:26 memcached
-rwxr-xr-x 1 root root 248008 Aug 10 23:26 memcached-debug
[root@master01 memcached-1.2.8-repcached-2.2]# 

  提示:可以看到对应目录下已经有二进制文件生成,到此repcached就编译安装好了;

  验证repcached/bin/memcached是否可执行?

[root@master01 ~]# /usr/local/repcached/bin/memcached -h
memcached 1.2.8
repcached 2.2
-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>      TCP port number for replication (default: 11212)
[root@master01 ~]# 

  提示:可以看到在memcached的帮助选项中多了一个-x和-X的选项,其中-x用于指定去那个地址上的memcached同步数据,-X用来指定本地那个端口来接收同步数据;

  启动memcached

  提示:通过 repcached 安装的 memcached 命令启动 memcache 服务并实现 memcache 主备结构,其中-x 为对方即主 memcache 的 IP,-X 为本地启动的用数据同步的端口;

  在master02上编译安装repcached的过程同master01上一样的;如果你不想那么麻烦的再去编译可以把master01上编译好的二进制文件拷到master02上执行也是可以的

  从master01上拷贝二进制文件到master02上

[root@master01 ~]# scp -r /usr/local/repcached/ master02:/usr/local/
memcached.1                                                          100% 4290     1.9MB/s   00:00    
memcached                                                            100%  228KB   4.9MB/s   00:00    
memcached-debug                                                      100%  242KB   4.9MB/s   00:00    
[root@master01 ~]#

  在master02上启动memcached

[root@master02 ~]# /usr/local/repcached/bin/memcached -d -m 2048 -p 11211 -u root -c 2048 -x 192.168.16.196 -X 52113
/usr/local/repcached/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
[root@master02 ~]#

  提示:上述报错主要原因上没有安装libevent-devel,所以在执行memcached时加载共享动态库文件找不到,解决办法安装libevent-devel即可

  在master02上安装依赖包libevent-devel

[root@master02 ~]# yum install -y libevent-devel

  再次启动memcached

  提示:在master02上指定master01上的用于复制监听端口,如果master02连接上master01,那么在master02和master01上就不会在监听我们手动指定的用于复制的端口;如果你在master02上还能够看到手动指定的复制端口,说明master01和master02没有建立连接;

  验证:往master01上写入数据看看是否能够同步到master02上呢?

  提示:可以看到在master01上写数据,在master02上是能够正常读取到的;

  验证在master02上写数据,master01上是否可读取呢?

  提示:可以看到在master02上写数据,也能够及时的同步到master01上;到此基于repcached双主模型的memcached就搭建好了,我们不管往那个master上写入数据,它都会立刻同步数据到对方到memcached中去;

posted @ 2020-08-11 12:18  Linux-1874  阅读(985)  评论(2编辑  收藏  举报