Redis+PHP扩展的安装和Redis集群的配置 与 PHP负载均衡开发方案
以前有想过用 Memcache 实现M/S架构的负载均衡方案,直到听说了 Redis 后才发现它做得更好。发了几天时间研究了一下 Redis ,感觉真的很不错,特整理一下!
以下操作都是在 SUSE Linux 11 下安装了好了PHP为例子,如果安装过程中有提示缺少的包可以使用zypper search rpm-name 进行查询,然后用 zypper install rpm-name 安装
一:memcache 和 Redis 对比总结
[memecache 特点]
1:速度最快(没有自测,但网上有详细的测试用例)
2:支持水平扩展,可以任意添加节点
[redis 特点]
1:速度没有memcache快
2:支持M/S的主从备份
3:可以支持多数据库
4:操作指令很丰富
4:支持异步数据持久化(以文件保存)
总结:
1:如果是简单的数据缓存建议使用MEMCACHE。
2:如果要对单一操作的数据量非常的大则使用MEMCACHE
3: 如果想做性能很好的缓存集群可以用Redis(M/S读写分离,如weibo中的排行榜等)
4: 如果在高并发下又想保存数据则可以用Redis (如更新热门文章浏览次数,memcachedb也不错)
二:Redis 的详细安装,
将 redis 安装到 /usr/local/webserver/redis
1:从 http://redis.io/download 上下载安装包
1 2 3 4 5 6 7 8 9 10 | wget http: //redis .googlecode.com /files/redis-2 .2.15. tar .gz <span style="font-family: Lucida Console;">tar</span> zxvf redis-2.2.15. tar .gz <span style="font-family: Lucida Console;">cd</span> <span style="font-family: Lucida Console;">redis-2.2.15 </span> <span style="font-family: Lucida Console;">make</span> <span style="font-family: Lucida Console;"> </span> <span style="font-family: Lucida Console;">mkdir</span> -p /usr/local/webserver/redis/conf <span style="font-family: Lucida Console;">mkdir</span> -p /usr/local/webserver/redis/run <span style="font-family: Lucida Console;">mkdir</span> -p /usr/local/webserver/redis/db <span style="font-family: Lucida Console;">cp</span> redis.conf /usr/local/webserver/redis/conf/ <span style="font-family: Lucida Console;">cd</span> <span style="font-family: Lucida Console;">src</span> |
# 将 src 目录下所有可执行文件复制到安装目录
1 | <span style="font-family: Lucida Console;">cp</span> redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server mkreleasehdr.sh /usr/local/webserver/redis/ |
2:修改配置文件中的如下选项
1 2 3 4 5 | <span style="font-family: Lucida Console;">------- vi /usr/local/webserver/redis/conf/redis.conf -------- </span> <span style="font-family: Lucida Console;">daemonize yes </span> <span style="font-family: Lucida Console;">pidfile /usr/local/webserver/redis/run/redis.pid </span> <span style="font-family: Lucida Console;">dir /usr/local/webserver/redis/db </span> <span style="font-family: Lucida Console;">--------------------------------------------------------------</span> |
3:创建服务脚本
1 2 3 4 5 6 7 8 9 10 | <span style="font-family: Lucida Console;">------- vi /usr/local/webserver/redis/start.sh --------- </span> <span style="font-family: Lucida Console;">#!/bin/bash </span> <span style="font-family: Lucida Console;">/usr/local/webserver/redis/redis-server /usr/local/webserver/redis/conf/redis.conf </span> <span style="font-family: Lucida Console;">-------------------------------------------------------- </span> <span style="font-family: Lucida Console;">------- vi /usr/local/webserver/redis/stop.sh --------- </span> <span style="font-family: Lucida Console;">#!/bin/bash </span> <span style="font-family: Lucida Console;">kill `cat /usr/local/webserver/redis/run/redis.pid` </span> <span style="font-family: Lucida Console;">-------------------------------------------------------- </span> <span style="font-family: Lucida Console;"> </span> <span style="font-family: Lucida Console;">chmod a+x /usr/local/webserver/redis/start.sh /usr/local/webserver/redis/stop.sh</span> |
4:启动 redis 服务
/usr/local/webserver/redis/start.sh
验证证服务是否成功:
netstat -nlpt | grep 6379
5:启动客户端验证
1 2 3 | <span style="font-family: Lucida Console;">/usr/local/webserver/redis/redis-cli </span> <span style="font-family: Lucida Console;">>set key1 val1 </span> <span style="font-family: Lucida Console;">>get key1</span> |
三:Redis Master/Slave 主从集群配置
这里我们以本机配置 1台Master + 2台Slave 为例子,其中:
1 2 3 | <span style="font-family: Lucida Console;">Master IP:127.0.0.1 PORT:6379 </span> <span style="font-family: Lucida Console;">Slave1 IP:127.0.0.1 PORT:63791 </span> <span style="font-family: Lucida Console;">Slave2 IP:127.0.0.1 PORT:63792</span> |
1:复制两个 Slave 目录,方便管理
1 2 | <span style="font-family: Lucida Console;">cp -r /usr/local/webserver/redis /usr/local/webserver/redis-slave1 </span> <span style="font-family: Lucida Console;">cp -r /usr/local/webserver/redis /usr/local/webserver/redis-slave2</span> |
2:修改 redis-slave1 的配置文件和服务脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | -- vi /usr/local/webserver/redis-slave1/conf/redis .conf -- <span style="font-family: Lucida Console;">port 63791 </span> pidfile /usr/local/webserver/redis-slave1/run/redis .pid <span style="font-family: Lucida Console;">dir</span> <span style="font-family: Lucida Console;">/usr/local/webserver/redis-slave1/db</span> <span style="font-family: Lucida Console;">slaveof 127.0.0.1 6379 </span> <span style="font-family: Lucida Console;">---------------------------------------------------------- </span> <span style="font-family: Lucida Console;"> </span> -- vi /usr/local/webserver/redis-slave1/start .sh -- <span style="font-family: Lucida Console;">#!/bin/bash </span> <span style="font-family: Lucida Console;">/usr/local/webserver/redis-slave1/redis-server</span> /usr/local/webserver/redis-slave1/conf/redis .conf <span style="font-family: Lucida Console;">--------------------------------------------------- </span> <span style="font-family: Lucida Console;"> </span> ------- vi /usr/local/webserver/redis-slave1/stop .sh --------- <span style="font-family: Lucida Console;">#!/bin/bash </span> <span style="font-family: Lucida Console;">kill</span> ` cat /usr/local/webserver/redis-slave1/run/redis .pid` <span style="font-family: Lucida Console;">--------------------------------------------------------</span> |
3:修改 redis-slave2 的配置文件和服务脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | -- vi /usr/local/webserver/redis-slave2/conf/redis .conf -- <span style="font-family: Lucida Console;">port 63792 </span> pidfile /usr/local/webserver/redis-slave2/run/redis .pid <span style="font-family: Lucida Console;">dir</span> <span style="font-family: Lucida Console;">/usr/local/webserver/redis-slave2/db</span> <span style="font-family: Lucida Console;">slaveof 127.0.0.1 6379 </span> <span style="font-family: Lucida Console;">---------------------------------------------------------- </span> <span style="font-family: Lucida Console;"> </span> -- vi /usr/local/webserver/redis-slave2/start .sh -- <span style="font-family: Lucida Console;">#!/bin/bash </span> <span style="font-family: Lucida Console;">/usr/local/webserver/redis-slave2/redis-server</span> /usr/local/webserver/redis-slave2/conf/redis .conf <span style="font-family: Lucida Console;">--------------------------------------------------- </span> <span style="font-family: Lucida Console;"> </span> ------- vi /usr/local/webserver/redis-slave2/stop .sh --------- <span style="font-family: Lucida Console;">#!/bin/bash </span> <span style="font-family: Lucida Console;">kill</span> ` cat /usr/local/webserver/redis-slave2/run/redis .pid` <span style="font-family: Lucida Console;">--------------------------------------------------------</span> |
4:启动 M/S 服务
1 2 3 | /usr/local/webserver/redis/start .sh /usr/local/webserver/redis-slave1/start .sh /usr/local/webserver/redis-slave2/start .sh |
验证服务是否正常: netstat -nlpt | grep redis-server 有3个端口存在证明成功了
1 2 3 | <span style="font-family: Lucida Console;">0 0.0.0.0:6379 </span> <span style="font-family: Lucida Console;">0 0.0.0.0:63791 </span> <span style="font-family: Lucida Console;">0 0.0.0.0:63792</span> |
5:验证M/S服务是否生效
/usr/local/webserver/redis/redis-cli [默认连接端口:6379 的 Master服务]
>set key1 val1
>quit
/usr/local/webserver/redis/redis-cli -p 63791 [连接 Slave1 服务]
>get key1
“val1″ (数据成功同步了)
/usr/local/webserver/redis/redis-cli -p 63792 [连接 Slave2 服务]
>get key1
“val1″ (数据成功同步了)