java - 无法使用 Lettuce 连接到本地 Redis 集群

标签 java redis lettuce

我正在像这样使用 Docker 运行 Redis 本地集群

docker run --env "IP=0.0.0.0" -p 7000-7007:7000-7007 -p 5000-5002:5000-5002 -p 6379:6379 grokzen/redis-cluster:5.0.3

通过类似telnet的方式连接时,我无法发送命令

telnet localhost 7000

但是当尝试使用以下代码连接生菜客户端时

    RedisURI redisURI = RedisURI.Builder.redis("localhost", 7000).build();
    RedisClusterClient client = RedisClusterClient.create(redisURI);
    StatefulRedisClusterConnection<String, String> connection = client.connect();
    RedisStringCommands sync = connection.sync();
    String value = (String) sync.get("key");

抛出以下异常

Exception in thread "main" io.lettuce.core.RedisException: Cannot retrieve initial cluster partitions from initial URIs [RedisURI [host='localhost', port=7000]]
at io.lettuce.core.cluster.RedisClusterClient.loadPartitions(RedisClusterClient.java:865)
at io.lettuce.core.cluster.RedisClusterClient.initializePartitions(RedisClusterClient.java:819)
at io.lettuce.core.cluster.RedisClusterClient.connect(RedisClusterClient.java:345)
at io.lettuce.core.cluster.RedisClusterClient.connect(RedisClusterClient.java:320)
at com.foo.DeleteMe.main(DeleteMe.java:22)
Caused by: io.lettuce.core.RedisConnectionException: Unable to establish a connection to Redis Cluster
at io.lettuce.core.cluster.topology.AsyncConnections.get(AsyncConnections.java:84)
at io.lettuce.core.cluster.topology.ClusterTopologyRefresh.loadViews(ClusterTopologyRefresh.java:68)
at io.lettuce.core.cluster.RedisClusterClient.doLoadPartitions(RedisClusterClient.java:871)
at io.lettuce.core.cluster.RedisClusterClient.loadPartitions(RedisClusterClient.java:844)
... 4 more
Suppressed: io.lettuce.core.RedisConnectionException: Unable to connect to localhost:7000
Caused by: java.nio.channels.UnresolvedAddressException
    at sun.nio.ch.Net.checkAddress(Net.java:101)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:622)
    at io.netty.channel.socket.nio.NioSocketChannel.doConnect(NioSocketChannel.java:209)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.connect(AbstractNioChannel.java:199)
    ... 21 more

最佳答案

  <!-- The configuration of maven-jar-plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <configuration>
                    <!-- put your configurations here -->
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>shadeall</shadedClassifierName>
                    <relocations>
                        <relocation>
                            <pattern>io.netty</pattern>
                            <shadedPattern>com.ly.dc.io.netty</shadedPattern>
                        </relocation>
                    </relocations>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

关于java - 无法使用 Lettuce 连接到本地 Redis 集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54309322/

相关文章:

java - 编写一个方法对单链表进行升序排序(java)

java - 如果参数匹配,XSLT 更改标记的值

java - 通过 parcelable extra 检索时为空对象

websocket - 在 ELB 后面的多个实例中使用 Socket.IO 和 Redis

redis - 为什么 Redis 禁止用户脚本调用其他脚本?如何保持 Lua 脚本的可维护性

compression - 在将字符串放入 redis 之前压缩字符串 - 这有意义吗?

java - 将 Lettuce 与 RedisTemplate 一起使用会抛出异常

java - 可以使用 RSA 加密的数据量有多少限制?

java - 使用 Lettuce 为 Redis master/slave 配置 Spring Data Redis

java - 每个 servlet 一个 Lettuce Redis 连接?