redis是個分佈式緩存,與傳統數據庫最大的優點,在於它的「分佈式」上。html
分佈式的優點:node
容易實現容量的擴展
git
數據的均等分佈github
很好的高可用性redis
redis 和memcached是分佈式緩存的兩款流行方案,他們之間的對比數據庫
redis | memcached | |
主從功能 |
Replication 支持緩存 主備自動切換 |
自己不支持,能夠經過客戶端本身實現 |
鍵值一致性 |
哈希槽 |
一致性哈希 |
集羣 |
服務端支持(可是beta版) unstable |
由客戶端實現 |
工具支持 |
提供自帶的工具(客戶端redis-cli,check-rdb...) |
較弱 |
數據存儲 |
支持 |
不支持,重啓數據丟失 |
本章主要內容bash
安裝一個三主的redis集羣環境。網絡
準備
因爲本人沒有接觸過ruby語言,在安裝以前作功課時,須要安裝ruby環境等,心理上有了點小負擔,擔憂遇到麻煩。後來,不出所料遇到了點麻煩。最後,仍是安裝成功了,回頭想一想,沒有什麼可擔憂的,無外乎失敗了重裝,真是年紀越大,膽子越小啊。我採用的方式,「兵來將擋,水來土掩」,有錯就解決錯,一步步安裝。藉助網絡的力量,逐個把問題擊破。
安裝ruby環境
1.安裝ruby
遇到的問題
未安裝ruby
版本太低
未安裝rubygem
gem源出錯
請參考
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