赞
踩
数据存储方式:Slab Allocation
数据过期方的两种方式
LRU
Laxzy Expiration
求余数 hash 算法
一致性 hash 算法
原理
主机 | 操作系统 | 主机名/IP地址 | 主要软件及版本 |
服务器 | CentOS7.9 | Memcache1/192.168.10.101 | libevent-2.1.8-stable.tar.gz |
服务器 | CentOS7.9 | Memcache2/192.168.10.102 | libevent-2.1.8-stable.tar.gz |
客户端 | CentOS7.9 | Memcached-API/192.168.10.103 | httpd-2.4.25.tar.gz |
设置各节点的主机名
主机
- [root@localhost ~]# hostnamectl set-hostname memcache1
- [root@localhost ~]# bash
主机
- [root@localhost ~]# hostnamectl set-hostname memcache2
- [root@localhost ~]# bash
主机
- [root@localhost ~]# hostnamectl set-hostname memcached-API
- [root@localhost ~]# bash
修改两台主机名
- [root@localhost ~]# hostnamectl set-hostname memcache1
- [root@localhost ~]# bash
- [root@localhost ~]# hostnamectl set-hostname memcache2
- [root@localhost ~]# bash
首先关闭防火墙、内核机制
- [root@memcache1 ~]# systemctl stop firewalld
- [root@memcache1 ~]# setenforce 0
上传 libevent 包至服务器
解压
[root@memcache1 ~]# tar zxvf libevent-2.1.8-stable.tar.gz
进入 libevent
[root@memcache1 ~]# cd libevent-2.1.8-stable
安装 gcc* 环境
[root@memcache1 libevent ~]# yum -y install gcc*
配置路径前缀
[root@memcache1 libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
make install 执行
[root@memcache1 libevent-2.1.8-stable]# make && make install
Libevent
亮点
上传 memcached-1.5.1.tar.gz 至服务器
解压
[root@memcache1 ~]# tar zxvf memcached-1.5.1.tar.gz
进入
[root@memcache1 ~]# cd memcached-1.5.1
配置路径
- [root@memcache1 memcached-1.5.1]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
-
- //退出
- [root@memcache1 memcached-1.5.1]# cd
上传 memcached.service 脚本至服务器
下载 psmisc
[root@memcache1 ~]# yum -y install psmisc
添加执行权
[root@memcache1 ~]# chmod +x memcached_service.sh
启动服务
[root@memcache1 ~]# ./memcached_service.sh start
查看进程
- [root@memcache1 ~]# netstat -anpt | grep memcached
- tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 20564/memcached
- tcp6 0 0 :::11211 :::* LISTEN 20564/memcached
Memcached API 客户端
先安装 gcc*
[root@memcached-api]# yum -y install gcc*
设置好 yum 源
- [root@memcached-api ~]# rm -rf /etc/yum.repos.d/*
- curl -o /etc/yum.repos.d/CentOS-Base.repo ftp://192.168.2.149/repo/CentOS-Base.repo
- curl -o /etc/yum.repos.d/epel.repo ftp://192.168.2.149/repo/epel.repo
- [root@memcached-api ~]# yum clean all
安装 httpd、php
[root@memcached-api ~]# yum -y install httpd mariadb mariadb-server php php-devel php-mysql
启动服务
[root@memcached-api ~]# systemctl start mariadb
数据库设置密码
[root@memcached-api ~]# mysqladmin -uroot password 'pwd123'
启动 httpd
[root@memcached-api ~]# systemctl start httpd
关闭防火墙、内核机制
- [root@memcached-api ~]# systemctl stop firewalld
- [root@memcached-api ~]# setenforce 0
切换目录
[root@memcached-api ~]# cd /var/www/html
编写测试文件
- [root@memcache-api html]# vim test1.php
- <?php
- phpinfo();
- ?>
浏览器访问 192.168.10.103/test1.php
退出当前目录
上传 libmemcached-1.0.18.tar.gz 至服务器
解压
[root@memcached-api ~]# tar zxvf libmemcached-1.0.18.tar.gz
进入
[root@memcached-api ~]# cd libmemcached-1.0.18
配置安装路径
[root@memcached-api libmemcached-1.0.18]# ./configure --prefix=/usr/local/libmemcached --with-memcached=/usr/local/memcached
make install 执行
root@memcached-api libmemcached-1.0.18]# make && make install
退出当前目录
上传 memcached-2.2.0.tgz 至服务器
解压
[root@memcached-api ~]# tar zxvf memcached-2.2.0.tgz
进入
[root@memcached-api ~]# cd memcached-2.2.0
利用 phpize 生产脚本
[root@memcached-api memcached-2.2.0]# phpize
安装 zlib
[root@memcached-api memcached-2.2.0]# yum -y install zlib
指定配置脚本位置
[root@memcached-api memcached-2.2.0]#./configure --with-php-config=/usr/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/ --disable-memcachedsasl --with-zlib-dir
make install 执行
[root@memcached-api memcached-2.2.0]# make && make install
退出当前目录
修改配置文件
- [root@memcached-api ~]# vim /etc/php.ini
- //添加下面内容
- extension=memcached.so
重启 httpd
[root@memcached-api ~]# systemctl restart httpd
浏览器刷新网站查看
切换目录
[root@memcached-api ~]# cd /var/www/html/
上传 test2.php代码至服务器
刷新网站测试
安装 telnet 软件包
[root@memcachel ~]# yum -y install telnet
连接
- [root@memcachel ~]# telnet 127.0.0.1 11211
- Trying 127.0.0.1...
- Connected to 127.0.0.1.
- Escape character is '^]'.
- get key
- VALUE key 3932160 25
- Memcache test successful!
- END
- quit
- root@memcachel ~]# telnet 127.0.0.1 11211
- //添加一条键值数据
- add username 0 0 8
- zhangsan
- STORED
表示键值名为 username
标记位表示自定义信息为 0
过期时间为 0(永不过期,单位为秒)
字节数为 8
zhangsan 为值,这里需要注意输入长度为8 字节,与设定值符合
- //查询键值数据
- get username
- VALUE username 0 8
- zhangsan
- END
- gets username
- VALUE username 0 8 2
- zhangsan
- END
get 后跟键名。如果检查最近是否更新,可以使用 gets,最后一位显示的是更新因子,每更新一次更新因子数会加一
- //更新一条键值数据
- set username 0 0 4
- lisi
- STORED
- get username
- VALUE username 0 4
- everything
- END
-
- gets username
- VALUE username 0 4 4
- lisi
- END
- //清除一条缓存数据
- delete username
- DELETED
- get username
- END
- //检查后更新 check and set
- add username 0 0 8
- zhangsan
- STORED
-
-
- gets username
- VALUE username 0 8 7
- zhangsan
- END
-
- //注意,因为更新因子和gets得到的不一致,这个语句的更新会失败
- cas username 0 0 8 1
- lodging
- EXISTS
-
- cas username 0 0 8 7
- zhangsai
- STORED
- gets username
- VALUE username 0 7 8
- lodging
- END

