SHIHUC

好记性不如烂笔头,还可以分享给别人看看! 专注基础算法,互联网架构,人工智能领域的技术实现和应用。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

redis连接错误处理方案分享

Posted on 2017-08-02 14:54  shihuc  阅读(5546)  评论(0编辑  收藏  举报

今天为了搞压测,定位是不是redis瓶颈。

 

在我们的服务器10.90.2.101上安装了一个redis,版本(redis-3.2.8.tar.gz),没有做任何配置,直接make & make install后,就启动了。 在IDEA里面,将工程的配置文件内容,redis的IP信息改成10.90.2.101.

spring.redis.hostName=10.90.2.101

 

启动我们的应用AI程序,后台老是报错,这个错误,是我们全局锁的逻辑里面,开始以为是setNX的使用有问题。

org.springframework.data.redis.RedisConnectionFailureException: Unexpected end of stream.; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream.
    at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:67)
    at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:41)
    at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37)
    at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37)
    at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:242)
    at org.springframework.data.redis.connection.jedis.JedisConnection.close(JedisConnection.java:299)
    at org.springframework.data.redis.connection.jedis.JedisConnection.<init>(JedisConnecti

分析了一整子,找不出原因,因为,我们这个redis的安装,和另外一个使用很久了的redis没有什么本质差别。

 

网上查看,有我遇到的这种错误。比较多的说法是下面这个:

config set client-output-buffer-limit "normal 1048576 1048576 60 slave 268435456 67108864 60 pubsub 33554432 8388608 60" 

我处理了,但是还是没有用!

 

为了验证,是不是连接没有建立好,就在setNX的前面,执行一次最简单的Set操作:

stringRedisTemplate.opsForValue().set("Hello","TKONLINE");

 

最后,在命令行查看是否写入数据:

127.0.0.1:6379[3]> keys *
(empty list or set)

奇怪吧,没有数据!

 

于是,将redis-cli的指令,指向这台机器的IP,进行再次测试,因为之前也遇到过类似的问题。

[root@localhost home]# redis-cli -h 10.90.2.101 -p 6379
10.90.2.101:6379> select 3
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

这个提示信息,非常有价值,是说,我们的这个redis工作在保护模式,没有绑定IP,无需认证密码。后面还有说,这种模式下,只能支持回环IP地址访问。

有4种解除保护模式的方案:

1) 在回环IP地址连接的情况下,执行  CONFIG SET protected-mode no, 为了永久有效,需要执行  CONFIG REWRITE

2) 修改配置文件,将里面的protected mode option设置为 'no',并重启redis server。推荐使用这种模式!

3) 也可以在启动redis server的时候,指定option。例如 redis-server --protected-mode no

4) 设置一个绑定的IP地址,或者认证密码。

 

我测试过程中,采用的是第一种:

[root@localhost home]# redis-cli
127.0.0.1:6379> CONFIG SET protected-mode no
OK
127.0.0.1:6379> exit
[root@localhost home]# redis-cli -h 10.90.2.101 -p 6379
10.90.2.101:6379> select 3
OK
10.90.2.101:6379[3]> keys *
(empty list or set)
10.90.2.101:6379[3]> keys *
1) "Hello"
10.90.2.101:6379[3]>  CONFIG REWRITE
OK
10.90.2.101:6379[3]> 

 

这样配置之后,再次测试我的AI程序。就没有再报错误!