redis是单线程,一般的作为缓存使用的话,redis足够了,因为它的读写速度太快了。但是对于访问量特别大的网站来说,还是稍有不足。那么,如何提升redis的性能呢?
搭建redis集群。
Redis集群中至少应该有三个节点。要保证集群的高可用,需要每个节点有一个备份机。Redis集群至少需要6台服务器。
搭建伪分布式。可以使用一台虚拟机运行6个redis实例。需要修改redis的端口号7001-7006。
集群搭建环境
1、使用ruby脚本搭建集群。需要ruby的运行环境。
安装ruby
yum install ruby
yum install rubygems
安装ruby脚本运行使用的包。
[root@localhost ~]# gem install redis-3.0.0.gem
Successfully installed redis-3.0.0
1 gem installed
Installing ri documentation for redis-3.0.0...
Installing RDoc documentation for redis-3.0.0...
[root@localhost ~]# cd redis-3.0.7/
[root@localhost ~]# cd src
[root@localhost src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 Apr 1 2015 redis-trib.rb
[root@root src]# ./redis-trib.rb
2.搭建步骤
需要6台redis服务器。搭建伪分布式。
需要6个redis实例。
需要运行在不同的端口7001-7006
第一步:创建6个redis实例,每个实例运行在不同的端口。需要修改redis.conf配置文件。配置文件中还需要把cluster-enabled yes前的注释去掉。
第二步:启动每个redis实例。(通过编写脚本文件启动startAll.sh和关闭redis实例stopAll.sh)
第三步:使用ruby脚本搭建集群。./redis-trib.rb create --replicas 1 192.168.144.131:7001 192.168.144.131:7002 192.168.144.131:7003 192.168.144.131:7004 192.168.144.131:7005 192.168.144.131:7006
3.集群的使用方法
Redis-cli连接集群。
[root@root redis01]# ./bin/redis-cli -p 7001 -c
-c:代表连接的是redis集群
4.java测试:
附:
启动脚本内容(startAll.sh)授权 用户可执行u+x
关闭脚本内容(stopAll.sh):授权 用户可执行u+x