1

I'm new to docker and I'm trying to run redis-server and my springboot app both on a container. The Redis server is running fine on docker but when i try to connect my spring boot app to the redis server it get this error

jakarta.servlet.ServletException: Request processing failed: org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis


at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1022)


at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914)


at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:547)


at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)


at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:614)


at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)


at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)


at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)


at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:67)


at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)


at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)


at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:67)


at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)


at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:110)


at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:67)


at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)


at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:108)


at org.springframework.security.web.FilterChainProxy.lambda$doFilterInternal$3(FilterChainProxy.java:231)


at org.springframework.security.web.ObservationFilterChainDecorator$FilterObservation$SimpleFilterObservation.lambda$wrap$1(ObservationFilterChainDecorator.java:479)


at org.springframework.security.web.ObservationFilterChainDecorator$AroundFilterObservation$SimpleAroundFilterObservation.lambda$wrap$1(ObservationFilterChainDecorator.java:340)


at org.springframework.security.web.ObservationFilterChainDecorator.lambda$wrapSecured$0(ObservationFilterChainDecorator.java:82)


at org.springframework.security.web.ObservationFilterChainDecorator$VirtualFilterChain.doFilter(ObservationFilterChainDecorator.java:128)

My properties file is like this

spring.data.redis.host= ${REDIS_HOST:127.0.0.1}
spring.data.redis.port= 6379

My docker-compose file is like this

  redis-server:
    image: redis:latest
    restart: always
    container_name: redis-server
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - royalnet

  iam-service:
    build:
      context: ./iam-service
    container_name: iam-service
    ports:
      - "8082:8082"
    environment:
      IAM_INTERNAL: http://iam-service:8082
      STORAGE_INTERNAL: http://storage-service:8083
      REDIS_HOST: redis-server
      POSTGRES_URL: jdbc:postgresql://my_postgres:5432/iam_db
      postgres:
        condition: service_healthy
      keycloak:
        condition: service_started
      redis-server:
        condition: service_started
    networks:
      - royalnet

I have try change the redis.conf to

bind 0.0.0.0
protected-mode no
port 6379

and try using jedis but still the same error

@EnableCaching
@Configuration
public class RedisConfig {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private Integer port;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPort(port);
        return new JedisConnectionFactory(config);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        return redisTemplate;
    }
}
3
  • 2
    Your code references spring.redis.host, but your configuration uses spring.data.redis.host. Commented Apr 25 at 14:24
  • thanks you for your answer and i have try both spring.redis.host and spring.data.redis.host but still the same error. Here its more of that error can you know the reason why? Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:6379 Caused by: java.net.ConnectException: Connection refused at java.base/sun.nio.ch.Net.pollConnect(Native Method) at java.base/sun.nio.ch.Net.pollConnectNow(Unknown Source) at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source) at Commented Apr 26 at 2:59
  • @RoyalPie from your last comment, notice the error shows localhost/127.0.0.1. There is something wrong when loading the variable with the redis server Commented May 17 at 14:08

1 Answer 1

0

so what's happening here is that the server defined in the application.yml is getting overriden by the conection factory. I was facing the same issue, what worked was manually specifyin ports users host and password like this:

@Value("${spring.redis.host}")
private String redisHost;

@Value("${spring.redis.port}")
private int redisPort;

@Value("${spring.redis.username}")
private String redisUsername;

@Value("${spring.redis.password}")
private String redisPassword;

@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost, redisPort);
    redisStandaloneConfiguration.setUsername(redisUsername);
    redisStandaloneConfiguration.setPassword(redisPassword);
    
    return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
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.