歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Redis 3.0集群

Redis 3.0集群

日期:2017/2/27 15:52:58   编辑:Linux教程

概述

這裡只記錄操作步驟和集群測試,保證快速搭建集群環境。具體原理請查閱官方文檔(中文版):

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

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

准備工作

  • 下載並編譯redis

  • 創建6個以端口號為名字的目錄,7000~7005,修改redis.conf的端口號

  • 修改配置:

    cluster-enabled yes
    cluster-config-file nodes-6379.conf
    cluster-node-timeout 15000
  • 分別啟動這6個服務:./redis-server redis.conf

安裝Ruby環境

sudo apt-get install ruby
安裝ruby的redis接口:
sudo gem install redis

如果不安裝,後面執行腳本會報錯:

custom_require.rb:36:in `require': cannot load such file -- redis (LoadError)

from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'

from ./redis-trib.rb:25:in `<main>'

啟動集群

進入到redis的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-trib.rb 程序的命令是 create , 這表示我們希望創建一個新的集群。

  • 選項 --replicas 1 表示我們希望為集群中的每個主節點創建一個從節點。

  • 之後跟著的其他參數則是實例的地址列表, 我們希望程序使用這些地址所指示的實例來創建新集群。

查看集群目前狀況

$ redis-cli -c -p 7000
127.0.0.1:7000> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:0

cluster_stats_messages_sent:8770

cluster_stats_messages_received:8770

測試存值取值

127.0.0.1:7000> set foo bar

-> Redirected to slot [12182] located at 127.0.0.1:7002

OK

127.0.0.1:7002> set hello world

-> Redirected to slot [866] located at 127.0.0.1:7000

OK

127.0.0.1:7000> get foo

-> Redirected to slot [12182] located at 127.0.0.1:7002

"bar"

127.0.0.1:7000> get hello

-> Redirected to slot [866] located at 127.0.0.1:7000

"world"

Copyright © Linux教程網 All Rights Reserved