redis的getResource为null问题

产生此错误的原因通常是:

 一、Redis没有启动;

我自己遇到一次这样的问题。汗!

二、由于防火墙原因无法连接到Redis;

1、服务器防火墙入站规则。

2、访问Redis的应用程序所在主机的出站规则。

三、IP地址或端口错误

四、Jedis 对象用完以后,要释放掉,不让会一直占用,所以会出现无法获取新的资源。

五、Spring Boot项目,缺少依赖

如果使用Redis与Spring Boot,也会抛出此异常。
如果你使用的是Spring Boot,那么Redis的依赖是不够的,
您还需要从redis.io手动下载并安装Redis,然后将其从终端运行


me@my_pc:/path/to/redis/dir$ ./src/redis-server ./redis.conf
运行服务器后,您需要在使用Redis的所有应用程序中添加相关行:
application.properties:

spring.redis.host: <yourhost> // usually localhost, but can also be on a LAN
spring.redis.port: <yourport> // usually 6379, but settable in redis.conf
application.yml:
...
spring:
  redis:
    host: <yourhost> // usually localhost, but can also be on a LAN
    port: <yourport> // usually 6379, but settable in redis.conf

六、vm.overcommit_memory = 0,fork失败


使用redis-cli,执行ping命令,异常信息出来了:

(error)MISCONF Redis is configured to save RDB snapshots, but is currently

not able to persist on disk. Commands that may modify the data set

are disabled. Please check Redis logs for details about the error.

然后查看redis日志,出现了

WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

问题已经很清晰了,bgsave会fork一个子进程,因为vm.overcommit_memory = 0,所以申请的内存大小和父进程的一样,由于redis已经使用了60%的内存空间,所以fork失败

解决办法:

/etc/sysctl.conf 添加 vm.overcommit_memory=1

sysctl  vm.overcommit_memory=1

七、当jedispool中的jedis被取完 等待超过你设置的 MaxWaitMillis 就会抛出Could not get a resource from the pool 
八、连接池中空闲的连接过一阵子就会自动断开,但是连接池还以为连接正常

参考配置文件:


JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(200);
config.setMaxIdle(50);
config.setMinIdle(8);//设置最小空闲数
config.setMaxWaitMillis(10000);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
//Idle时进行连接扫描
config.setTestWhileIdle(true);
//表示idle object evitor两次扫描之间要sleep的毫秒数
config.setTimeBetweenEvictionRunsMillis(30000);
//表示idle object evitor每次扫描的最多的对象数
config.setNumTestsPerEvictionRun(10);
//表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
config.setMinEvictableIdleTimeMillis(60000);

JedisPool pool = new JedisPool(config, ip, port, 10000, "密码", 0);

猜你喜欢

转载自blog.csdn.net/weixin_41492331/article/details/88123555