无法获取Jedis连接,嵌套异常:无法从池中获取资源

时间:2018-07-18 15:30:41

标签: spring-boot jedis spring-data-redis spring-cache

在尝试使用Spring Boot连接到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"));

    }
}

我知道有一个类似的问题here,但是我还没有要求评分的评语,我已经尝试了他们的建议,但结果仍然相同。 这是我的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(AbstractPlainSocketImpl.java:350)             在java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)             在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)             在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)             在java.net.Socket.connect(Socket.java:589)             在redis.clients.jedis.Connection.connect(Connection.java:184)

知道我在做什么错吗? 谢谢

1 个答案:

答案 0 :(得分:1)

您的端口号在属性和自配置的连接工厂之间配置错误。

我建议坚持使用其中一个插入RedisAutoconfiguration的Spring Boot的JedisConnectionConfiguration,这会根据属性为您自动创建一个ConnectionFactory

https://github.com/spring-projects/spring-boot/blob/8f4bf233b4895a4fade5aff41e0a309f90ba3193/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java

spring.redis属性驱动。