redis連接錯誤處理方案分享


今天為了搞壓測,定位是不是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程序。就沒有再報錯誤!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM