jedis 无法连接到 redis docker 实例 - 连接被拒绝(连接被拒绝)

时间:2021-07-08 08:53:52

标签: java docker redis jedis

按照 url link 中的建议在本地运行 redis docker 镜像 从日志中可以看出运行良好

package test;

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

然后 docker 启动。我可以看到如下日志

docker pull redis
docker run --name some-redis -d redis

我使用 jedis 连接到这个 redis 容器,这里的代码和 redis docker 都在本地机器上。下面的代码不在容器上,而是在本地主机上。 redis 已经暴露了 6379 端口 redis dockerfile link

docker logs --follow some-redis
1:C 08 Jul 2021 07:17:51.243 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 08 Jul 2021 07:17:51.243 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 08 Jul 2021 07:17:51.243 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 08 Jul 2021 07:17:51.244 * monotonic clock: POSIX clock_gettime
1:M 08 Jul 2021 07:17:51.247 * Running mode=standalone, port=6379.
1:M 08 Jul 2021 07:17:51.248 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 08 Jul 2021 07:17:51.248 # Server initialized
1:M 08 Jul 2021 07:17:51.250 * Ready to accept connections

我在连接时出错

public static void main(String [] args) {
    System.out.println("connecting");
    Jedis jedis;
    jedis = new Jedis("localhost",6379);
    System.out.println("connected and pinging now");
    //jedis.ping();
    jedis.set("events/city/rome", "32,15,223,828");  //line 274
    String cachedResponse = jedis.get("events/city/rome");
    System.out.println("ping done");
}

此错误也发生在 connecting connected and pinging now Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused) at redis.clients.jedis.Connection.connect(Connection.java:164) at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80) at redis.clients.jedis.Connection.sendCommand(Connection.java:100) at redis.clients.jedis.BinaryClient.set(BinaryClient.java:97) at redis.clients.jedis.Client.set(Client.java:32) at redis.clients.jedis.Jedis.set(Jedis.java:68) at com.org.app.module.TaskPerformer.main(TaskPerformer.java:274) Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:606) at redis.clients.jedis.Connection.connect(Connection.java:158) ... 6 more 的第 273 行

1 个答案:

答案 0 :(得分:2)

您应该通过明确提及端口或使用主机网络将 Redis 端口 (TCP 6379) 发布到主机系统。

这里是如何只发布 TCP 端口 6379(推荐的解决方案):

docker run --rm -p 6379:6379 redis

这里是您如何通过使用网络主机(这对于您的简单案例来说似乎有点矫枉过正,并且可能面临进一步的安全风险):

docker run --rm --network host redis