Home > Back-end >  Spring boot not connect to redis docker container
Spring boot not connect to redis docker container

Time:10-05

I'm trying to connect a Spring Boot application on a instance of redis in my docker, I have configure the host localhost and port 16379, but something strange happens, the cache works, but when i see the redis keys are empty, o think that the Spring boot is using a embeded redis instance.

My pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
        <version>2.4.3</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>2.3.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
    </dependency>

my redis configuration class

@Configuration 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, ?> redisTemplate() {
    RedisTemplate<String, ?> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory());
    template.setEnableTransactionSupport(true);
    template.setExposeConnection(true);
    template.afterPropertiesSet();
    return template;
}

my application.properties

spring.cache.type=simple

spring.redis.host = localhost
spring.redis.port = 16379

my docker image of redis

  redis.school:
    image: redis:latest
    command: redis-server
    ports:
      - 16379:6379
    volumes:
      - redis.school.data:/data

how can i connect to the docker image instance instead a internal Spring Boot redis?

CodePudding user response:

You have to change this spring.cache.type=simple to spring.cache.type=redis to work with redis cache.

  • Related