0

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.

1 Answer 1

5

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.