Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spring boot not connect to redis docker container

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading