前言

这也是之前碰到的一个问题, 使用 redisson 连接 redis 集群, 然后 报错如下

Illegal character in scheme name at index 0: []r[]e[]d[]i[]s[]:[]/[]/[]:[]7[]0[]0[]6[]

这里 来大致看一下这个问题

这个问题 大概是由于 集群未搭建成功导致的一个问题

错误信息如下

Caused by: java.lang.IllegalArgumentException: Illegal character in scheme name at index 0: []r[]e[]d[]i[]s[]:[]/[]/[]:[]7[]0[]0[]6[]
        at java.net.URI.create(URI.java:852) ~[na:1.8.0_291]
        at org.redisson.misc.URIBuilder.create(URIBuilder.java:37) ~[redisson-3.5.0.jar!/:na]
        at org.redisson.cluster.ClusterNodeInfo.setAddress(ClusterNodeInfo.java:56) ~[redisson-3.5.0.jar!/:na]
        at org.redisson.client.protocol.decoder.ClusterNodesDecoder.decode(ClusterNodesDecoder.java:50) ~[redisson-3.5.0.jar!/:na]
        at org.redisson.client.protocol.decoder.ClusterNodesDecoder.decode(ClusterNodesDecoder.java:35) ~[redisson-3.5.0.jar!/:na]
        at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:259) ~[redisson-3.5.0.jar!/:na]
        at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:103) ~[redisson-3.5.0.jar!/:na]
        at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:502) ~[netty-all-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366) ~[netty-all-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:278) ~[netty-all-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1408) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:930) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:682) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:617) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:534) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496) ~[netty-transport-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:906) ~[netty-common-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.36.Final.jar!/:4.1.36.Final]
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.36.Final.jar!/:4.1.36.Final]
        at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_291]
Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0: []r[]e[]d[]i[]s[]:[]/[]/[]:[]7[]0[]0[]6[]
        at java.net.URI$Parser.fail(URI.java:2845) ~[na:1.8.0_291]
        at java.net.URI$Parser.checkChars(URI.java:3018) ~[na:1.8.0_291]
        at java.net.URI$Parser.checkChar(URI.java:3028) ~[na:1.8.0_291]
        at java.net.URI$Parser.parse(URI.java:3044) ~[na:1.8.0_291]
        at java.net.URI.<init>(URI.java:588) ~[na:1.8.0_291]
        at java.net.URI.create(URI.java:850) ~[na:1.8.0_291]
        ... 25 common frames omitted

正常情况/异常情况

根据上下文的情况可以知道, 这里是处理 cluster nodes 的结果 

然后 cluster nodes 响应回来的数据如下, 在 params[1] 中是具体的主机端口连接信息 

24 连接 redis 集群 Illegal character in scheme name at index 0: []r[]e[]d[]i[]s[]:[]/[]/[]:[]7[]0[]0[]6_springboot

然后我们来看一下 异常情况

可以看到 cluster nodes 响应的节点信息 貌似存在问题, 没有 ip 的信息 

24 连接 redis 集群 Illegal character in scheme name at index 0: []r[]e[]d[]i[]s[]:[]/[]/[]:[]7[]0[]0[]6_java_02

然后 我们找一台机器, 然后 连接上集群, 然后 查看一下 cluster nodes, 可以看到的是 和 代码中拿到的是一样的, 然后 和 期望的正常情况 存在较大的差异

root@ubuntu:~/redis-cluster/redis-5.0.8# ./bin/redis-cli -p 7001 -c
127.0.0.1:7001> cluster nodes
3fbdd16e912aa29b8f28c5446f3bf8d1c78bc608 :7001@17001 myself,master - 0 0 0 connected

集群的启动方式存在错误

错误的启动命令如下, 主要的问题是在于 这里让我输入 yes, 我输入的是 y

导致 集群创建存在一些问题, 输入的时候 输入 yes 集群即可正常运行 

root@ubuntu:~/redis-cluster/redis-5.0.8# ./startCluster.sh
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.220.133:7005 to 192.168.220.133:7001
Adding replica 192.168.220.133:7006 to 192.168.220.133:7002
Adding replica 192.168.220.133:7004 to 192.168.220.133:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 3fbdd16e912aa29b8f28c5446f3bf8d1c78bc608 192.168.220.133:7001
   slots:[0-5460] (5461 slots) master
M: aeef16a6ce5788f31b48645122702040a0f7acfa 192.168.220.133:7002
   slots:[5461-10922] (5462 slots) master
M: 81c2648a8c6833887b6fbfebf01eeec1955784f6 192.168.220.133:7003
   slots:[10923-16383] (5461 slots) master
S: 803762bdf1fc4ac0cd60b7ea2ce9b54253e09cbf 192.168.220.133:7004
   replicates 81c2648a8c6833887b6fbfebf01eeec1955784f6
S: d24f8da74ddb23d0fdc0de4bc2725764132c73a5 192.168.220.133:7005
   replicates 3fbdd16e912aa29b8f28c5446f3bf8d1c78bc608
S: e6054a36ba9f85e1da866d2f291c2d1aaffe77ac 192.168.220.133:7006
   replicates aeef16a6ce5788f31b48645122702040a0f7acfa
