【问题标题】:Springboot application container cannot connect to redis containerSpring Boot 应用程序容器无法连接到 redis 容器
【发布时间】:2020-12-23 03:10:33
【问题描述】:

我正在尝试使用 docker 在本地运行带有 redis 的 Spring Boot 应用程序。该应用似乎无法连接到 redis 容器。

我的 Dockerfile

FROM java:8
VOLUME /tmp
ADD target/test-*.jar test.jar
RUN bash -c 'touch /test.jar'
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/test.jar"]

docker-compose.yml

version: '2'
services:
  app:
    build: .
    ports:
      - "8080:8080"
    links:
      - "db:redis"
  db:
    image: "redis:alpine"
    hostname: redis
    ports:
      - "6379:6379"

应用程序属性

spring.redis.host=redis

当应用程序尝试连接到它抛出的数据库时

Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool] with root cause
app_1  | 
app_1  | java.net.ConnectException: Connection refused (Connection refused)

我用来连接数据库的代码

@Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }

任何帮助将不胜感激。谢谢

【问题讨论】:

标签: spring-boot docker redis


【解决方案1】:

将应用服务中的 docker-compose.yml 文件链接更改为 - links: - "db"。 在两个服务之间建立链接时,您不需要添加主机名。主机名将用于在您的代码中引用。您在 applications.properties 文件中正确完成的操作。

【讨论】:

  • 感谢您的回复。我试过了..但仍然给出同样的例外
  • "主机名将用于在您的代码中引用。" - 不。主机名在它指定的容器中设置主机名(即从内部 ping 主机名容器将导致 ping 本地主机)
猜你喜欢
  • 2018-06-23
  • 1970-01-01
  • 2021-12-23
  • 2019-11-25
  • 2022-11-06
  • 2021-04-22
  • 1970-01-01
  • 2018-09-26
  • 2021-08-24
相关资源
最近更新 更多