magent + memcached 集羣測試

memcached 介紹:

Memcached 是一個高性能的分佈式內存對象緩存系統,用於動態Web應用以減輕數據庫負載。它通過在內存中緩存數據和對象來減少讀取數據庫的次數,從而提高動態、數據 庫驅動網站的速度。Memcached基於一個存儲鍵/值對的hashmap。其守護進程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,並通過memcached協議與守護進程通信。

詳細信息如下:

http://www.runoob.com/memcached/memcached-get-data.html

環境部署:
本次安裝需要用到4臺虛擬機可以一臺或者多臺根據實驗環境去做更改
192.168.3.150   安裝 memcached 服務 +magent服務
192.168.3.152   安裝 memcached 服務 +magent服務
192.168.3.153   安裝 memcached 服務
192.168.3.154   安裝 memcached 服務
創建軟件安裝目錄
[root@localhost ~]#  mkdir /home/softwares
時間同步
[root@localhost ~]#  \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  -rf
       echo '*/5 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn' >> /var/spool/cron/root
        echo 'TIMEZONE="Asia/Shanghai"' > /etc/sysconfig/clock  
        /usr/sbin/ntpdate ntp.sjtu.edu.cn
關閉selinux
[root@localhost ~]# setenforce 0
關閉防火牆
[root@localhost ~]# service iptables stop

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

/etc/init.d/memcached start

[root@localhost ~]#  memcached -u root -d -l 192.168.3.150 -p 11211 -m 1
[root@localhost ~]# ps -ef | grep memcached
498        2703      1  0 10:52 ?        00:00:00 memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid
root       2718   2646  0 10:53 pts/1    00:00:00 grep memcached
其中命令選項說明:
-d    是啓動一個守護進程;
-m    是分配給Memcache使用的內存數量,單位是MB;
-u    是運行Memcache的用戶;
-l    是監聽的服務器IP地址,可以有多個地址;
-p    是設置Memcache監聽的端口,,最好是1024以上的端口;
-c    是最大運行的併發連接數,默認是1024;
-P    是設置保存Memcache的pid文件。
其餘的三臺機器安裝memcache的安裝步驟和上面一樣,自行安裝
安裝完成之後依次執行
memcached -u root -d -l 192.168.3.150 -p 11211 -m 1
memcached -u root -d -l 192.168.3.152 -p 11211 -m 1
memcached -u root -d -l 192.168.3.153 -p 11211 -m 1
memcached -u root -d -l 192.168.3.154 -p 11211 -m 1

安裝:magent 在150 和151 上面安裝
tar -zxvf magent-0.5.tar.gz -C /home/softwares/
在ketama.h
[root@localhost softwares]# vi ketama.h
裏面添加如下內容不然報錯
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
[root@localhost softwares]# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
[root@localhost softwares]# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
[root@localhost softwares]# make
gcc -Wall -O2 -g  -c -o magent.o magent.c
gcc -Wall -O2 -g  -c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm
[root@localhost softwares]# ls
ketama.c  ketama.h  ketama.o  magent  magent.c  magent.o  Makefile
[root@localhost softwares]# cp magent /usr/sbin/
另一臺安裝流程一樣

啓動magent
=========================================================================================
設置啓動參數
 -u                              當然是啓動賬號
 -n                              是最大連接數,
 -l                              是magent監聽的IP
 -p                              是magent監聽的端口
 -s IP:端口                                          -s 代表是主服務器 然後指定IP:端口的格式
 -b IP:端口                      -b 的意思是 備用服務器
 ==========================================================================================
 格式如下
