以前使用Sprintboot链接redis使用都很正常,但是最近申请了一套服务器,在Sprintboot链接新redis集群时,Sprintboot日志报链接redis集群失败,日志里显示奇怪的redis IP,例如127.0.0.1。
2020-08-05 16:46:02.388 [http-nio-5252-exec-2] DEBUG io.lettuce.core.EpollProvider - Starting without optional epoll library
2020-08-05 16:46:02.391 [http-nio-5252-exec-2] DEBUG io.lettuce.core.KqueueProvider - Starting without optional kqueue library
2020-08-05 16:46:02.393 [http-nio-5252-exec-2] DEBUG i.l.c.r.DefaultEventLoopGroupProvider - Allocating executor io.netty.channel.nio.NioEventLoopGroup
2020-08-05 16:46:02.393 [http-nio-5252-exec-2] DEBUG i.l.c.r.DefaultEventLoopGroupProvider - Creating executor io.netty.channel.nio.NioEventLoopGroup
2020-08-05 16:46:02.394 [http-nio-5252-exec-2] DEBUG i.n.c.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 8
2020-08-05 16:46:02.399 [http-nio-5252-exec-2] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
2020-08-05 16:46:02.399 [http-nio-5252-exec-2] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
2020-08-05 16:46:02.402 [http-nio-5252-exec-2] DEBUG i.l.c.r.DefaultEventLoopGroupProvider - Adding reference to io.netty.channel.nio.NioEventLoopGroup@49610338, existing ref count 0
2020-08-05 16:46:02.422 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9001
2020-08-05 16:46:02.426 [http-nio-5252-exec-2] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 150361 (auto-detected)
2020-08-05 16:46:02.429 [http-nio-5252-exec-2] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
2020-08-05 16:46:02.429 [http-nio-5252-exec-2] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
2020-08-05 16:46:02.431 [http-nio-5252-exec-2] DEBUG io.netty.util.NetUtil - Loopback interface: lo (lo, 127.0.0.1)
2020-08-05 16:46:02.432 [http-nio-5252-exec-2] DEBUG io.netty.util.NetUtil - /proc/sys/net/core/somaxconn: 4096
2020-08-05 16:46:02.432 [http-nio-5252-exec-2] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 00:50:56:ff:fe:85:3d:80 (auto-detected)
2020-08-05 16:46:02.462 [http-nio-5252-exec-2] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
2020-08-05 16:46:02.462 [http-nio-5252-exec-2] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
2020-08-05 16:46:02.462 [http-nio-5252-exec-2] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
2020-08-05 16:46:02.474 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9002
2020-08-05 16:46:02.475 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9003
2020-08-05 16:46:02.485 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9004
2020-08-05 16:46:02.486 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9005
2020-08-05 16:46:02.486 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9006
2020-08-05 16:46:02.489 [lettuce-nioEventLoop-4-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
2020-08-05 16:46:02.489 [lettuce-nioEventLoop-4-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
2020-08-05 16:46:02.489 [lettuce-nioEventLoop-4-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
2020-08-05 16:46:02.489 [lettuce-nioEventLoop-4-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
2020-08-05 16:46:02.489 [lettuce-nioEventLoop-4-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.delayedQueue.ratio: 8
2020-08-05 16:46:02.499 [lettuce-nioEventLoop-4-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkAccessible: true
2020-08-05 16:46:02.499 [lettuce-nioEventLoop-4-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkBounds: true
2020-08-05 16:46:02.499 [lettuce-nioEventLoop-4-1] DEBUG i.n.u.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@3424791d
2020-08-05 16:46:02.531 [lettuce-nioEventLoop-4-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9002: Success
2020-08-05 16:46:02.532 [lettuce-nioEventLoop-4-4] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9004: Success
2020-08-05 16:46:02.533 [lettuce-nioEventLoop-4-3] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9003: Success
2020-08-05 16:46:02.535 [lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9001: Success
2020-08-05 16:46:02.539 [lettuce-nioEventLoop-4-4] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=CLIENT, output=StatusOutput [output=null, error='null'], commandType=io.lett
uce.core.protocol.Command]
2020-08-05 16:46:02.539 [lettuce-nioEventLoop-4-3] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=CLIENT, output=StatusOutput [output=null, error='null'], commandType=io.lett
uce.core.protocol.Command]
2020-08-05 16:46:02.540 [lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=CLIENT, output=StatusOutput [output=null, error='null'], commandType=io.lett
uce.core.protocol.Command]
2020-08-05 16:46:02.541 [lettuce-nioEventLoop-4-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=CLIENT, output=StatusOutput [output=null, error='null'], commandType=io.lett
uce.core.protocol.Command]
2020-08-05 16:46:02.553 [lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9005: Success
2020-08-05 16:46:02.553 [lettuce-nioEventLoop-4-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9006: Success
2020-08-05 16:46:02.561 [lettuce-nioEventLoop-4-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=CLIENT, output=StatusOutput [output=null, error='null'], commandType=io.lett
uce.core.protocol.Command]
2020-08-05 16:46:02.561 [lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=CLIENT, output=StatusOutput [output=null, error='null'], commandType=io.lett
uce.core.protocol.Command]
2020-08-05 16:46:02.563 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=CLUSTER, output=StatusOutput [output=null, error='null'], commandType=io.le
ttuce.core.protocol.Command]
2020-08-05 16:46:02.565 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=CLUSTER, output=StatusOutput [output=null, error='null'], commandType=io.le
ttuce.core.protocol.Command]
2020-08-05 16:46:02.566 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=CLUSTER, output=StatusOutput [output=null, error='null'], commandType=io.le
ttuce.core.protocol.Command]
2020-08-05 16:46:02.573 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=CLUSTER, output=StatusOutput [output=null, error='null'], commandType=io.le
ttuce.core.protocol.Command]
2020-08-05 16:46:02.574 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=CLUSTER, output=StatusOutput [output=null, error='null'], commandType=io.le
ttuce.core.protocol.Command]
2020-08-05 16:46:02.574 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=CLUSTER, output=StatusOutput [output=null, error='null'], commandType=io.le
ttuce.core.protocol.Command]
2020-08-05 16:46:02.575 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=INFO, output=StatusOutput [output=null, error='null'], commandType=io.lettu
ce.core.protocol.Command]
2020-08-05 16:46:02.575 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=INFO, output=StatusOutput [output=null, error='null'], commandType=io.lettu
ce.core.protocol.Command]
2020-08-05 16:46:02.575 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=INFO, output=StatusOutput [output=null, error='null'], commandType=io.lettu
ce.core.protocol.Command]
2020-08-05 16:46:02.577 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=INFO, output=StatusOutput [output=null, error='null'], commandType=io.lettu
ce.core.protocol.Command]
2020-08-05 16:46:02.585 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=INFO, output=StatusOutput [output=null, error='null'], commandType=io.lettu
ce.core.protocol.Command]
2020-08-05 16:46:02.585 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command TimedAsyncCommand [type=INFO, output=StatusOutput [output=null, error='null'], commandType=io.lettu
ce.core.protocol.Command]
2020-08-05 16:46:02.627 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9006
2020-08-05 16:46:02.633 [lettuce-nioEventLoop-4-3] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9006: Success
2020-08-05 16:46:02.635 [lettuce-nioEventLoop-4-3] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=CLIENT, output=StatusOutput [output=null, error='null'], commandType=io.lett
uce.core.protocol.Command]
2020-08-05 16:46:02.635 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9003
2020-08-05 16:46:02.636 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 127.0.0.1:9006
2020-08-05 16:46:02.636 [lettuce-nioEventLoop-4-4] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9003: Success
2020-08-05 16:46:02.637 [lettuce-nioEventLoop-4-4] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=CLIENT, output=StatusOutput [output=null, error='null'], commandType=io.lett
uce.core.protocol.Command]
2020-08-05 16:46:02.638 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9002
2020-08-05 16:46:02.639 [http-nio-5252-exec-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 127.0.0.1:9005
2020-08-05 16:46:02.640 [lettuce-nioEventLoop-4-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9002: Success
2020-08-05 16:46:02.640 [lettuce-nioEventLoop-4-2] DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=CLIENT, output=StatusOutput [output=null, error='null'], commandType=io.lett
uce.core.protocol.Command]
2020-08-05 16:46:02.643 [lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 127.0.0.1:9006: 127.0.0.1:9006
io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: /127.0.0.1:9006
io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: /127.0.0.1:9006
Caused by: java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:702)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
2020-08-05 16:46:02.643 [lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.RedisChannelHandler - close()
2020-08-05 16:46:02.643 [lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.RedisChannelHandler - closeAsync()
2020-08-05 16:46:02.644 [lettuce-nioEventLoop-4-1] DEBUG i.l.c.c.t.ClusterTopologyRefresh - Unable to connect to [127.0.0.1:9006]: Connection refused: /127.0.0.1:9006
io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: /127.0.0.1:9006
Caused by: java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:702)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
2020-08-05 16:46:02.646 [lettuce-nioEventLoop-4-3] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 127.0.0.1:9005: 127.0.0.1:9005
io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: /127.0.0.1:9005
Caused by: java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:702)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
2020-08-05 16:46:02.647 [lettuce-nioEventLoop-4-3] DEBUG io.lettuce.core.RedisChannelHandler - close()
2020-08-05 16:46:02.647 [lettuce-nioEventLoop-4-3] DEBUG io.lettuce.core.RedisChannelHandler - closeAsync()
本来是打算链接redis集群:
10.10.10.142:9001,10.10.10.142:9002,10.10.10.142:9003,10.10.10.142:9004,10.10.10.142:9005,10.10.10.142:9006
日志显示也链接成功了:
2020-08-05 16:46:02.531 [lettuce-nioEventLoop-4-2] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9002: Success
2020-08-05 16:46:02.532 [lettuce-nioEventLoop-4-4] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9004: Success
2020-08-05 16:46:02.533 [lettuce-nioEventLoop-4-3] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9003: Success
2020-08-05 16:46:02.535 [lettuce-nioEventLoop-4-1] DEBUG io.lettuce.core.RedisClient - Connecting to Redis at 10.10.10.142:9001: Success
为什么会连接IP为127.0.0.1的redis集群呢?如下:
127.0.0.1:9001,127.0.0.1:9002,127.0.0.1:9003,127.0.0.1:9004,127.0.0.1:9005,127.0.0.1:9006
重要的是:但是端口是正确的。
登录redis机子,查看redis集群状态:
$ ../bin/redis-cli -p 9001 cluster nodes
53b81ff738ce1f0368ee2ebd2253de99982aaa6c 127.0.0.1:9002@19002 master - 0 1596607175841 2 connected 5461-10922
db99d0ec79054e58bdb6a2d8da2c29bd4cd7f439 127.0.0.1:9006@19006 slave ac8096ff06260d23b75bcb130edd1710a50ba25e 0 1596607177846 6 connected
b55b3d8c0828101ba500952ab9879488f7d871ae 127.0.0.1:9005@19005 slave cdf1a8f1475aca64a5050e310b6b3c6f79e6839d 0 1596607178848 5 connected
cdf1a8f1475aca64a5050e310b6b3c6f79e6839d 127.0.0.1:9003@19003 master - 0 1596607176842 3 connected 10923-16383
ac8096ff06260d23b75bcb130edd1710a50ba25e 127.0.0.1:9001@19001 myself,master - 0 1596607177000 1 connected 0-5460
ae33879808984f2e58a0f44dde7b1fe628e5fc21 127.0.0.1:9004@19004 slave 53b81ff738ce1f0368ee2ebd2253de99982aaa6c 0 1596607177000 4 connected
哈哈,这里居然出现了127.0.0.1信息。
于是立马关闭集群,清空data,再用IP 10.10.10.142创建集群:
./bin/redis-cli --cluster create 10.10.10.142:9001 10.10.10.142:9002 10.10.10.142:9003 10.10.10.142:9004 10.10.10.142:9005 10.10.10.142:9006 --cluster-replicas 1
查看集群状态:
$ ../bin/redis-cli -c -p 9001 cluster nodes
e08676e289e291f8dc85ad8d344e6f83f9c27b82 10.10.10.142:9005@19005 slave fc0af031e9399d61a9b1d49543c62dcce6535e8c 0 1596614376805 5 connected
ff664dc29addc758000b696d517c939af2dafbcd 10.10.10.142:9004@19004 slave e6fb1c309c8ef0ea036745f4355ef0c45220f275 0 1596614377806 4 connected
6ef689a2592463653cab7dac82aa2d5935d2daf4 10.10.10.142:9006@19006 slave 61ffc9d221519afb92fd721250abf177cb57df26 0 1596614375000 6 connected
e6fb1c309c8ef0ea036745f4355ef0c45220f275 10.10.10.142:9003@19003 master - 0 1596614374802 3 connected 10923-16383
fc0af031e9399d61a9b1d49543c62dcce6535e8c 10.10.10.142:9001@19001 myself,master - 0 1596614375000 1 connected 0-5460
61ffc9d221519afb92fd721250abf177cb57df26 10.10.10.142:9002@19002 master - 0 1596614376000 2 connected 5461-10922
可以看到,IP不是127.0.0.1了
再查看Sprintboot日志,没有出现链接redis集群报错了。
Sprintboot再链接到redis节点后,会更具redis节点获取redis cluster集群信息,这个时候你配置的redis信息以及被替换了,所以创建redis集群时最好使用真实IP。
如果觉得文章写得不错,能对你有帮助,可以扫描我的微信二维码请我喝咖啡哦~~哈哈~~