简体   繁体   English

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

[英]How to connect to a Docker Redis cluster instance using jedis for Java?

I have created a cluster of 6 nodes using redis docker-compose (3 master and 3 slave) and also linked all of them.我使用 redis docker-compose(3 个主节点和 3 个从节点)创建了一个由 6 个节点组成的集群,并将它们全部链接起来。 cluster info集群信息

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

cluster nodes集群节点

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

As shown above the cluster seems to be working but when I try to call the cluster instance in a java application using jedis by using the code如上所示,集群似乎正在工作,但是当我尝试使用 jedis 使用代码在 java 应用程序中调用集群实例时

jedis = new JedisCluster(new HostAndPort("0.0.0.0", 32790));
jedis.set("events/city/rome", "32,15,223,828");

then I get the following error然后我收到以下错误

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)

Please help me find out what could be the problem.请帮我找出可能是什么问题。 Is it because I have not specified the right IP and port number when creating a jedis instance.是不是因为我在创建jedis实例的时候没有指定正确的IP和端口号。 Or is it something that I am missing.或者是我想念的东西。

Thanks.谢谢。

The problem is in Redis Cluster config!问题出在 Redis 集群配置中!

Your app requests to one of the configured nodes.您的应用程序请求配置的节点之一。 It receives the address of all Redis nodes, in your case is IP in the Docker network.它接收所有 Redis 节点的地址,在您的情况下是 Docker 网络中的 IP。 As a result, you get a connection error.因此,您会收到连接错误。

To fix this, add the following parameter to each node in the cluster to update the "advertise" address要解决此问题,请将以下参数添加到集群中的每个节点以更新“广告”地址

Example with node 192.168.83.101:7000:17000 (docker host is 192.168.83.101 , command port is 7000 , cluster bus port is 17000 )以节点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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM