Springboot Docker-Compose Redis无法获得Jedis连接;

发布于 2025-01-17 14:16:03 字数 2458 浏览 0 评论 0原文

这似乎是 docker-compose with springboot和springboot and redis href =“ https://stackoverflow.com/questions/63646880/docker-compose-redis-connection-issue”> docker-compose redis连接问题但建议在我的情况下提出的解决方案提出的解决方案。

我正在使用Springboot,其他服务无法连接到REDIS服务。 以下是我的docker撰写文件:

version: "3.7"

services:

...

  users:
    build: ./users
    ports:
      - "8081:8081"
    networks:
      - jiji-microservices-network
    depends_on:
      - registry
      - gateway
      - redis_cache
      - postgresDB
      - rabbitmq
    links:
      - redis_cache
      - postgresDB
      - rabbitmq
    environment:
#      SPRING_CACHE_TYPE: redis
#      SPRING_REDIS_HOST: redis_cache
#      SPRING_REDIS_PORT: 6379
#      SPRING_REDIS_PASSWORD:
      SPRING_DATASOURCE_URL: jdbc:postgresql://postgresDB:5432/jiji_users
      SPRING_DATASOURCE_USERNAME: postgres
      SPRING_DATASOURCE_PASSWORD: postgres
      SPRING_JPA_HIBERNATE_DDL_AUTO: update
      EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://registry:8090/eureka


  postgresDB:
    image: postgres
    restart: unless-stopped
    ports:
      - "5432:5432"
    networks:
      - jiji-microservices-network
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: jiji_users

  redis_cache:
    image: redis:latest
    restart: on-failure
    command: ["redis-server","--bind","redis_cache","--port","6379"]
    ports:
      - "6379:6379"
    networks:
      - jiji-microservices-network


  rabbitmq:
    image: rabbitmq:management
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - jiji-microservices-network

networks:
  jiji-microservices-network:
    driver: bridge

以下是我的应用程序。yml文件:

...
  cache:
    type: redis
  redis:
    host: redis_cache
    port: 6379
#    cache-null-values: true
    time-to-live: 2592000 #30 days
...

我收到的错误消息:

users_1         | 2022-03-28 02:53:40.417  INFO 1 --- [nio-8081-exec-4] c.o.u.s.CustomAuthorizationFilter        : /api/v1/users/getCode
users_1         | Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

docker PS显示所有容器都启动并运行,Redis在端口6379上都在听。

。PS:没有Docker,它可以正常运行。

This may seems as duplicate of docker-compose with springboot and redis and docker-compose redis connection issue but the solutions proposed there are not working in my case.

I am using springboot and the other services are unable to connect to redis service.
Below is my docker compose file:

version: "3.7"

services:

...

  users:
    build: ./users
    ports:
      - "8081:8081"
    networks:
      - jiji-microservices-network
    depends_on:
      - registry
      - gateway
      - redis_cache
      - postgresDB
      - rabbitmq
    links:
      - redis_cache
      - postgresDB
      - rabbitmq
    environment:
#      SPRING_CACHE_TYPE: redis
#      SPRING_REDIS_HOST: redis_cache
#      SPRING_REDIS_PORT: 6379
#      SPRING_REDIS_PASSWORD:
      SPRING_DATASOURCE_URL: jdbc:postgresql://postgresDB:5432/jiji_users
      SPRING_DATASOURCE_USERNAME: postgres
      SPRING_DATASOURCE_PASSWORD: postgres
      SPRING_JPA_HIBERNATE_DDL_AUTO: update
      EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://registry:8090/eureka


  postgresDB:
    image: postgres
    restart: unless-stopped
    ports:
      - "5432:5432"
    networks:
      - jiji-microservices-network
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: jiji_users

  redis_cache:
    image: redis:latest
    restart: on-failure
    command: ["redis-server","--bind","redis_cache","--port","6379"]
    ports:
      - "6379:6379"
    networks:
      - jiji-microservices-network


  rabbitmq:
    image: rabbitmq:management
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - jiji-microservices-network

networks:
  jiji-microservices-network:
    driver: bridge

Here below is my application.yml file:

...
  cache:
    type: redis
  redis:
    host: redis_cache
    port: 6379
#    cache-null-values: true
    time-to-live: 2592000 #30 days
...

The error message I am getting:

users_1         | 2022-03-28 02:53:40.417  INFO 1 --- [nio-8081-exec-4] c.o.u.s.CustomAuthorizationFilter        : /api/v1/users/getCode
users_1         | Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

docker ps shows that all the containers are up and running and that redis is listening on port 6379.

PS: It works fine without docker.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2025-01-24 14:16:03

该问题与我的 Redisconfiguration 有关。修复了将 redis 主机和 redis 端口添加到 JedisConnectionFactory Bean 的问题。

@Configuration
@Slf4j
public class RedisConfiguration {
    @Value("${spring.redis.host}")
    private String REDIS_HOST;
    @Value("${spring.redis.port}")
    private Integer REDIS_PORT;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(REDIS_HOST, REDIS_PORT);
        return new JedisConnectionFactory(config);
    }

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

The issue was related to my Redisconfiguration. Fixed bit adding redis host and redis port to JedisConnectionFactory Bean.

@Configuration
@Slf4j
public class RedisConfiguration {
    @Value("${spring.redis.host}")
    private String REDIS_HOST;
    @Value("${spring.redis.port}")
    private Integer REDIS_PORT;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(REDIS_HOST, REDIS_PORT);
        return new JedisConnectionFactory(config);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setEnableTransactionSupport(true);
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文