Redis 叢集方法

拿客_三產發表於2017-06-27

配置 Redis

為了方便管理,我們將統一的配置放到 cluster.conf 檔案中,別的檔案引入該檔案:

bind 127.0.0.1
protected-mode yes
port 6370
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "./redis.log"
databases 100
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename master.rdb
dir ./
#slaveof 10.1.1.105 6379
#masterauth  admin123
#將次 Redis 例項中啟用 Redis Cluster 支援。否則,例項通常作為獨立例項啟動 
cluster-enabled yes  
#這是叢集中的節點能夠失聯的最大時間,超過這個時間,該節點就會被認為故障。 
cluster-node-timeout 15000 
#此配置檔案不能人工編輯,它是叢集節點自動維護的檔案,主要用於記錄叢集中有哪些節點、他們的狀態以及一些持久化引數等,方便在重啟時恢復這些狀態
cluster-config-file master-cluster.config
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
#密碼,這裡可以不設定,設定的話啟動叢集管理的時候會報錯,下文會講解錯誤的解決方法
requirepass admin123
appendonly no
appendfilename "appendonly6370.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

檔案目錄如下:

└── cluster
    ├── 6370
    │   ├── 6370.conf
    │   ├── master-cluster.config
    │   ├── master.rdb
    │   └── redis.log
    ├── 6371
    │   ├── 6371.conf
    │   ├── master-cluster.config
    │   ├── master.rdb
    │   └── redis.log
    ├── 6372
    │   ├── 6372.conf
    │   ├── master-cluster.config
    │   └── redis.log
    ├── 6373
    │   ├── 6373.conf
    │   ├── master-cluster.config
    │   └── redis.log
    ├── 6374
    │   ├── 6374.conf
    │   ├── master-cluster.config
    │   └── redis.log
    ├── 6375
    │   ├── 6375.conf
    │   ├── master-cluster.config
    │   └── redis.log
    └── cluster.conf

其中資料夾名為埠號,我們只拿 6370 看一眼看(別的只是修改一下埠號):

include /root/redis/cluster/cluster.conf
port 6370

怎麼樣是不是很簡單呢?

我們在每個埠的目錄下啟動各自的 redis :

[coderknock 6370]# redis-server 6370.conf 

按照上面的模式我們將設定的 6 個 redis 啟動。

安裝 ruby

[root@iZ28glf0u1jZ ~]# sudo yum install ruby
Loaded plugins: security
Setting up Install Process
base                                                                                                                 | 3.7 kB     00:00
elrepo                                                                                                               | 2.9 kB     00:00
epel                                                                                                                 | 4.3 kB     00:00
extras                                                                                                               | 3.3 kB     00:00
fpco                                                                                                                 | 2.9 kB     00:00
updates                                                                                                              | 3.4 kB     00:00
updates/primary_db                                                                                                   | 817 kB     00:00
Resolving Dependencies
--> Running transaction check
---> Package ruby.i686 0:1.8.7.374-5.el6 will be installed
--> Processing Dependency: ruby-libs = 1.8.7.374-5.el6 for package: ruby-1.8.7.374-5.el6.i686
--> Processing Dependency: libruby.so.1.8 for package: ruby-1.8.7.374-5.el6.i686
--> Running transaction check
---> Package ruby-libs.i686 0:1.8.7.374-5.el6 will be installed
--> Processing Dependency: libreadline.so.5 for package: ruby-libs-1.8.7.374-5.el6.i686
--> Running transaction check
---> Package compat-readline5.i686 0:5.2-17.1.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
 Package                                Arch                       Version                                 Repository                  Size
============================================================================================================================================
Installing:
 ruby                                   i686                       1.8.7.374-5.el6                         base                       538 k
Installing for dependencies:
 compat-readline5                       i686                       5.2-17.1.el6                            base                       128 k
 ruby-libs                              i686                       1.8.7.374-5.el6                         base                       1.6 M

Transaction Summary
============================================================================================================================================
Install       3 Package(s)

Total download size: 2.3 M
Installed size: 7.8 M
Is this ok [y/N]: 也
Is this ok [y/N]: y
Downloading Packages:
(1/3): compat-readline5-5.2-17.1.el6.i686.rpm                                                                        | 128 kB     00:00
(2/3): ruby-1.8.7.374-5.el6.i686.rpm                                                                                 | 538 kB     00:00
(3/3): ruby-libs-1.8.7.374-5.el6.i686.rpm                                                                            | 1.6 MB     00:01
--------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                       1.1 MB/s | 2.3 MB     00:02
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : compat-readline5-5.2-17.1.el6.i686                                                                                       1/3
  Installing : ruby-libs-1.8.7.374-5.el6.i686                                                                                           2/3
  Installing : ruby-1.8.7.374-5.el6.i686                                                                                                3/3
  Verifying  : compat-readline5-5.2-17.1.el6.i686                                                                                       1/3
  Verifying  : ruby-1.8.7.374-5.el6.i686                                                                                                2/3
  Verifying  : ruby-libs-1.8.7.374-5.el6.i686                                                                                           3/3

