gpt4 book ai didi

java - 如何使用 jedis for Java 连接到 Docker Redis 集群实例?

转载 作者:行者123 更新时间:2023-12-04 13:37:15 63 4
gpt4 key购买 nike

我使用 redis docker-compose(3 个主节点和 3 个从节点)创建了一个由 6 个节点组成的集群,并且还链接了所有节点。
集群信息

cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:5
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:23
cluster_stats_messages_pong_sent:31
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:55
cluster_stats_messages_ping_received:26
cluster_stats_messages_pong_received:24
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:55

集群节点
d794a9ab002f0c3cb699ce68a09310dd0fdb17de 192.168.65.3:32789@32783 slave c6c05515c3be01a1438b6d2aad823c0fa50b1743 0 1586629088989 5 connected
7d4fab850bcfac8754a559c5e9469698b7f182bc 192.168.65.3:32792@32787 master - 0 1586629087000 2 connected 5461-10922
c6c05515c3be01a1438b6d2aad823c0fa50b1743 192.168.65.3:32793@32788 master - 0 1586629089995 4 connected 10923-16383
229b9b4f919f79a1c24b7b849c42acb9e3378532 192.168.65.3:32790@32785 slave 9b649a67dc53084ed7416b20e8bab00289e636d2 0 1586629089000 6 connected
9b649a67dc53084ed7416b20e8bab00289e636d2 192.168.65.3:32791@32786 myself,master - 0 1586629086000 1 connected 0-5460

如上所示,集群似乎正在工作,但是当我尝试使用 jedis 使用代码在 Java 应用程序中调用集群实例时
jedis = new JedisCluster(new HostAndPort("0.0.0.0", 32790));
jedis.set("events/city/rome", "32,15,223,828");

然后我收到以下错误
Exception in thread "main" redis.clients.jedis.exceptions.JedisClusterMaxAttemptsException: No more cluster attempts left.
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:86)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:124)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:124)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:124)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:124)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:124)
at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:25)
at redis.clients.jedis.JedisCluster.set(JedisCluster.java:112)
at sc.dev.algordf.ALGORDF.main(ALGORDF.java:53)

请帮我找出可能是什么问题。是不是因为我在创建jedis实例的时候没有指定正确的IP和端口号。或者是我缺少的东西。

谢谢。

最佳答案

问题出在 Redis 集群配置中!
您的应用程序向配置的节点之一发出请求。它接收所有 Redis 节点的地址,在您的情况下是 Docker 网络中的 IP。因此,您会收到连接错误。
要解决此问题,请将以下参数添加到集群中的每个节点以更新“广告”地址
节点示例 192.168.83.101:7000:17000 (docker 主机是 192.168.83.101 ,命令端口是 7000 ,集群总线端口是 17000 )

cluster-announce-ip 192.168.83.100
cluster-announce-port 7000
cluster-announce-bus-port 17000

关于java - 如何使用 jedis for Java 连接到 Docker Redis 集群实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61162024/

63 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com