Redis Q&A (持续更新)

版权声明:欢迎转载,转载请说明出处https://csdn.yanxml.com。大数据Github项目地址https://github.com/SeanYanxml/bigdata。 https://blog.csdn.net/u010416101/article/details/81115815

前言

本章主要记录使用Redis&Redis Cluster&Jedis的过程中遇到的报错与一系列问题。


Q&A

  • 单个Jedis,多线程调用,导致异常。
 * 多线程redis抛出异常B cannot be cast to java.lang.Long
 * https://blog.csdn.net/JavaMoo/article/details/77233976
 * Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long
    at redis.clients.jedis.Connection.getIntegerReply(Connection.java:265)
    at redis.clients.jedis.Jedis.lpush(Jedis.java:882)
    at com.yanxml.redis.demos.mq.RedisMQService.lpush(RedisMQService.java:23)
    at com.yanxml.redis.demos.mq.RedisMQDemoMain.main(RedisMQDemoMain.java:15)
Exception in thread "Thread-0" redis.clients.jedis.exceptions.JedisDataException: ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context
    at redis.clients.jedis.Protocol.processError(Protocol.java:127)
    at redis.clients.jedis.Protocol.process(Protocol.java:161)
    at redis.clients.jedis.Protocol.read(Protocol.java:215)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
    at redis.clients.jedis.Connection.getRawObjectMultiBulkReply(Connection.java:285)
    at redis.clients.jedis.JedisPubSub.process(JedisPubSub.java:121)
    at redis.clients.jedis.JedisPubSub.proceed(JedisPubSub.java:115)
    at redis.clients.jedis.Jedis.subscribe(Jedis.java:2680)
    at com.yanxml.redis.demos.mq.RedisMQService.subscribe(RedisMQService.java:14)
    at com.yanxml.redis.demos.mq.RedisMQSubscribeService$1.run(RedisMQSubscribeService.java:17)
    at java.lang.Thread.run(Thread.java:745)

原因:多个线程同时调用了同一个jedis对象,导致内存数据被多个线程竞争,产生数据混乱
解决方案:每个线程都new出一个自己的jedis对象
Reference:
[1]. 多线程redis抛出异常B cannot be cast to java.lang.Long
[2]. 使用redis时出现java.util.ArrayList cannot be cast to java.lang.Long
[3]. (源码剖析)jedis 用连接池时超时返回值类型错误

  • 运维配置protected-mode no导致保护模式未关闭。
// protected-mode no
//三月 22, 2018 2:20:52 下午 redis.clients.jedis.JedisSentinelPool initSentinels
//信息: Trying to find master from available Sentinels...
//三月 22, 2018 2:20:52 下午 redis.clients.jedis.JedisSentinelPool initSentinels
//警告: Cannot get master address from sentinel running @ 192.168.100.91:26379. Reason: redis.clients.jedis.exceptions.JedisDataException: 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.. Trying next one.
//三月 22, 2018 2:20:52 下午 redis.clients.jedis.JedisSentinelPool initSentinels
//警告: Cannot get master address from sentinel running @ 192.168.100.90:26379. Reason: redis.clients.jedis.exceptions.JedisDataException: 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.. Trying next one.
//Exception in thread "main" java.lang.ExceptionInInitializerError
//Caused by: redis.clients.jedis.exceptions.JedisConnectionException: All sentinels down, cannot determine where is mymaster master is running...
//  at redis.clients.jedis.JedisSentinelPool.initSentinels(JedisSentinelPool.java:180)
//  at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:95)
//  at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:82)
//  at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:70)
//  at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:54)
//  at com.yanxml.redris.sentinel.JedisSentinelPoolConfig.<clinit>(JedisSentinelPoolConfig.java:39)

原因: 远程登录时,redis.conf文件内protected-mode yes导致保护模式未关闭,只能本地登录。
解决办法: 设置protected-mode no,重启Redis实例。

阅读更多

扫码向博主提问

在风中的意志

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • Java
  • 大数据
去开通我的Chat快问
换一批

没有更多推荐了,返回首页