Installed:
  ruby.i686 0:1.8.7.374-5.el6

Dependency Installed:
  compat-readline5.i686 0:5.2-17.1.el6                                   ruby-libs.i686 0:1.8.7.374-5.el6

Complete!

安裝 rubygems

[root@iZ28glf0u1jZ ~]# yum -y install ruby rubygems
Loaded plugins: security
Setting up Install Process
Package ruby-1.8.7.374-5.el6.i686 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package rubygems.noarch 0:1.3.7-5.el6 will be installed
--> Processing Dependency: ruby-rdoc for package: rubygems-1.3.7-5.el6.noarch
--> Running transaction check
---> Package ruby-rdoc.i686 0:1.8.7.374-5.el6 will be installed
--> Processing Dependency: ruby-irb = 1.8.7.374-5.el6 for package: ruby-rdoc-1.8.7.374-5.el6.i686
--> Running transaction check
---> Package ruby-irb.i686 0:1.8.7.374-5.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================================================
 Package                                         Arch                                         Version                                                 Repository                                  Size
=======================================================================================================================================================================================================
Installing:
 rubygems                                        noarch                                       1.3.7-5.el6                                             base                                       207 k
Installing for dependencies:
 ruby-irb                                        i686                                         1.8.7.374-5.el6                                         base                                       318 k
 ruby-rdoc                                       i686                                         1.8.7.374-5.el6                                         base                                       381 k

Transaction Summary
=======================================================================================================================================================================================================
Install       3 Package(s)

Total download size: 905 k
Installed size: 3.0 M
Downloading Packages:
(1/3): ruby-irb-1.8.7.374-5.el6.i686.rpm                                                                                                                                        | 318 kB     00:00     
(2/3): ruby-rdoc-1.8.7.374-5.el6.i686.rpm                                                                                                                                       | 381 kB     00:00     
(3/3): rubygems-1.3.7-5.el6.noarch.rpm                                                                                                                                          | 207 kB     00:00     
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                  1.7 MB/s | 905 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : ruby-irb-1.8.7.374-5.el6.i686                                                                                                                                                       1/3 
  Installing : ruby-rdoc-1.8.7.374-5.el6.i686                                                                                                                                                      2/3 
  Installing : rubygems-1.3.7-5.el6.noarch                                                                                                                                                         3/3 
  Verifying  : ruby-rdoc-1.8.7.374-5.el6.i686                                                                                                                                                      1/3 
  Verifying  : rubygems-1.3.7-5.el6.noarch                                                                                                                                                         2/3 
  Verifying  : ruby-irb-1.8.7.374-5.el6.i686                                                                                                                                                       3/3 

Installed:
  rubygems.noarch 0:1.3.7-5.el6                                                                                                                                                                        

Dependency Installed:
  ruby-irb.i686 0:1.8.7.374-5.el6                                                                   ruby-rdoc.i686 0:1.8.7.374-5.el6                                                                  

Complete!

安裝配置叢集管理

安裝 ruby 下的 redis 管理工具

gem install redis

啟動叢集管理

找到 redis 原始碼包中 src 下的 redis-trib.rb 或者下載一個該檔案也可以

[coderknock redis]# /root/redis-3.2.0/src/redis-trib.rb create --replicas 1 127.0.0.1:6370 127.0.0.1:6371 127.0.0.1:6372 127.0.0.1:6373 127.0.0.1:6374 127.0.0.1:6375
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6370
127.0.0.1:6371
127.0.0.1:6372
Adding replica 127.0.0.1:6373 to 127.0.0.1:6370
Adding replica 127.0.0.1:6374 to 127.0.0.1:6371
Adding replica 127.0.0.1:6375 to 127.0.0.1:6372
M: f9acf4403c84c23ee9a79952e1f26a02cca40647 127.0.0.1:6370
   slots:0-5460 (5461 slots) master
M: f070ba8fedc4e9240c35b33684554080d0e2f7da 127.0.0.1:6371
   slots:5461-10922 (5462 slots) master
M: ae1502fbdfe2823dff2836ff61664da5affbd7e4 127.0.0.1:6372
   slots:10923-16383 (5461 slots) master
S: b1c4a2eeff66c8d5f1997d3a3210120552f7bc72 127.0.0.1:6373
   replicates f9acf4403c84c23ee9a79952e1f26a02cca40647
S: 8d661ae944b842b21739aff41ce7005ed30ccaad 127.0.0.1:6374
   replicates f070ba8fedc4e9240c35b33684554080d0e2f7da
S: 429fea95c1428e6cfa39a3a0b0d67fa8e2c606d2 127.0.0.1:6375
   replicates ae1502fbdfe2823dff2836ff61664da5affbd7e4
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:6370)
M: f9acf4403c84c23ee9a79952e1f26a02cca40647 127.0.0.1:6370
   slots:0-5460 (5461 slots) master
