1. Java

Redis-Cluster

暑假期间看了一部分Redis教程,但是没有看完,昨天晚上1.5倍加速从头看完了Redis入门教程。

感觉应该差不多了,今天就来在WSL上模拟一下集群的搭建吧!

1 搭建流程

首先看一下我的redis位置

root@DESKTOP-JUOEVVQ:/usr/local/bin# ll
total 40160
drwxr-xr-x 1 root root    4096 Nov 27 00:51 ./
drwxr-xr-x 1 root root    4096 Nov 27 11:49 ../
-rw-r--r-- 1 root root     349 Nov 26 23:25 dump.rdb
-rwxr-xr-x 1 root root 4688472 Nov 26 18:37 redis-benchmark*
-rwxr-xr-x 1 root root 8782336 Nov 26 18:37 redis-check-aof*
-rwxr-xr-x 1 root root 8782336 Nov 26 18:37 redis-check-rdb*
-rwxr-xr-x 1 root root 5216680 Nov 26 18:37 redis-cli*
lrwxrwxrwx 1 root root      12 Nov 26 18:37 redis-sentinel -> redis-server*
-rwxr-xr-x 1 root root 8782336 Nov 26 18:37 redis-server*
-rw-r--r-- 1 root root   61796 Nov 26 18:48 redis.conf

1.1 创建Redis节点安装目录

root@DESKTOP-JUOEVVQ:/usr/local#mkdir redis_cluster

1.2 在redis_cluster目录下,创建7001-7006个文件夹下

root@DESKTOP-JUOEVVQ:/usr/local/redis_cluster#mkdir 7001 7002 7003 7004 7005 7006

1.3 并将redis.conf分别拷贝到7001-7006文件夹下

root@DESKTOP-JUOEVVQ:/usr/local/redis_cluster# cp ../bin/redis.conf  ./7001
root@DESKTOP-JUOEVVQ:/usr/local/redis_cluster# cp ../bin/redis.conf  ./7002
root@DESKTOP-JUOEVVQ:/usr/local/redis_cluster# cp ../bin/redis.conf  ./7003
root@DESKTOP-JUOEVVQ:/usr/local/redis_cluster# cp ../bin/redis.conf  ./7004
root@DESKTOP-JUOEVVQ:/usr/local/redis_cluster# cp ../bin/redis.conf  ./7005
root@DESKTOP-JUOEVVQ:/usr/local/redis_cluster# cp ../bin/redis.conf  ./7006

1.4 分别修改配置文件(这里举例7001节点,其他节点类比即可)

protected-mode no
# bind 127.0.0.1
port 7001
daemonize yes
pidfile /var/run/redis_7001.pid
cluster-enabled yes
cluster-config-file nodes-7001.conf

1.5 复制src到各个节点文件夹

root@DESKTOP-JUOEVVQ:/home/michael/redis-5.0.7# cp -r src/ /usr/local/redis_cluster/7001
root@DESKTOP-JUOEVVQ:/home/michael/redis-5.0.7# cp -r src/ /usr/local/redis_cluster/7002
root@DESKTOP-JUOEVVQ:/home/michael/redis-5.0.7# cp -r src/ /usr/local/redis_cluster/7003
root@DESKTOP-JUOEVVQ:/home/michael/redis-5.0.7# cp -r src/ /usr/local/redis_cluster/7004
root@DESKTOP-JUOEVVQ:/home/michael/redis-5.0.7# cp -r src/ /usr/local/redis_cluster/7005
root@DESKTOP-JUOEVVQ:/home/michael/redis-5.0.7# cp -r src/ /usr/local/redis_cluster/7006

1.6 启动各个节点(这里只给出7001,其他节点类比即可)

root@DESKTOP-JUOEVVQ:/usr/local/redis_cluster# ./7001/src/redis-server ./7001/redis.conf
270:C 27 Nov 2019 12:17:22.135 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
270:C 27 Nov 2019 12:17:22.135 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=270, just started       270:C 27 Nov 2019 12:17:22.135 # Configuration loaded

完成六个节点启动后ps看一下

root@DESKTOP-JUOEVVQ:/usr/local/redis_cluster# ps aux | grep redis
michael    221  0.0  0.0  52352  2236 ?        Ssl  10:26   0:00 ./redis-server *:6379
root       271  0.0  0.0  52352  5084 ?        Ssl  12:17   0:00 ./7001/src/redis-server *:7001 [cluster]
root       276  0.0  0.0  52352  5084 ?        Ssl  12:18   0:00 ./7002/src/redis-server *:7002 [cluster]
root       281  0.0  0.0  52352  5092 ?        Ssl  12:18   0:00 ./7003/src/redis-server *:7003 [cluster]
root       293  0.0  0.0  55424  5112 ?        Ssl  12:19   0:00 ./7004/src/redis-server *:7004 [cluster]
root       298  0.0  0.0  52352  5084 ?        Ssl  12:19   0:00 ./7005/src/redis-server *:7005 [cluster]
root       303  0.0  0.0  52352  5088 ?        Ssl  12:19   0:00 ./7006/src/redis-server *:7006 [cluster]
root       315  0.0  0.0  14804  1196 pts/0    S    12:20   0:00 grep --color=auto redis

