Memcached高可用元件之repcached

1874發表於2020-08-11

  在前邊的tomcat session server msm的那篇部落格我們用memcached做tomcat session伺服器,預設官方memcached是不支援主從同步的,為了解決memcached的高可用,我們是在客戶端實現雙寫和排程,把一份session 多次寫入後端的session伺服器上,這樣使得多臺memcached伺服器之間的資料有了冗餘備份,即便叢集中某一臺memcached當機以後,也不會丟失session;在使用者訪問上,我們在客戶端上實現把排程自動切換到沒有當機的memcached上,這樣又保證了線上業務的正常使用;memcached 它沒有redis那種持久化的特點,它的資料上儲存到記憶體中,一旦伺服器掉電,或者重啟服務都會導致資料丟失;

  memcached單機部署

  1、使用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就安裝啟動完成;

  2、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的編譯安裝就完成了

  3、使用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]# 

  提示:檢查編譯環境沒有問題後,就可以執行編譯和安裝了;

  編譯安裝

[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

  提示:通過 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中去;

相關文章