Cannot get Jedis connection; nested exception is java.lang.NullPointerException

本文介绍了解决Spring Boot应用中出现的Redis连接失败问题的具体步骤。通过在代码中添加`afterPropertiesSet()`方法调用,确保了JedisConnectionFactory正确初始化,从而避免了空指针异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前提:Redis服务器已经运行,且端口号,服务器地址都已经配置正常,但任然抛出无法获取连接异常

原来的代码如下:

  1. @Bean  
  2.    public JedisConnectionFactory connectionFactory(){  
  3.        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();  
  4.        jedisConnectionFactory.setUsePool(true);  
  5.        jedisConnectionFactory.setPoolConfig(jedisPoolConfig());  
  6.        jedisConnectionFactory.setHostName(environment.getProperty("redis.host"));  
  7.        jedisConnectionFactory.setPort(Integer.parseInt(environment.getProperty("redis.port")));  
  8.        return jedisConnectionFactory;  
  9.    }  

其他配置都很正常,但是运行后会抛出空指针异常。
Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is java.lang.NullPointerException

解决方法如下,在return前添加如下代码

  1. jedisConnectionFactory.afterPropertiesSet();  
看源码便知原因:看jedis如何获取到连接的:
  1. public JedisConnection getConnection() {  
  2.         Jedis jedis = fetchJedisConnector();  
  3.         JedisConnection connection = (usePool ? new JedisConnection(jedis, pool, dbIndex) : new JedisConnection(jedis,  
  4.                 null, dbIndex));  
  5.         connection.setConvertPipelineAndTxResults(convertPipelineAndTxResults);  
  6.         return postProcessConnection(connection);  
  7.     }  
我们看fetchJedisConnector()方法:
  1. protected Jedis fetchJedisConnector() {  
  2.         try {  
  3.             if (usePool && pool != null) {  
  4.                 return pool.getResource();  
  5.             }  
  6.             Jedis jedis = new Jedis(getShardInfo());  
  7.             // force initialization (see Jedis issue #82)  
  8.             jedis.connect();  
  9.             return jedis;  
  10.         } catch (Exception ex) {  
  11.             throw new RedisConnectionFailureException("Cannot get Jedis connection", ex);  
  12.         }  
  13.     }  

如果没用用pool的话,需要用到getShardInfo()方法,而这个方法就是返回一个JedisShardInfo shardInfo。那么这个JedisShardInfo shardInfo在什么时候初始化呢?查看后发现,哈哈在这:
  1. public void afterPropertiesSet() {  
  2.         if (shardInfo == null) {  
  3.             shardInfo = new JedisShardInfo(hostName, port);  
  4.   
  5.             if (StringUtils.hasLength(password)) {  
  6.                 shardInfo.setPassword(password);  
  7.             }  
  8.   
  9.             if (timeout > 0) {  
  10.                 setTimeoutOn(shardInfo, timeout);  
  11.             }  
  12.         }  
  13.   
  14.         if (usePool) {  
  15.             this.pool = createPool();  
  16.         }  
  17.     }  

这个方法将在所有的属性被初始化后调用。但是会在init前调用。是spring中InitializingBean接口的方法。spring-data-redis里面实现了它。在这里对shardIndo类进行了初始化。所以,我们只要在代码中添加:

  1. jedisConnectionFactory.afterPropertiesSet();  
这句就可以啦。再运行一次,连接成功!
报错“Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException”是由于在调试代码时无法获取Jedis连接引起的。 解决这个问题的方法是确保已正确配置了Redis连接信息,并且Redis服务器正常运行。您可以检查以下几点: 1. 确保已正确导入了Jedis相关的jar包,如jedis-2.4.1.jar、redis.clients-3.0.1.jar、jedis-3.0.0.jar。 2. 检查您的代码中是否正确地创建了Jedis连接池,并指定了正确的Redis服务器地址和端口。 3. 检查您的代码中是否正确地获取Jedis连接并执行相关操作之前,已经初始化了连接池。 4. 如果您的代码中使用了缓存配置,如<***请确保已正确配置了缓存 eviction 和 type 属性。 如果您已经检查过上述内容并确认没有问题,您还可以尝试以下步骤来解决这个问题: 1. 检查您的网络连接是否正常,尝试重新连接Redis服务器。 2. 检查Redis服务器的运行状态,确保Redis服务器已正常启动并且没有其他配置或权限问题。 3. 检查Redis服务器的配置文件,如redis.conf,确保没有限制连接数或其他相关设置。 通过以上步骤,您应该能够解决报错“Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException”的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [redis使用问题一:Cannot get Jedis ... nested exception is redis.clients.jedis.exceptions.JedisExce...](https://blog.csdn.net/denghe4720/article/details/101362329)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [jedis-2.4.1.jar、redis.clients-3.0.1.jar、jedis-3.0.0.jar](https://download.csdn.net/download/a284835492/86796955)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值