[英]How to fix connection refused error in Jedis in docker-compose?
i am setting docker-compose with redis server and java application, when redis run inside docker-compose my code working fine with outside java program in HOST OS . 当redis在docker-compose内部运行时,我正在使用Redis服务器和Java应用程序设置docker-compose,我的代码可以与HOST OS中的外部Java程序一起正常工作。
but when i put java program inside container i am getting 但是当我将Java程序放入容器中时
java.net.ConnectException: Connection refused java.net.ConnectException:连接被拒绝
Redis-cli 127.0.0.1:6379> is also working fine Redis-cli 127.0.0.1:6379>也可以正常工作
try {
Thread.sleep(3000);
jedis = new Jedis("localhost");
this.restTemplate = new RestTemplate();
startWorking();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
when use "redis" instead of "localhost" its give me unknown host error from outside docker-compose and connection refused inside docker-compose 当使用“ redis”而不是“ localhost”时,它从docker-compose外部给我未知主机错误,并且在docker-compose内拒绝连接
this is my dockercompose.yml 这是我的dockercompose.yml
redis:
image: redis
ports:
- "6379:6379"
worker:
build: worker
Try specifying networks explicitly and adding depends_on
to make sure redis is up when the worker needs it. 尝试显式指定网络并添加
depends_on
以确保在工作人员需要时redis已启动。
version: "3.7"
services:
worker:
build: worker
networks:
- net
depends_on:
- redis
redis:
image: redis
ports:
- "6379:6379"
networks:
- net
networks:
net:
Create the jedis
instance with new Jedis("redis", 6379);
使用
new Jedis("redis", 6379);
创建jedis
实例new Jedis("redis", 6379);
. 。
You might want to read more about networking in compose and dependencies between services . 您可能想了解有关服务之间的 组合和依赖关系的更多信息。
You can also try running an example without compose and then transforming it to a compose configuration. 您也可以尝试在没有撰写的情况下运行示例,然后将其转换为撰写配置。 Here is an example of network configuration without docker compose.
这是一个没有docker compose 的网络配置示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.