cas命令的最后一个数字是更新因子,如果gets获取到的更新因子和cas命令提供的更新因子一致,则更新改数据,否则不会更新,会提示exists
cas命令的第三个数字是要更新的数据的字节数,和接下来输入进去的字符串的字节数要一致,否则会报错
- //追加数据
- //后追加 8 字节
- append username 0 0 8
- zhangsan
- STORED
- get username
- VALUE username 0 14
- lodgingzhangsan
- END
- //清除所有缓存数据
- flush_all
- OK
- //查看服务器统计信息
- stats
- //返回所有键值对统计信息
- stats items
- //返回指定存储空间的键值对
- stats cachedump 1 0
- //显示各个 slab 的信息,包括 chunk 的大小、数目、使用情况等
- stats slabs
- //输出所有 item 的大小和个数
- stats sizes
- //清空统计数据
- stats reset
- //退出
- quit
支持多个 Memcached 之间相互复制(双向复制,主备都是可读可写的)
可以解决 Memcached 的容灾问题
(此步骤在两台memcache服务器上都执行)
修改主机名
- [root@localhost ~]# hostnamectl set-hostname memcache1
- [root@localhost ~]# bash
- [root@localhost ~]# hostnamectl set-hostname memcache2
- [root@localhost ~]# bash
这里开启会话同步
首先关闭防火墙、内核机制
- [root@memcache1 ~]# systemctl stop firewalld
- [root@memcache1 ~]# setenforce 0
安装 gcc* 环境
[root@memcache1 ~]# yum -y install gcc*
上传 libevent-2.1.8-stable.tar.gz 至服务器
解压
[root@memcache1 ~]# tar zxvf libevent-2.1.8-stable.tar.gz
进入
[root@memcache1 ~]# cd libevent-2.1.8-stable
配置文件路径
[root@memcache1 libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
make install 执行
[root@memcache1 libevent-2.1.8-stable]# make && make install
上传 memcached-1.2.8-repcached-2.2.tar.gz
解压
[root@memcache1 ~]# tar zxvf memcached-1.2.8-repcached-2.2.tar.gz
进入
[root@memcache1 ~]# cd memcached-1.2.8-repcached-2.2
配置文件路径
[root@Memcached1 memcached-1.2.8-repcached-2.2]# ./configure --prefix=/usr/local/memcached_replication --enable-replication --with-libevent=/usr/local/libevent
修改源代码配置文件
- [root@memcache1 memcached-1.2.8-repcached-2.2]# vim memcached.c
- //找到如下几行(在55行--60行):
- /* FreeBSD 4.x doesn't have IOV_MAX exposed. */
- #ifndef IOV_MAX
- #if defined(__FreeBSD__) || defined(__APPLE__)
- # define IOV_MAX 1024
- #endif
- #endif
-
- //修改成如下所示:(删掉上面红色的两行)
- /* FreeBSD 4.x doesn't have IOV_MAX exposed. */
- #ifndef IOV_MAX
- # define IOV_MAX 1024
- #endif
make install 执行
[root@memcache1 memcached-1.2.8-repcached-2.2]# make && make install
函数路径链接
[root@memcache1 ~]# ln -s /usr/local/libevent/lib/libevent-2.1.so.6 /usr/lib64/
这里取消会话同步
启动服务
[root@Memcached1 ~]# /usr/local/memcached_replication/bin/memcached -d -u root -m 128 -x 192.168.10.101
两台服务器查看进程
- [root@memcache1 ~]# netstat -antp |grep memcached
- tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 52646memcached
- tcp 0 0 192.168.10.101:11212 192.168.10.102:38298 ESTABLISHED 52646memcached
- tcp6 0 0 :::11211 :::* LISTEN 52646memcached
- [root@memcache2 ~]# netstat -antp |grep memcached
- tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 29976memcached
- tcp 0 0 192.168.10.102:38298 192.168.10.101:11212 ESTABLISHED 29976memcached
- tcp6 0 0 :::11211 :::* LISTEN 29976memcached
使用 telnet 进行简单验证复制功能
安装 telnet
[root@memcache1 ~]# yum -y install telnet
在 Memcached1 上插入一条具有特点的键值
- [root@memcache1 ~]# telnet 192.168.10.102 11211
- //自定义信息+永不过期+8个字节
- set username 0 0 8
- 12345678
- STORED
- get username
- VALUE username 0 8
- 12345678
- END
- quit
在 Memcached2 上进行查看刚刚插入的键值
安装 telnet
[root@memcache1 ~]# yum -y install telnet
- [root@memcache2 ~]# telnet 192.168.10.102 11211
- get username
- VALUE username 0 8
- 12345678
- END
- gets username
- //1代表更新因子
- VALUE username 0 8 1
- 12345678
- END
- //自定义信息+永不过期+8个字节+更新因子(要与gets得到的一致)
- cas username 0 0 8 1
- zhangsan
- STORED
这里同步会话
安装配置 keepalived(两台主机都安装)
[root@memcache1 ~]# yum -y install keepalived
修改主 keepalived 配置文件
- [root@memcache1 ~]# vim /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
-
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- notification_email_from Alexandre.Cassen@firewall.loc
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_01
- vrrp_skip_check_adv_addr
- #vrrp_strict
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- }
-
- //定义要执行的脚本
- vrrp_script check_down {
- //脚本路径和名称
- script "/etc/keepalived/memcached.sh"
- //间隔1秒执行一次
- interval 1
- }
-
-
- vrrp_instance VI_1 {
- //master角色不能设置nopreempt,所以此处要设置为BACKUP
- state BACKUP
- interface ens33
- virtual_router_id 51
- //从主机的优先级要小于此数字
- priority 100
- advert_int 1
- //添加,此语句关闭了抢占功能,从主机不需要
- nopreempt
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.10.100
- }
-
- //调用前面的脚本
- track_script {
- check_down
- }
- }