Can I set the above configuration? (type 'yes' to accept): y
root@ubuntu:~/redis-cluster/redis-5.0.8# ps -ef | grep redis
999       4761  4741  0 Jun20 ?        00:01:59 redis-server *:6379
999      39736 39713  0 02:20 ?        00:00:09 redis-server *:6379
root     59621     1  0 04:14 ?        00:00:00 ./bin/redis-server *:7001 [cluster]
root     59623     1  0 04:14 ?        00:00:00 ./bin/redis-server *:7002 [cluster]
root     59628     1  0 04:14 ?        00:00:00 ./bin/redis-server *:7003 [cluster]
root     59633     1  0 04:14 ?        00:00:00 ./bin/redis-server *:7004 [cluster]
root     59638     1  0 04:14 ?        00:00:00 ./bin/redis-server *:7005 [cluster]
root     59643     1  0 04:14 ?        00:00:00 ./bin/redis-server *:7006 [cluster]
root     59666 39062  0 04:14 pts/1    00:00:00 grep --color=auto redis
root@ubuntu:~/redis-cluster/redis-5.0.8# ./bin/redis-cli -p 7001 -c
127.0.0.1:7001> cluster nodes
3fbdd16e912aa29b8f28c5446f3bf8d1c78bc608 :7001@17001 myself,master - 0 0 0 connected

startCluster 脚本如下 

./bin/redis-server ./redis_7001.conf
./bin/redis-server ./redis_7002.conf
./bin/redis-server ./redis_7003.conf
./bin/redis-server ./redis_7004.conf
./bin/redis-server ./redis_7005.conf
./bin/redis-server ./redis_7006.conf

./bin/redis-cli --cluster create 192.168.220.133:7001 192.168.220.133:7002 192.168.220.133:7003 192.168.220.133:7004 192.168.220.133:7005 192.168.220.133:7006 --cluster-replicas 1

问题的处理 

重新创建集群即可 

root@ubuntu:~/redis-cluster/redis-5.0.8# ./bin/redis-cli --cluster create 192.168.220.133:7001 192.168.220.133:7002 192.168.220.133:7003 192.168.220.133:7004 192.168.220.133:7005 192.168.220.133:7006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.220.133:7005 to 192.168.220.133:7001
Adding replica 192.168.220.133:7006 to 192.168.220.133:7002
Adding replica 192.168.220.133:7004 to 192.168.220.133:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 3fbdd16e912aa29b8f28c5446f3bf8d1c78bc608 192.168.220.133:7001
   slots:[0-5460] (5461 slots) master
M: aeef16a6ce5788f31b48645122702040a0f7acfa 192.168.220.133:7002
   slots:[5461-10922] (5462 slots) master
M: 81c2648a8c6833887b6fbfebf01eeec1955784f6 192.168.220.133:7003
   slots:[10923-16383] (5461 slots) master
S: 803762bdf1fc4ac0cd60b7ea2ce9b54253e09cbf 192.168.220.133:7004
   replicates 3fbdd16e912aa29b8f28c5446f3bf8d1c78bc608
S: d24f8da74ddb23d0fdc0de4bc2725764132c73a5 192.168.220.133:7005
   replicates aeef16a6ce5788f31b48645122702040a0f7acfa
S: e6054a36ba9f85e1da866d2f291c2d1aaffe77ac 192.168.220.133:7006
   replicates 81c2648a8c6833887b6fbfebf01eeec1955784f6
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 192.168.220.133:7001)
M: 3fbdd16e912aa29b8f28c5446f3bf8d1c78bc608 192.168.220.133:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 81c2648a8c6833887b6fbfebf01eeec1955784f6 192.168.220.133:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: d24f8da74ddb23d0fdc0de4bc2725764132c73a5 192.168.220.133:7005
   slots: (0 slots) slave
   replicates aeef16a6ce5788f31b48645122702040a0f7acfa
S: 803762bdf1fc4ac0cd60b7ea2ce9b54253e09cbf 192.168.220.133:7004
   slots: (0 slots) slave
   replicates 3fbdd16e912aa29b8f28c5446f3bf8d1c78bc608
S: e6054a36ba9f85e1da866d2f291c2d1aaffe77ac 192.168.220.133:7006
   slots: (0 slots) slave
   replicates 81c2648a8c6833887b6fbfebf01eeec1955784f6
M: aeef16a6ce5788f31b48645122702040a0f7acfa 192.168.220.133:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@ubuntu:~/redis-cluster/redis-5.0.8# bin/redis-cli -p 7001 -c
127.0.0.1:7001> cluster nodes
81c2648a8c6833887b6fbfebf01eeec1955784f6 192.168.220.133:7003@17003 master - 0 1687346484560 3 connected 10923-16383
d24f8da74ddb23d0fdc0de4bc2725764132c73a5 192.168.220.133:7005@17005 slave aeef16a6ce5788f31b48645122702040a0f7acfa 0 1687346482000 5 connected
803762bdf1fc4ac0cd60b7ea2ce9b54253e09cbf 192.168.220.133:7004@17004 slave 3fbdd16e912aa29b8f28c5446f3bf8d1c78bc608 0 1687346482527 4 connected
3fbdd16e912aa29b8f28c5446f3bf8d1c78bc608 192.168.220.133:7001@17001 myself,master - 0 1687346483000 1 connected 0-5460
e6054a36ba9f85e1da866d2f291c2d1aaffe77ac 192.168.220.133:7006@17006 slave 81c2648a8c6833887b6fbfebf01eeec1955784f6 0 1687346483549 6 connected
aeef16a6ce5788f31b48645122702040a0f7acfa 192.168.220.133:7002@17002 master - 0 1687346485570 2 connected 5461-10922
127.0.0.1:7001>

然后 应用即可正常连接

24 连接 redis 集群 Illegal character in scheme name at index 0: []r[]e[]d[]i[]s[]:[]/[]/[]:[]7[]0[]0[]6_.net_03