redis集群三种方式
一、Redis官方提供的集群解决方案-Redis-cluster (刚发布差很少一年)
二、 Twemproxy ( twitter贡献的) 使用时间比较长,比较成熟,可是有些功能不支持
三、豌豆尖针对Twemproxy 进行改良优化后的解决方案codis
更多分布式缓存方案级别选型请参考博文:http://my.oschina.net/tantexian/blog/685620
redis应用场景:任何须要用到缓存的地方,解决本地缓存数据量过小问题。分布式缓存能有效防止本地缓存失效数据库雪崩现象。 解决分布式应用中缓存不能共用(分布式session保存)。云计算中为客户提供缓存分布式缓存redis服务(青云、UnitedStack等厂商已经具备了)
接下来主要讲解如何搭建Redis-cluster集群环境及wishstack如何使用Jedis调用Redis集群:
mkdir cluster-test
cd cluster-test mkdir 7000 7001 7002 7003 7004 7005
yum install gcc wget -y
wget http://download.redis.io/releases/redis-3.0.5.tar.gz
tar xzvf redis-3.0.5.tar.gz
cd redis-3.0.5
make(直接make报错则使用make MALLOC=libc)
yum install tcl -y
make test
make install
安装完毕redis-*常规操做命令脚本即被copy到linux bin下面,能够直接使用(固然也可使用编译完的源码包./redis-3.0.5/src/下面的脚本)
将redis源码包的配置文件复制到7000/
cp redis-3.0.5/redis.conf 7000/
vi 7000/redis.confphp
修改配置文件中的下面选项html
port 7000node
daemonize yeslinux
cluster-enabled yesgit
cluster-config-file nodes-7000.confgithub
cluster-node-timeout 5000redis
appendonly yes数据库
将修改好的7000/redis.conf文件复制到其余文件夹(修改redis.conf 端口port为对于的port)
cp 7000/redis.conf 7001/
cp 7000/redis.conf 7002/
cp 7000/redis.conf 7003/
cp 7000/redis.conf 7004/
cp 7000/redis.conf 7005/
注:7001文件夹下面的redis.conf将端口修改成7001,其余依次类推。缓存
分别启动6个redis实例:ruby
redis-server ./7000/redis.conf
redis-server ./7001/redis.conf
redis-server ./7002/redis.conf
redis-server ./7003/redis.conf
redis-server ./7004/redis.conf
redis-server ./7005/redis.conf
redis-server ./7000/redis.conf && redis-server ./7001/redis.conf && redis-server ./7002/redis.conf && redis-server ./7003/redis.conf && redis-server ./7004/redis.conf&& redis-server ./7005/redis.conf
使用ruby脚本redis-trib.rb执行redis的建立集群命令建立集群
#redis3.0.5源码包中的redis-trib.rb的create子命令构建集群
其中参数--replicas
则指定了Redis Cluster中的每一个Master节点配备几个Slave节点
其中节点角色由顺序决定,先master以后是slave
使用redis-trib.rb脚本必须有ruby环境,及redis的ruby接口。
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis(网速不行则使用淘宝的gem源:gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/)
建立redis集群:
./redis-3.0.5/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
./redis-3.0.5/src/redis-trib.rb create --replicas 1 172.31.2.32:7000 172.31.2.32:7001 172.31.2.32:7002 172.31.2.32:7003 172.31.2.32:7004 172.31.2.32:7005
./redis-3.0.5/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
这个命令在这里用于建立一个新的集群, 选项--replicas 1
表示咱们但愿为集群中的每一个主节点建立一个从节点。
以后跟着的其余参数则是这个集群实例的地址列表,3个master3个slave
redis-trib 会打印出一份预想中的配置给你看, 若是你以为没问题的话, 就能够输入 yes , redis-trib 就会将这份配置应用到集群当中,让各个节点开始互相通信,最后能够获得以下信息:
[OK] All 16384 slots covered
这表示集群中的 16384 个槽都有至少一个主节点在处理, 集群运做正常。
测试redis-cluster:
slaveof配置项。
./redis-trib.rb create --replicas添加主从节点???
注:若报上述错误,则使用redis-cli -c -p 7000端口链接,使用dbsize命令,若是不为零,则使用flushall命令清零。若是报是slave不能清零,
则找到该slave对应的master,执行上述操做。知道dbsize为0.
redis-cli -c -p 7000 shutdown && redis-cli -c -p 7001 shutdown && redis-cli -c -p 7002 shutdown && redis-cli -c -p 7003 shutdown && redis-cli -c -p 7004 shutdown &&redis-cli -c -p 7005 shutdown
redis-server ./7000/redis.conf && redis-server ./7001/redis.conf && redis-server ./7002/redis.conf && redis-server ./7003/redis.conf && redis-server ./7004/redis.conf&& redis-server ./7005/redis.conf
./redis-3.0.5/src/redis-trib.rb create --replicas 1 172.31.2.32:7000 172.31.2.32:7001 172.31.2.32:7002 172.31.2.32:7003 172.31.2.32:7004 172.31.2.32:7005
注:del-node以前须要将该节点./redis-trib.rb reshard 127.0.0.1:7003到其余节点,只有空的实例才能被删除。
若是使用keys * 查询到结果为空,说明当前实例保存数据为空,可以使用del-node命令移除。
将127.0.0.1::7000关掉:redis-cli -c -p 7000 shutdown
更多关于redis-cli操做请参考:http://blog.csdn.net/tantexian/article/details/49924873
接下来再172.31.2.33上面从新启动redis-server来说该节点添加到上述建立好的172.31.2.32的redis集群中。
第一步仍是跟上述步骤同样,安装redis基础环境。
将172.31.2.33上面新跑的实例*:8000 及*:8001加入到集群中来。
cluster集群相关命令,更多redis相关命令见文档:http://redis.readthedocs.org/en/latest/
真是场景的redis集群环境部署:
建议至少三台机器:(其实redis.server服务时PING-PONG无中心架构,且,能够在同一台机器上面启动多个master及slave)
node1:172.31.2.31
node2:172.31.2.32
node3:172.31.2.33
三组一主两从设计:
172.31.2.31:7000(主) 172.31.2.32:7000(从) 172.31.2.33:7000(从)
172.31.2.32:7001(主) 172.31.2.31:7001(从) 172.31.2.33:7001(从)
172.31.2.33:7002(主) 172.31.2.31:7002(从) 172.31.2.32:7002(从)
这样就达到了比较高的稳定性。
jedis使用:
https://github.com/xetorthio/jedis
关于redis性能测试:
http://www.runoob.com/redis/redis-benchmarks.html
redis主从复制: