Мне нужна помощь в отношении некоторых проблем, с которыми я сталкиваюсь при попытке подключиться к Redis с помощью весенней загрузки.
Я использую следующую конфигурацию RedisConfiguration:
@Component
@Configuration
public class SpringRedisConf extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String redisHostName;
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setHostName("localhost");
connectionFactory.setUsePool(true);
connectionFactory.setPort(PortName);
connectionFactory.getPoolConfig().setMaxTotal(10);
connectionFactory.getPoolConfig().setMaxIdle(10);
return connectionFactory;
}
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
if (null == redisConnectionFactory) {
LOG.error("Redis Template Service is not available");
return null;
}
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(redisConnectionFactory);
template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
template.setKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
template.setEnableTransactionSupport(true);
return template;
}
@Bean
public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// Number of seconds before expiration. Defaults to unlimited (0)
return cacheManager;
}
И следующий класс, в котором я пытаюсь создать модульные тесты для проверки моего соединения:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserApp.class)
public class RedisConnectionTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
public void testRedisConnection() {
redisTemplate.opsForValue().set("mouse", "loves cheese");
assertThat( redisTemplate.opsForValue().get( "mouse").equals("loves cheese"));
}
}
Я знаю, что есть здесь аналогичный вопрос. , но у меня еще нет требуемой оценки для комментариев, я попробовал их предложения и получил тот же результат. Вот мой pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${redis_clients_version}</version>
</dependency>
А вот и мой файл свойств:
spring.data.redis.repositories.enabled=true
spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=XXXX
spring.cache.redis.time-to-live=0ms
spring.cache.redis.cache-null-values=false
StackTrace таков:
redis.clients.jedis.exceptions.JedisConnectionException: не удалось получить ресурс из пула в java.net.PlainSocketImpl.socketConnect (собственный метод) в java.net.AbstractPlainSocketImpl.doConnect (AbstractPlainSocket.netImpl.java:350) в java:350) в java. AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:206) на java.net. .java: 589) в redis.clients.jedis.Connection.connect (Connection.java:184)
Есть идеи, что я делаю не так? Спасибо