今天小編給大家分享的是web中的memcache緩存服務(wù)器的詳細(xì)介紹,相信大部分人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,話(huà)不多說(shuō),一起往下看吧。
創(chuàng)新互聯(lián)專(zhuān)業(yè)提供成都主機(jī)托管四川主機(jī)托管成都服務(wù)器托管四川服務(wù)器托管,支持按月付款!我們的承諾:貴族品質(zhì)、平民價(jià)格,機(jī)房位于中國(guó)電信/網(wǎng)通/移動(dòng)機(jī)房,簡(jiǎn)陽(yáng)服務(wù)器托管服務(wù)有保障!
MemCache 是一個(gè)自由、源碼開(kāi)放、高性能、分布式的分布式內(nèi)存對(duì)象緩存系統(tǒng),用于動(dòng)態(tài)Web 應(yīng)用以減輕數(shù)據(jù)庫(kù)的負(fù)載。它通過(guò)在內(nèi)存中緩存數(shù)據(jù)和對(duì)象來(lái)減少讀取數(shù)據(jù)庫(kù)的次數(shù),從而提高了網(wǎng)站訪(fǎng)問(wèn)的速度。
實(shí)驗(yàn):
1、安裝 libevent 軟件(四臺(tái))
[root@localhost ~]# tar -zxvf libevent-2.0.21-stable.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/libevent-2.0.21-stabl
[root@localhost libevent-2.0.21-stable]# ./configure --prefix=/usr/
[root@localhost libevent-2.0.21-stable]# make && make install
2、安裝 memcached 軟件(Memcached 服務(wù)器 2 臺(tái))
[root@localhost ~]# tar -zxvf memcached-1.4.31.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/memcached-1.4.31/
[root@localhost memcached-1.4.31]# ./configure --enable-memcache --with-libevent=/usr/
[root@localhost memcached-1.4.31]# make && make install
主緩存:
[root@localhost ~]# memcached -d -m 1024 -u root -l 192.168.1.201 -p 11211
備緩存:
[root@localhost ~]# memcached -d -m 1024 -u root -l 192.168.1.202 -p 11211
[root@localhost ~]# netstat -anpt | grep memcached
3、安裝 magent 軟件(Magent 服務(wù)器 2 臺(tái))
[root@localhost ~]# mkdir /usr/magent
[root@localhost ~]# tar -zxvf magent-0.6.tar.gz -C /usr/magent/
[root@localhost ~]# cd /usr/magent/
[root@localhost magent]# vim ketama.h
添加(頭部添加):
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
[root@localhost magent]# ln -s /usr/lib64/libm.so /usr/lib64/libm.a
[root@localhost magent]# ln -s /usr/lib64/libevent-1.4.so.2 /usr/lib64/libevent.a
無(wú)法 make 的時(shí)候需要安裝 libevent-1.4.so.2
[root@localhost magent]# /sbin/ldconfig
[root@localhost magent]# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
[root@localhost magent]# make
[root@localhost magent]# cp magent /usr/bin/
主緩存和備緩存一樣的啟動(dòng)命令(-l 是漂移 IP 還沒(méi)有搭建 Keepalived 服務(wù) 暫時(shí)不要啟動(dòng) Magent 服務(wù))
[root@localhost ~]# magent -u root -n 51200 -l 192.168.1.10 -p 12000 -s 192.168.1.201:11211
-b 192.168.1.202:11211
[root@localhost ~]# magent -u root -n 51200 -l 192.168.1.10 -p 12000 -s 192.168.1.201:11211
-b 192.168.1.202:11211
-u:用戶(hù)
-n:最大連接數(shù)
-l:magent 對(duì)外監(jiān)聽(tīng) IP 地址
-p:magent 對(duì)外監(jiān)聽(tīng)端口
-s:magent 主緩存 IP 地址和端口
-b:magent 備緩存 IP 地址和端口
[root@localhost ~]# ps -elf | grep magent
4、安裝 keepalived 軟件(Magent 服務(wù)器 2 臺(tái))
[root@localhost ~]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/keepalived-1.2.13/
[root@localhost keepalived-1.2.13]# ./configure --prefix=/ --with-kerneldir=/usr/src/kernels/2.6.32-431.el6.x86_64
[root@localhost keepalived-1.2.13]# make && make install
配置主緩存服務(wù)器
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
修改:
global_defs {
router_id LVS_DEVEL_R1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.10
}
}
[root@localhost ~]# service keepalived restart
[root@localhost ~]# ip add show dev eth0
配置備緩存服務(wù)器
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
修改:
global_defs {
router_id LVS_DEVEL_R2
}
vrrp_instance VI_1 {
state BACKUP
priority 99
......
virtual_ipaddress {
192.168.1.10
}
---其他參數(shù)與主緩存服務(wù)器保持一致----
}
[root@localhost ~]# service keepalived restart
[root@localhost ~]# ip add show dev eth0
驗(yàn)證:
客戶(hù)端 ping 192.168.1.10 查看 VIP 的變化情況。
5、驗(yàn)證:
1)用主緩存節(jié)點(diǎn)連接上主緩存的 1200 端口插入數(shù)據(jù)
[root@localhost ~]# telnet 192.168.1.10 12000
Trying 192.168.1.10...
Connected to 192.168.1.10.
Escape character is '^]'.
set key 33 0 5
ggggg
STORED
quit
Connection closed by foreign host.
2)查看插入的數(shù)據(jù)
[root@localhost ~]# telnet 192.168.1.10 12000
Trying 192.168.1.10...
Connected to 192.168.1.10.
Escape character is '^]'.
get key
VALUE key 33 5
ggggg
END
quit
Connection closed by foreign host.
3)連接主緩存節(jié)點(diǎn)的 11211 端口進(jìn)行查看
[root@localhost ~]# telnet 192.168.1.201 11211
Trying 192.168.1.100...
Connected to 192.168.1.100.
Escape character is '^]'.
get key
VALUE key 33 5
ggggg
END
quit
Connection closed by foreign host.
4)連接主緩存節(jié)點(diǎn)的 11211 端口進(jìn)行查看
[root@localhost ~]# telnet 192.168.1.202 11211
Trying 192.168.1.200...
Connected to 192.168.1.200.
Escape character is '^]'.
get key
VALUE key 33 5
ggggg
END
quit
Connection closed by foreign host.
說(shuō)明主緩存節(jié)點(diǎn)和備緩存節(jié)點(diǎn)都有數(shù)據(jù)。
宕掉主緩存節(jié)點(diǎn)。
1)停止 memcached 進(jìn)程(或者斷開(kāi)主緩存節(jié)點(diǎn)的網(wǎng)卡),
2)客戶(hù)端查看
[root@localhost ~]# telnet 192.168.1.10 12000
Trying 192.168.1.10...
Connected to 192.168.1.10.
Escape character is '^]'.
get key
VALUE key 33 5
ggggg
END
總結(jié):
memcache 的優(yōu)點(diǎn):可以做多主或者多從
memcache 的缺點(diǎn):當(dāng)主緩存節(jié)點(diǎn)當(dāng)?shù)粲只謴?fù),之前的緩存數(shù)據(jù)會(huì)丟失。
配置主緩存服務(wù)器
vim keepalived.conf
! Configuration File for keepalived
global_defs {
** router_id magent-1**
}
vrrp_instance VI_1 {
state **MASTER**
interface **ens33**
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.164.100 #漂移地址
}
}
scp keepalived.conf 192.168.164.7:/etc/keepalived/
備服務(wù)器
cd /etc/keepalived/
vim keepalived.conf
! Configuration File for keepalived
global_defs {
** router_id magent-2**
}
vrrp_instance VI_1 {
state **BACKUP**
interface **ens33**
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.164.100 #漂移地址
}
}
啟動(dòng)keepalived
systemctl start keepalived.service
主緩存和備緩存一樣的啟動(dòng)命令(-l 是漂移 IP 還沒(méi)有搭建 Keepalived 服務(wù) 暫時(shí)不要啟動(dòng) Magent 服務(wù))
magent -uroot -n 51200 -l 192.168.164.100 -p 12000 -s 192.168.164.8:11211 -b 192.168.164.9:11211
以上就是memcache緩存服務(wù)器的具體介紹,內(nèi)容較為全面,而且我也相信有相當(dāng)?shù)囊恍┕ぞ呖赡苁俏覀內(nèi)粘9ぷ骺赡軙?huì)見(jiàn)到或用到的。通過(guò)這篇文章,希望你能收獲更多。