memcached集群負載均衡

2021-09-08 16:06:48 字數 3590 閱讀 9885

memcached是針對資料庫的快取軟體 能有效降低資料庫的負載

下面是何如將多個memcached做成集群負載均衡,讓memcached變的更高可用

tar zxvf libevent-1.4.9-stable.tar.gz

cd libevent-1.4.9-stable/./configure --prefix=/usr

make

make install

tar zxvf memcached-1.2.6.tar.gz

cd memcached-1.2.6/./configure --with-libevent=/usr

make

make install

mkdir magent

cd magent/tar zxvf magent-0.5.tar.gz

/sbin/ldconfig

sed -i "

s#libs = -levent#libs = -levent -lm#g

"makefile

make

cp magent /usr/bin/magent

memcached -m 1 -u root -d -l 10.1.94.100 -p 11211memcached -m 1 -u root -d -l 10.1.94.101 -p 11211memcached -m 1 -u root -d -l 10.1.94.102 -p 11211magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 10.1.94.100:11211 -s 10.1.94.101:11211 -b 10.1.94.102:11211

1、分別在10.1.94.100 – 10.1.94.102 3臺機器的11211埠啟動3個memcached程序,在12000埠開啟magent**程式;(以此類推可在不同機器上啟動memcached)

2、10.1.94.100和101為主memcached,10.1.94.102為備份memcached;

3、連線上12000的magent,set key1和set key2,根據雜湊演算法,key1被寫入10.1.94.100和10.1.94.102的memcached,key2被寫入

10.1.94.101和10.1.94.102的memcached;

4、當10.1.94.100和101的memcached死掉,連線到12000埠的magent取資料,資料會從10.1.94.102的memcached取出;

5、當10.1.94.100和101的memcached重啟復活,連線到12000埠,magent會從10.1.94.100或10.1.94.101的memcached取資料,

由於這兩台memcached重啟後無資料,因此magent取得的將是空值,儘管10.1.94.102的memcached還有資料(此問題尚待改進)。

先在magent中新增1個key 如下:

telnet 127.0.0.1 12000

trying 127.0.0.1…

connected to 127.0.0.1.

escape character is 『^]』.

stats

memcached agent v0.4

matrix 1 -> 127.0.0.1:11211, pool size 0

matrix 2 -> 127.0.0.1:11212, pool size 0

endset key1 0 0 8

wangqi

stored

set key2 0 0 8

wangqi

stored

quit

connection closed by foreign host.

然後在2個輪詢的memcached中get剛才新增的key是否返回正確:

telnet 10.1.94.100 11211

trying 10.1.94.100…

connected to 10.1.94.100.

escape character is 『^]』.

get key1

endget key2

value key2 0 8

wangqi

endquit

connection closed by foreign host.

telnet 10.1.94.101 11211

trying 10.1.94.101…

connected to 10.1.94.101.

escape character is 『^]』.

get key1

value key1 0 8

wangqi

endget key2

endquit

connection closed by foreign host.

telnet 10.1.94.103 11211

trying 10.1.94.103…

connected to 10.1.94.103.

escape character is 『^]』.

get key1

value key1 0 8

wangqi

endget key2

value key2 0 8

wangqi

endquit

connection closed by foreign host.

模擬10.1.94.100和101的memcached死掉

直接kill程序然後:

telnet 127.0.0.1 12000

trying 127.0.0.1…

connected to 127.0.0.1.

escape character is 『^]』.

get key1

value key1 0 8

wangqi

endget key2

value key2 0 8

wangqi

endquit

connection closed by foreign host.

模擬10.1.94.100和101的memcached重啟復活

telnet 127.0.0.1 12000

trying 127.0.0.1…

connected to 127.0.0.1.

escape character is 『^]』.

get key1

endget key2

endquit

connection closed by foreign host.

magent -u root(啟動使用者uid) -n 51200(最大連線數) -l 127.0.0.1(ip位址) -p 12000(監聽埠) -s 127.0.0.1:11211(memcached的ip) -s 127.0.0.1:11212(memcached的ip) -b 127.0.0.1:11213(備份memcached的ip)

如果不需要輪詢hash的話 只有一主一備就可以了 但是在主當掉後備份memcached接手 然後在主服務恢復後資料會根據主伺服器走….

**

Apache tomcat集群(負載均衡)

本次實踐我是在windows系統的環境下進行的,apache是2.2.21版本,tomcat是7.0.16和7.0.23兩個版本 集群中tomcat不能既有tomcat6又有tomcat7,否則雖能夠負載均衡,但不能進行session複製 2.配置tomcat 1 首先講講tomcat需要配置的檔案...

集群與負載均衡

集群是一組協同工作的服務實體。當客戶請求到來時,集群內部的負載均衡器會比較均衡的把很多客戶的請求分布到集群環境下的計算和網路資源,也就是說,負載均衡是集群的乙個重要特性。集群的另外乙個特性就是錯誤恢復的能力,當乙個實體中的資源不能工作,另乙個實體中的資源透明的繼續完成任務的過程叫做錯誤恢復。集群實體...

Nginx Tomcat負載均衡集群

nginx 1.13.4 apache tomcat 8.5.20 實現高效能負載均衡的tomcat集群 2 然後解壓兩個tomcat,分別命名為nginx tomcat和nginx tomcat1 3 然後修改這兩個tomcat的啟動埠,分別為8080和8081,下面以修改第一台tomcat為例,...