redis是个分布式缓存,与传统数据库最大的优势,在于它的“分布式”上。

分布式的优势:

  1. 容易实现容量的扩展

  2. 数据的均等分布

  3. 很好的高可用性


redis 和memcached是分布式缓存的两款流行方案,他们之间的对比


redismemcached
主从功能

Replication 支持

主备自动切换

本身不支持,可以通过客户端自己实现
键值一致性
哈希槽
一致性哈希
集群
服务端支持(但是beta版) unstable
由客户端实现
工具支持
提供自带的工具(客户端redis-cli,check-rdb...)
较弱
数据存储
支持
不支持,重启数据丢失

本章主要内容

安装一个三主的redis集群环境。

准备

由于本人没有接触过ruby语言,在安装之前做功课时,需要安装ruby环境等,心理上有了点小负担,担心遇到麻烦。后来,不出所料遇到了点麻烦。最后,还是安装成功了,回头想想,没有什么可担心的,无外乎失败了重装,真是年纪越大,胆子越小啊。我采用的方式,“兵来将挡,水来土掩”,有错就解决错,一步步安装。借助网络的力量,逐个把问题击破。

安装ruby环境


1.安装ruby

遇到的问题

未安装ruby

版本过低

未安装rubygem

gem源出错

请参考

http://blog.csdn.net/c77_cn/article/details/38227797

2. 编译redis cluster

https://github.com/antirez/redis/branches下载unstable版本。

编译
make
此时,在/usr/local/src/redis-unstable/src 目录下生成了支持cluster的redis-server


3.建立集群环境

1.建立目录
cp /usr/local/redis
mkdir cluster-test
cd cluster-test/
mkdir 7000
mkdir 7001
mkdir 7002
2.复制redis-server
 cp /usr/local/src/redis-unstable/src/redis-server  /usr/local/redis/cluster-test/7001
 cp /usr/local/src/redis-unstable/src/redis-server  /usr/local/redis/cluster-test/7000
 cp /usr/local/src/redis-unstable/src/redis-server  /usr/local/redis/cluster-test/7002
 #集群管理工具(为了方便,将他复制到合适位置)
 cp /usr/local/src/redis-unstable/src/redis-trib.rb /usr/local/redis/cluster-test

3.建立3个cluster 节点对应的redis.conf文件

[root@hadoop2 cluster-test]# vi 7000/redis.conf 
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
logfile "redis7000.log"

#这是最简单的集群参数

其他7001/redis.conf,7002.conf文件, 仅仅port和logfile参数不同,其他一样。

此时 7000, 7001,7002目录下,只有2个文件,分别是 redis-server,redis.conf

4. 启动(分别启动3个主节点)

  cd 7000
  ./redis-server ./redis.conf
  cd ../7001/
  ./redis-server ./redis.conf
  cd ../7002
  ./redis-server ./redis.conf

对应某个节点日志

26361:M 10 Sep 21:13:22.054 * No cluster configuration found, I'm 7f242cf2c7ce5d84103638c5017f2204e1509f4c
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.3 (00000000/0) 32 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 7001
 |    `-._   `._    /     _.-'    |     PID: 26361
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

26361:M 10 Sep 21:13:22.075 # Server started, Redis version 3.2.3
26361:M 10 Sep 21:13:22.075 * DB loaded from append only file: 0.000 seconds
26361:M 10 Sep 21:13:22.076 # I have keys for unassigned slot 5659. Taking responsibility for it.
26361:M 10 Sep 21:13:22.076 # I have keys for unassigned slot 9980. Taking responsibility for it.
26361:M 10 Sep 21:13:22.080 * The server is now ready to accept connections on port 7001
                          


5. 建立集群

 ./redis-trib.rb create  127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002

[root@hadoop2 cluster-test]# ./redis-trib.rb create  127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
>>> Creating cluster
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
M: 5ad201ebdf0553690295dcd06f89f15750e73989 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 7f242cf2c7ce5d84103638c5017f2204e1509f4c 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 8ea7b0976e4848653435e4e77af311a6ade330a8 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join.
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 5ad201ebdf0553690295dcd06f89f15750e73989 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   0 additional replica(s)
M: 8ea7b0976e4848653435e4e77af311a6ade330a8 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   0 additional replica(s)
M: 7f242cf2c7ce5d84103638c5017f2204e1509f4c 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

这时候主节点日志

26536:M 10 Sep 21:25:32.360 * DB loaded from append only file: 0.000 seconds
26536:M 10 Sep 21:25:32.361 * The server is now ready to accept connections on port 7000
26536:M 10 Sep 21:27:54.651 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH
26536:M 10 Sep 21:27:54.660 # IP address for this node updated to 127.0.0.1
26536:M 10 Sep 21:27:59.619 # Cluster state changed: ok

6.测试

[root@hadoop2 cluster-test]#  /usr/local/redis/bin/redis-cli  -c -p 7000
127.0.0.1:7000> keys *
(empty list or set)
127.0.0.1:7000> set title "cluster1"
OK
127.0.0.1:7000> set title1 "cluster1"
OK
127.0.0.1:7000> set title2 "cluster1"
-> Redirected to slot [14045] located at 127.0.0.1:7002
OK

127.0.0.1:7002> set title5 "cluster4"
-> Redirected to slot [1594] located at 127.0.0.1:7000
OK
127.0.0.1:7000> set title3 "cluster4"
-> Redirected to slot [9980] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get title
-> Redirected to slot [2217] located at 127.0.0.1:7000
"cluster1"
127.0.0.1:7000> get title2
-> Redirected to slot [14045] located at 127.0.0.1:7002
"cluster8111"
127.0.0.1:7002> get title3
-> Redirected to slot [9980] located at 127.0.0.1:7001
"cluster4"

#节点间来回重定向


到了这里,环境算是搭建好了。当然,这是最简单的集群了。以后随着演练的深入,也会对集群进行深入探讨。



遇到问题

可能遇到的错误(是第二次建立集群时报错)

ERR Slot 2217 is already busy (Redis::CommandError)

解决方法: (in all nodes did FLUSHALL and then CLUSTER RESET SOFT)


Either the node already knows other nodes

[root@hadoop2 cluster-test]# ./redis-trib.rb create  127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
>>> Creating cluster
[ERR] Node 127.0.0.1:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

解决方法:这是因为重复设置集群。

重启集群,发现数据丢失

集群创建完成后,当关机或者关闭集群再次启动的时候,一定要在node-conf所在的目录启动各个节点,

集群才能正常连接。

参考资源

http://blog.csdn.net/c77_cn/article/details/38227797

http://redis.io/topics/cluster-tutorial

http://redisdoc.com/topic/cluster-tutorial.html