magent是一款开源的Memcached代理服务器软件,可以用它做一些高可用架构尝试。目前magent已更新到0.6,我在centos 6.0 64bit机器上面未编译通过,所以我在这里用0.5的源码来测试;

google项目地址:http://code.google.com/p/memagent/

 

一、安装步骤:
1、编译安装libevent:

 
  
  1. wget https://github.com/downloads/libevent/libevent/libevent-2.0.16-stable.tar.gz 
  2. tar zxvf libevent-2.0.16-stable.tar.gz 
  3. cd libevent-2.0.16-stable/ 
  4. ./configure --prefix=/usr 
  5. make && make install 
  6. cd ../ 

2、编译安装Memcached:

 
  
  1. wget http://memcached.googlecode.com/files/memcached-1.4.14.tar.gz 
  2. tar zxvf memcached-1.4.14.tar.gz 
  3. cd memcached-1.4.14/ 
  4. ./configure --prefix=/usr --with-libevent=/usr 
  5. make && make install 
  6. cd ../ 

3、编译安装magent:

 
  
  1. mkdir magent 
  2. cd magent/ 
  3. wget http://memagent.googlecode.com/files/magent-0.5.tar.gz 
  4. tar zxvf magent-0.5.tar.gz 
  5. /sbin/ldconfig 
  6. vim Makefile 
  7. CFLAGS = -Wall -O2 -g 
  8. 改为: 
  9. CFLAGS = -lrt -Wall -O2 -g 
  10. sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile 
  11. vim ketama.h 
  12. 加入 
  13. #ifndef SSIZE_MAX 
  14. #define SSIZE_MAX     32767 
  15. #endif  
  16. make 
  17. cp magent /usr/bin/magent 
  18. cd ../ 