1.7 复制redis-trib.rb到/usr/local/bin

root@DESKTOP-JUOEVVQ:/home/michael/redis-5.0.7/src# cp redis-trib.rb /usr/local/bin/

1.8 启动集群

root@DESKTOP-JUOEVVQ:/usr/local/bin# redis-trib.rb create --replicas 1 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 127.0.0.1:7006
/usr/bin/env: ‘ruby’: No such file or directory

报错没有ruby,安装

root@DESKTOP-JUOEVVQ:/usr/local/bin# apt install ruby

1.9 看来我看的教材老了唉

root@DESKTOP-JUOEVVQ:/usr/local/bin# redis-trib.rb create --replicas 1 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 127.0.0.1:7006
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.

All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.

Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]

Example:
redis-cli --cluster create 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 127.0.0.1:7006 --cluster-replicas 1

To get help about all subcommands, type:
redis-cli --cluster help

1.10 按提示来吧

root@DESKTOP-JUOEVVQ:/usr/local/bin# redis-cli --cluster create 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 127.0.0.1:7006 --cluster-replicas 1 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 081843660c373d95c6c69794da0bca23e3ef7a2a 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
M: d5d90d62310d8dbfa384e8bea4d0a1b3ae14035b 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
M: 42cf5706c29e387d79597ee954bfdf31e43b1bfc 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
S: 168711e677c7b6adef17e4779850f3387f86c41c 127.0.0.1:7004
   replicates 42cf5706c29e387d79597ee954bfdf31e43b1bfc
S: d04c5ecc5e79777afd32fe4fb33ddb82c938bc1c 127.0.0.1:7005
   replicates 081843660c373d95c6c69794da0bca23e3ef7a2a
S: bfbc2a40cc4fccf4c9f9727391d574e435c8cc05 127.0.0.1:7006
   replicates d5d90d62310d8dbfa384e8bea4d0a1b3ae14035b
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:7001)
M: 081843660c373d95c6c69794da0bca23e3ef7a2a 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: d5d90d62310d8dbfa384e8bea4d0a1b3ae14035b 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 42cf5706c29e387d79597ee954bfdf31e43b1bfc 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: bfbc2a40cc4fccf4c9f9727391d574e435c8cc05 127.0.0.1:7006
   slots: (0 slots) slave
   replicates d5d90d62310d8dbfa384e8bea4d0a1b3ae14035b
S: 168711e677c7b6adef17e4779850f3387f86c41c 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 42cf5706c29e387d79597ee954bfdf31e43b1bfc
S: d04c5ecc5e79777afd32fe4fb33ddb82c938bc1c 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 081843660c373d95c6c69794da0bca23e3ef7a2a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@DESKTOP-JUOEVVQ:/usr/local/bin#

成了???

root@DESKTOP-JUOEVVQ:/usr/local/redis_cluster# redis-cli -c -h 127.0.0.1 -p 7001 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7001> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7003
OK
127.0.0.1:7003> get foo
"bar"
127.0.0.1:7003>

这里要注意一下就是如果节点设置密码的话每个节点的配置文件还要修改一下下面的配置

masterauth 123456
requirepass 123456

这样集群的简单测试就完成了。接下来就是java代码测试一下连接集群

2 Java连接测试

这里遇到的主要问题就是jedisCluster.auth()用不了了

后来发现在构造里面有带密码的

public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout,
                      int maxAttempts, String password, final GenericObjectPoolConfig poolConfig) 

拿来试试

/**
     * Jedis Cluster Test Demo
     * Michael Jiang
     * 2019年11月27日 13点24分
     */
    @Test
    void testJedisCluser() throws IOException {
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
        nodes.add(new HostAndPort("192.168.235.1",7001));
        nodes.add(new HostAndPort("192.168.235.1",7002));
        nodes.add(new HostAndPort("192.168.235.1",7003));
        nodes.add(new HostAndPort("192.168.235.1",7004));
        nodes.add(new HostAndPort("192.168.235.1",7005));
        nodes.add(new HostAndPort("192.168.235.1",7006));

        // 因为到目前jedis还不兹磁直接设置带密码的集群访问,所以需要使用poolconfig
        JedisPoolConfig poolConfig = new JedisPoolConfig();

        JedisCluster jedisCluster = new JedisCluster(nodes,1000,1000,100,"123456",poolConfig);

        String foo = jedisCluster.get("foo");

        System.out.println(foo);

        jedisCluster.close();
    }

运行结果

bar

吼以了!