今天小编给大家分享的是web中的memcache缓存服务器的详细介绍,相信大部分人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,话不多说,一起往下看吧。
MemCache 是一个自由、源码开放、高性能、分布式的分布式内存对象缓存系统,用于动态Web 应用以减轻数据库的负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高了网站访问的速度。
实验:
1、安装 libevent 软件(四台)
[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 服务器 2 台)
[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 服务器 2 台)
[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
无法 make 的时候需要安装 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/
主缓存和备缓存一样的启动命令(-l 是漂移 IP 还没有搭建 Keepalived 服务 暂时不要启动 Magent 服务)
[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:用户
-n:最大连接数
-l:magent 对外监听 IP 地址
-p:magent 对外监听端口
-s:magent 主缓存 IP 地址和端口
-b:magent 备缓存 IP 地址和端口
[root@localhost ~]# ps -elf | grep magent
4、安装 keepalived 软件(Magent 服务器 2 台)
[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
配置主缓存服务器
[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
配置备缓存服务器
[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
}
---其他参数与主缓存服务器保持一致----
}
[root@localhost ~]# service keepalived restart
[root@localhost ~]# ip add show dev eth0
验证:
客户端 ping 192.168.1.10 查看 VIP 的变化情况。
5、验证:
1)用主缓存节点连接上主缓存的 1200 端口插入数据
[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)查看插入的数据
[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)连接主缓存节点的 11211 端口进行查看
[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)连接主缓存节点的 11211 端口进行查看
[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.
说明主缓存节点和备缓存节点都有数据。
宕掉主缓存节点。
1)停止 memcached 进程(或者断开主缓存节点的网卡),
2)客户端查看
[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
总结:
memcache 的优点:可以做多主或者多从
memcache 的缺点:当主缓存节点当掉又恢复,之前的缓存数据会丢失。
配置主缓存服务器
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/
备服务器
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 #漂移地址
}
}
启动keepalived
systemctl start keepalived.service
主缓存和备缓存一样的启动命令(-l 是漂移 IP 还没有搭建 Keepalived 服务 暂时不要启动 Magent 服务)
magent -uroot -n 51200 -l 192.168.164.100 -p 12000 -s 192.168.164.8:11211 -b 192.168.164.9:11211
以上就是memcache缓存服务器的具体介绍,内容较为全面,而且我也相信有相当的一些工具可能是我们日常工作可能会见到或用到的。通过这篇文章,希望你能收获更多。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。