安装中常遇到的问题

 
  
  1. gcc -lrt -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c 
  2.  
  3. magent.c: In function ‘writev_list’: 
  4.  
  5. magent.c:729: error: ‘SSIZE_MAX’ undeclared (first use in this function
  6.  
  7. magent.c:729: error: (Each undeclared identifier is reported only once 
  8.  
  9. magent.c:729: error: for each function it appears in.) 
  10.  
  11. make: *** [magent.o] Error 1 
解决办法:

 
  
  1. vim ketama.h 
  2.  
  3. 加入 
  4.  
  5. #ifndef SSIZE_MAX 
  6. #define SSIZE_MAX     32767 
  7. #endif  
二、高可用网络架构
此架构生产环境需要负载均衡器做负载,具体使用什么软件或硬件就要看你的具体情况来定了。                             
生产环境每台服务器分别启动一个memcached进程和magen进程做冗余
 
  
  1. memcached -d -m 256 -l 192.168.10.11 -p 11211 -u www -c 10240 -P /tmp/memcached.pid  -vv >/home/logs/memcached.txt 2>&1 
  2. memcached -d -m 128 -l 192.168.10.12 -p 11211 -u www -c 10240 -P /tmp/memcached.pid  -vv >/home/logs/memcached.txt 2>&1 
  3. memcached -d -m 128 -l 192.168.10.13 -p 11211 -u www -c 10240 -P /tmp/memcached.pid  -vv >/home/logs/memcached.txt 2>&1 
  4.  
  5. magent -u www -n 51200 -l 192.168.10.11 -p 12000 -s 192.168.10.12:11211 -s 192.168.10.13:11211 -b 192.168.10.11:11211  
  6. magent -u www -n 51200 -l 192.168.10.12 -p 12000 -s 192.168.10.12:11211 -s 192.168.10.13:11211 -b 192.168.10.11:11211  
  7. magent -u www -n 51200 -l 192.168.10.13 -p 12000 -s 192.168.10.12:11211 -s 192.168.10.13:11211 -b 192.168.10.11:11211  
参数说明:
-s 为要写入的memcached, 
-b 为备份用的memcached。
说明:测试环境用magent和memached的不同端口来实现,在生产环境中可以将magent和memached作为一组放到多台服务器上。
看到了吧这样的架构最好做负载了,用一个VIP分别映射三台magent的12000端口即可。
#实验环境测试过程:

 
  
  1. [root@odb ~]# telnet 127.0.0.1 12000 
  2. Trying 127.0.0.1… 
  3. Connected to localhost.localdomain (127.0.0.1). 
  4. Escape character is ‘^]’. 
  5. set key 0 0 8                       <—在10000端口设置key的值 
  6. 88888888 
  7. STORED 
  8. quit 
  9. Connection closed by foreign hos 
  10. [root@odb ~]# telnet 127.0.0.1 11211 
  11. Trying 127.0.0.1… 
  12. Connected to localhost.localdomain (127.0.0.1). 
  13. Escape character is ‘^]’. 
  14. get key                     <—在11211端口获取key的值成功 
  15. VALUE key 0 8 
  16. 88888888 
  17. END 
  18. quit 
  19. Connection closed by foreign host. 
  20. [root@odb ~]# telnet 127.0.0.1 11211 
  21. Trying 127.0.0.1… 
  22. Connected to localhost.localdomain (127.0.0.1). 
  23. Escape character is ‘^]’. 
  24. get key                     <—在11211端口获取key的值成功 
  25. VALUE key 0 8 
  26. 88888888 
  27. END 
  28. quit 
  29. Connection closed by foreign host. 
  30.  
  31. 高可用性测试: 
  32. [root@odb ~]# ps aux |grep -v grep |grep memcached 
  33. root     23455  0.0  0.0  5012 1796 ?        Ss   09:22   0:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11212 
  34. root     24950  0.0  0.0  4120 1800 ?        Ss   10:58   0:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11211 
  35. [root@odb ~]# ps aux |grep -v grep |grep ‘magent -u’ 
  36. root     25919  0.0  0.0  2176  484 ?        Ss   12:00   0:00 magent -u root -n 51200 -l 127.0.0.1 -p 10000 -s 127.0.0.1:11211 -b 127.0.0.1:11212 
  37. root     25925  0.0  0.0  3004  484 ?        Ss   12:00   0:00 magent -u root -n 51200 -l 127.0.0.1 -p 11000 -s 127.0.0.1:11212 -b 127.0.0.1:11211 
  38. [root@odb ~]# telnet 127.0.0.1 10000 
  39. Trying 127.0.0.1… 
  40. Connected to localhost.localdomain (127.0.0.1). 
  41. Escape character is ‘^]’. 
  42. set stone 0 0 6                      <—在10000端口设置stone的值 
  43. 123456 
  44. STORED 
  45. quit 
  46. Connection closed by foreign host. 
  47. [root@odb ~]# telnet 127.0.0.1 11000 
  48. Trying 127.0.0.1… 
  49. Connected to localhost.localdomain (127.0.0.1). 
  50. Escape character is ‘^]’. 
  51. set shidl 0 0 6                      <—在11000端口设置shidl的值 
  52. 666666 
  53. STORED 
  54. get stone                      <—在11000端口获取stone的值成功 
  55. VALUE stone 0 6 
  56. 123456 
  57. END 
  58. incr stone 2                      <—在11000端口修改stone的值成功 
  59. 123458 
  60. get stone 
  61. VALUE stone 0 6                     <—在11000端口验证stone的值,证明上面的修改成功 
  62. 123458 
  63. END 
  64. get shidl                      <—在11000端口获取shidl的值成功 
  65. VALUE shidl 0 6 
  66. 666666 
  67. END 
  68. quit                     <—退出11000端口 
  69. Connection closed by foreign host. 
  70. [root@odb ~]# telnet 127.0.0.1 10000 
  71. Trying 127.0.0.1… 
  72. Connected to localhost.localdomain (127.0.0.1). 
  73. Escape character is ‘^]’. 
  74. get stone                      <—在10000端口获取stone的值,已被修改 
  75. VALUE stone 0 6 
  76. 123458 
  77. END 
  78. get shidl                      <—在10000端口获取shidl的值成功 
  79. VALUE shidl 0 6 
  80. 666666 
  81. END 
  82. delete shidl                      <—在10000端口删除shidl 
  83. DELETED 
  84. get shidl                      <—在10000端口删除shidl生效 
  85. END 
  86. quit 
  87. Connection closed by foreign host. 
  88. [root@odb ~]# telnet 127.0.0.1 11000 
  89. Trying 127.0.0.1… 
  90. Connected to localhost.localdomain (127.0.0.1). 
  91. Escape character is ‘^]’. 
  92. get shidl                      <—在11000端口验证删除shidl生效 
  93. END 
  94. get stone                      <—在11000端口获取stone的值成功 
  95. VALUE stone 0 6 
  96. 123458 
  97. END 
  98. quit 
  99. Connection closed by foreign host. 
Down机模拟测试:
Down掉11211端口的memcached:

 
   
  1. [root@odb ~]# kill -9 24950 
  2. [root@odb ~]# telnet 127.0.0.1 10000 
  3. Trying 127.0.0.1… 
  4. Connected to localhost.localdomain (127.0.0.1). 
  5. Escape character is ‘^]’. 
  6. get stone                      <—在10000依然可以获取stone的值 
  7. VALUE stone 0 6 
  8. 123458 
  9. END 
  10. quit 
  11. Connection closed by foreign host. 
  12. [root@odb ~]# telnet 127.0.0.1 11000 
  13. Trying 127.0.0.1… 
  14. Connected to localhost.localdomain (127.0.0.1). 
  15. Escape character is ‘^]’. 
  16. get stone                      <—在11000依然可以获取stone的值 
  17. VALUE stone 0 6 
  18. 123458 
  19. END 
  20. quit 
  21. Connection closed by foreign host. 
  22.  
  23. Down掉11000端口的magent: 
  24.  
  25. [root@odb ~]# kill -9 25925 
  26. [root@odb ~]# telnet 127.0.0.1 10000 
  27. Trying 127.0.0.1… 
  28. Connected to localhost.localdomain (127.0.0.1). 
  29. Escape character is ‘^]’. 
  30. get stone                      <—在10000依然可以获取stone的值 
  31. VALUE stone 0 6 
  32. 123458 
  33. END 
  34. quit 
  35. Connection closed by foreign host.