I am getting below error
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis] with root cause
java.net.ConnectException: Connection refused
below is my code
application.properties
# Redis Configurationspring.cache.type=redisspring.redis.host=redis-14218.c301.ap-south-1-1.ec2.cloud.redislabs.comspring.redis.port=14218spring.redis.password=***spring.redis.database=0
my Application.Java
package com.serverless.serverlessapi;import java.time.Duration;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.data.redis.cache.RedisCacheConfiguration;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair; @SpringBootApplication @EnableCaching public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public RedisCacheConfiguration cacheConfiguration() { return RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofMinutes(60)) .disableCachingNullValues() .serializeValuesWith(SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); } @Bean public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() { return (builder) -> builder .withCacheConfiguration("leads", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10))) .withCacheConfiguration("leadsCahce2", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(5))); } }
my RestController
package com.serverless.serverlessapi.rest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.Cacheable;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import com.serverless.serverlessapi.services.LeadsService;@RestController@RequestMapping("/api")public class LeadsRestController { @Autowired private LeadsService leadsService; @Cacheable(value = "leads") @GetMapping("/leads2") public String getAllLeads2(@RequestParam(name = "limit") int limit, @RequestParam(name = "skip") int skip){ try { return leadsService.getAllLeads2(limit,skip); } catch (Exception e) { e.printStackTrace(); } return "data not found, please contact to admin"; }}
I have also enbled the redis dbenter image description here