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

3

我希望你能为我提供帮助,有关连接Redis使用Spring Boot时遇到的问题。

我正在使用以下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

堆栈跟踪是这样的:
   redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
            at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
            at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
            at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
            at java.net.Socket.connect(Socket.java:589)
            at redis.clients.jedis.Connection.connect(Connection.java:184)



        

请问我做错了什么?谢谢。


1
你的端口号在Spring自动配置中(即属性)和你自己定义的连接工厂之间是不同的。如果你要创建自己的连接工厂,指定属性就没有意义了。 - Darren Forsythe
现在它工作了,老实说当我设置端口时没有看到我输入了错误的数字。非常感谢!是的,我知道它们应该从属性文件中取出,这是另一个问题,我无法读取int值,如果我尝试将其作为String读取并在之后转换为int,就会得到NumberFormatException错误,或者会提示它是String而我正在尝试读取int。我也发布了那个问题。非常感谢您发现我的错误。 - user9934722
@DarrenForsythe,您想回答我的问题吗?我认为让我回答这个问题是不公平的,我已经浪费了一整天的时间,非常感谢。 - user9934722
1个回答

2
您的端口号在属性和自配置的连接工厂之间没有正确配置。
我建议使用Spring Boot的RedisAutoconfiguration或者JedisConnectionConfiguration中的一个。其中RedisAutoconfiguration会自动基于属性为您创建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 属性驱动。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接