无法从 docker 容器内的 Spring Boot 连接到 Redis

Posted

技术标签:

【中文标题】无法从 docker 容器内的 Spring Boot 连接到 Redis【英文标题】:Cannot connect to Redis from Spring Boot inside docker container 【发布时间】:2021-12-23 02:35:35 【问题描述】:

我在从 Spring Boot 应用程序连接到 Docker 容器内的 Redis 时遇到问题。我试图将保护模式更改为 no 并更改 redis 中的绑定,但它对我不起作用。我还尝试将redis主机从'localhost'更改为'redis',但它也不起作用。我正在尝试将我的应用程序 dockerize 很长时间,这就是我寻求帮助的原因。顺便说一句,我正在使用 Jedis。

上次的日志有一些问题: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/boot/autoconfigure/session/RedisSessionConfiguration$SpringBootRedisHttpSessionConfiguration.class]: Invocation of init method failed; Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379 java.net.ConnectException: Connection refused

你可以在这里找到我的代码: https://github.com/CommoMegaSator/Versatile-Development

这是我的 docker-compose 文件:

version: "3.7"

services:
  postgresql:
    image: postgres:latest
    container_name: postgresql
    volumes:
      - /opt/postgres-data:/var/lib/postgresql/data
    ports:
      - "3001:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=versatile
      - POSTGRES_SCHEMA=main
    restart: on-failure
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
    expose:
      - 3001
    networks:
      - versatile

  redis:
    image: redis:latest
    container_name: redis
    command: [ "redis-server", "--protected-mode", "no", "--bind", "0.0.0.0" ]
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
      - REDIS_DISABLE_COMMANDS=FLUSHDB, FLUSHALL
    restart: on-failure
    ports:
      - "3002:6379"
    networks:
      - versatile

  versatile:
    image: versatile
    build:
      context: .
    container_name: versatile
    ports:
      - "3000:8081"
    restart: on-failure
    links:
      - redis
      - postgresql
    depends_on:
      - redis
      - postgresql
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://postgresql:5432/versatile
      - SPRING_DATASOURCE_USERNAME=postgres
      - SPRING_DATASOURCE_PASSWORD=postgres
      - SPRING_DATASOURCE_HIKARI_SCHEMA=main
      - SPRING_JPA_HIBERNATE_DDL_AUTO=update
    expose:
      - 8081
    networks:
      - versatile

networks:
  versatile:
    driver: bridge

还有我的 application.properties:

#Spring DATASOURCE
spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.url = jdbc:postgresql://localhost:5432/versatile
spring.datasource.hikari.schema=main
spring.datasource.username = postgres
spring.datasource.password = postgres
flyway.createSchemas=true
spring.flyway.schemas=main

#Spring JPA properties
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation = true
spring.jpa.show-sql = false

#Redis properties
spring.session.store-type = redis
spring.session.redis.flush-mode = on_save
spring.session.redis.namespace = spring:session
spring.cache.type = redis
spring.redis.host = localhost
spring.redis.password =
spring.redis.port = 6379

【问题讨论】:

请在您的问题中添加一个可重现的最小示例,不要指望这里的人通过您的整个代码进行故障排除 【参考方案1】:

在您的应用程序属性中,您已将数据库和 redis 的主机定义为 localhost

spring.datasource.url = jdbc:postgresql://localhost:5432/versatile
spring.redis.host = localhost

如果您在本地开发机器上运行所有内容,这可能是正确的,但在 docker-compose 场景中,您需要将主机名设置为分配给 docker 容器的名称 - 即设置为 container_name 的值在撰写文件中。那分别是postgresqlredis

【讨论】:

我尝试将 redis 主机从 localhost 更改为 redis,但结果相同。 然后您需要进行故障排除。要看的东西... redis 是否启动并在您认为的端口上运行(容器本身上的 3002),网络当前是否正常(配置看起来不错) - CLI 到测试容器并检查您是否可以看到/连接到redis 容器。查看日志以获取有关问题的任何线索,检查启动正常的 redis 日志,看看是否可以启用更多日志记录以帮助排除故障。

以上是关于无法从 docker 容器内的 Spring Boot 连接到 Redis的主要内容,如果未能解决你的问题,请参考以下文章