vrrp_strict:严格遵守VRRP协议,禁止以下状况
无VIP地址
配置了单播邻居
在VRRP版本2中有IPv6地址,开启动此项会自动开启iptables防火墙规则,建议关闭此项配置
编写脚本文件
- [root@memcached1 keepalived]# vim /etc/keepalived/memcached.sh
- #!/bin/bash
- if [ $(ps -C memcached --no-header | wc -l) -eq 0 ]; then
- systemctl stop keepalived
- fi
添加执行权限
[root@memcached1 keepalived]# chmod +x /etc/keepalived/memcached.sh
配置从 keepalived 文件
- [root@memcache2 ~]# vim /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
-
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- notification_email_from Alexandre.Cassen@firewall.loc
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_02
- vrrp_skip_check_adv_addr
- #vrrp_strict
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- }
-
- //定义要执行的脚本
- vrrp_script check_down {
- //脚本路径和名称
- script "/etc/keepalived/memcached.sh"
- //间隔1秒执行一次
- interval 1
- }
-
-
- vrrp_instance VI_1 {
- state BACKUP
- interface ens33
- virtual_router_id 51
- //从主机的优先级要小于此数字
- priority 99
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.10.100
- }
-
- //调用前面的脚本
- track_script {
- check_down
- }
- }