[root@localhost softwares]# magent -u root -n 52100 -l 192.168.3.150 -p 12000 -s 192.168.3.152:11211 -s 192.168.3.150:11211 -b 192.168.3.153:11211 -b 192.168.3.154:11211
[root@localhost softwares]#
[root@localhost softwares]#
[root@localhost softwares]# ps -ef | grep mag
root       2890      1  0 12:51 ?        00:00:00 magent -u root -n 52100 -l 192.168.3.150 -p 12000 -s 192.168.3.152:11211 -s 192.168.3.150:11211 -b 192.168.3.153:11211 -b 192.168.3.154:11211
root       2892   2646  0 12:51 pts/1    00:00:00 grep mag
===============================3.152的機器================================================
[root@localhost softwares]# magent -u root -n 52100 -l 192.168.3.152 -p 12000 -s 192.168.3.152:11211 -s 192.168.3.150:11211 -b 192.168.3.153:11211 -b 192.168.3.154:11211
[root@localhost softwares]# ps -ef | grep mag
root       2941      1  0 12:52 ?        00:00:00 magent -u root -n 52100 -l 192.168.3.152 -p 12000 -s 192.168.3.152:11211 -s 192.168.3.150:11211 -b 192.168.3.153:11211 -b 192.168.3.154:11211
root       2943   2684  0 12:52 pts/1    00:00:00 grep mag
測試:=================
[root@localhost softwares]# telnet 192.168.3.150 12000
Trying 192.168.3.150...
Connected to 192.168.3.150.
Escape character is '^]'.
set key1 0 0 1
1
STORED
set key2 0 0 1
2
STORED
set key3 0 0 1
3
STORED
set key4 0 0 1
5
STORED
set key5 0 0 1
1
STORED
get key1
VALUE key1 0 1
1
END
get key2
VALUE key2 0 1
2
END
get key3
VALUE key3 0 1
3
END
get key4
VALUE key4 0 1
5
END
get key5
VALUE key5 0 1
1
END
quit
Connection closed by foreign host.
================登錄其他機器查看==============
[root@localhost softwares]# telnet 192.168.3.152 12000
Trying 192.168.3.152...
Connected to 192.168.3.152.
Escape character is '^]'.
set key1
UNSUPPORTED COMMAND
get key1
VALUE key1 0 1
1
END
get key2
VALUE key2 0 1
2
END
get key3
VALUE key3 0 1
3
END
get ket4
END
get key5
VALUE key5 0 1
1
END
quit
===================[root@localhost ~]# telnet 192.168.3.153 11211
Trying 192.168.3.153...
Connected to 192.168.3.153.
Escape character is '^]'.
get key1
END
get key2
VALUE key2 0 1
2
END
get key3
END
get key4
VALUE key4 0 1
5
END
get key5
END
quit
Connection closed by foreign host.
###由此看出magent + memcached 也是根據內部算法自行分配的
=================================================
重啓其中一臺機器後啓動服務查看是否可以自動分發過來內存配置
==================總結=============================
通過以上測試可以得出結論:
1. 通過magent的連接池存放的值會分別存在magent代理的所有memcached上去。
2. 如果有一個memcached宕機通過magent代理方式還能取到值。
3. 如果memcached修復重啓後通過magent代理方式取到的值就會爲Null,這是由於memcache重啓后里邊的值隨着memcache服務的 停止就消失了(因爲在內存中),但是magent是通過key進行哈希計算分配到某臺機器上的,memcache重啓後會還從這臺機器上取值,所有取到的 值就爲空。

解決辦法:
1. 在每次memcache宕機修復後可以寫一個程序把集羣中的其他memcache的所有信息全給拷貝到當前宕機修復後的memcache中。
2. 自己寫代理,當從一個memcached服務上取到的值爲Null時再去其他memcached上取值。

注意事項:
magent的調用方式同memcached一樣,客戶端可以不用改代碼即可實現切換到magent模式下。
############################################################################################
安裝及開啓服務過程中經常遇到的問題:
在安裝magent的過程中
執行到 make 的時候,
報錯1:
gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c
magent.c: In function 'writev_list':
magent.c:729: error: 'SSIZE_MAX' undeclared (first use in this function)
magent.c:729: error: (Each undeclared identifier is reported only once
magent.c:729: error: for each function it appears in.)
make: *** [magent.o] Error 1

解決方案:
vim ./ketama.h
#在開頭加入
#ifndef SSIZE_MAX
#define SSIZE_MAX      32767
#endif



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章