M: f070ba8fedc4e9240c35b33684554080d0e2f7da 127.0.0.1:6371
   slots:5461-10922 (5462 slots) master
M: ae1502fbdfe2823dff2836ff61664da5affbd7e4 127.0.0.1:6372
   slots:10923-16383 (5461 slots) master
M: b1c4a2eeff66c8d5f1997d3a3210120552f7bc72 127.0.0.1:6373
   slots: (0 slots) master
   replicates f9acf4403c84c23ee9a79952e1f26a02cca40647
M: 8d661ae944b842b21739aff41ce7005ed30ccaad 127.0.0.1:6374
   slots: (0 slots) master
   replicates f070ba8fedc4e9240c35b33684554080d0e2f7da
M: 429fea95c1428e6cfa39a3a0b0d67fa8e2c606d2 127.0.0.1:6375
   slots: (0 slots) master
   replicates ae1502fbdfe2823dff2836ff61664da5affbd7e4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

這樣就啟動成功了。

[ERR] Sorry, can`t connect to node 127.0.0.1:6370

出現這個可能的原因是 ruby 版本太低,升級之前安裝的那幾個東西。還有可能就是設定了 redis 密碼的緣故。

找到 usr/lib/ruby/gems/1.8/gems/redis-3.3.3/lib/redis,編輯 client.rb:

require "redis/errors"
require "socket"
require "cgi"

class Redis
  class Client

    DEFAULTS = {
      :url => lambda { ENV["REDIS_URL"] },
      :scheme => "redis",
      :host => "127.0.0.1",
      :port => 6379,
      :path => nil,
      :timeout => 5.0,
      :password => "admin123",//這裡設定你的密碼
      :db => 0,
      :driver => nil,
      :id => nil,
      :tcp_keepalive => 0,
      :reconnect_attempts => 1,
      :inherit_socket => false
    } 
          
    def options
      Marshal.load(Marshal.dump(@options))
    end  
        
    def scheme
      @options[:scheme] 
    end

    def host
      @options[:host] 
    end

    def port
      @options[:port]
    end
    
    def path
      @options[:path]

然後就可以正常啟動了。

測試

叢集中使用 redis-cli 應該 使用 -c 引數啟動,並且不支援 select 等操作(具體可以檢視官網)

[coderknock redis]# redis-cli -c -p 6371 -a admin123
127.0.0.1:6371> keys *
(empty list or set)
127.0.0.1:6371> get admin
-> Redirected to slot [1358] located at 127.0.0.1:6370
"123"
127.0.0.1:6370> set admin aa123
OK
127.0.0.1:6370> get admin
"aa123"
127.0.0.1:6370> quit
[coderknock redis]# redis-cli -c -p 6370 -a admin123
127.0.0.1:6370> get admin
"aa123"
127.0.0.1:6370> 
[coderknock redis]# redis-cli -c -p 6373 -a admin123
127.0.0.1:6373> get admin
-> Redirected to slot [1358] located at 127.0.0.1:6370
"aa123"
127.0.0.1:6370> set admin 73
OK
127.0.0.1:6370> get admin
"73"
127.0.0.1:6370> quit
[coderknock redis]# redis-cli -c -p 6370 -a admin123
127.0.0.1:6370> get admin
"73"

Java Jedis 訪問

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

/**
 * <p></p>
 *
 * @author 三產
 * @version 1.0
 * @date 2017-05-25
 * @QQGroup 213732117
 * @website http://www.coderknock.com
 * @copyright Copyright 2017 拿客 coderknock.com  All rights reserved.
 * @since JDK 1.8
 */
public class Test {
    /**
     * 獲取配置的主從 Redis
     *
     * @param serverInfo
     * @return
     */
    private static Set<HostAndPort> getClusterInfo(String serverInfo) {
        Set<HostAndPort> set = new HashSet<HostAndPort>();
        if (serverInfo == null || "".equals(serverInfo.length())) {
            throw new RuntimeException("The serverInfo can not be empty");
        }
        String ipPort[] = serverInfo.split(",");
        int len = ipPort.length;
        for (int i = 0; i < len; i++) {
            String server[] = ipPort[i].split(":");
            set.add(new HostAndPort(server[0], Integer.parseInt(server[1])));
        }
        return set;
    }


    public static void main(String[] args) {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(20);
        String redis_url = "127.0.0.1:6370,127.0.0.1:6371,127.0.0.1:6372,127.0.0.1:6373";
        String auth = "admin123";
        int timeout = 1000;
        Set<HostAndPort> jedisClusterNodes = getClusterInfo(redis_url);
        JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, timeout, config, auth);
        System.out.println(jedisCluster.get(1, "admin"));
    }
    //Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR This instance has cluster support disabled
    //Exception in thread "main" redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster
}

我是廣告

本人的直播課程在 7 月份就要開始了,希望小夥伴們支援一下,現在報名有優惠噢

https://segmentfault.com/l/15…

https://segmentfault.com/l/15…

相關文章