编写脚本文件
- [root@memcached2 keepalived]# vim /etc/keepalived/memcached.sh
- #!/bin/bash
- if [ $(ps -C memcached --no-header | wc -l) -eq 0 ]; then
- systemctl stop keepalived
- fi
添加执行权限
[root@memcached2 keepalived]# chmod +x /etc/keepalived/memcached.sh
vrrp_strict #严格遵守VRRP协vim议
这将禁止VIPs unicast peers (虚拟IP单播对等体),这样会使得这个VIP无法进行单播通信
两台服务器关闭 NetworkManager
[root@memcachel keepalived]# systemctl stop NetworkManager
测试验证
两台服务器上启动 keepalived
[root@Memcached1 ~]# systemctl start keepalived
[root@memcached2 ~]# systemctl start keepalived
主服务器查看 VIP
- [root@memcachel keepalived]# ip a
- 2:ens33: <BROADCAST,MULTICAST,UP,LOWER UP> mtu 1500 gdisc pfifo fast stateoup default glen 1000
- inet 192.168.10.100/32 scope global ens33
在客户端修改缓存服务器地址
切换目录
[root@memcached-api ~]# cd /var/www/html
- [root@memcached-api html]# vim /usr/local/httpd/htdocs/test2.php
- <?php
- $memcache = new Memcached();
- $memcache->addServer('192.168.10.100', 11211);
- $memcache->set('key', 'Memcache test01 successful!', 0, 60);
- $result = $memcache->get('key');
- unset($memcache);
- echo $result;
- ?>
浏览器刷新测试
pkill memcached
检查当前主机的memceched进程和keepalived进程
关闭memcached后,该主机的keepalived也会被脚本关闭,于是VIP就漂移到了另一台主机,待此主机修好后,把memcached和keepalived开启,由于设置了不抢占,VIP不会抢占回来,但是把第二台主机的memcached关闭后,第二台主机的keepalived也会关闭,于是VIP就漂移回了第一台主机。完成故障的转移
小阿轩yx-高性能内存对象缓存
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。