Jedis常见异常汇总

  • A+
所属分类:Redis
高性能企业级服务器首台5折

Jedis虽然使用起来比较简单,但是如果用户不能根据使用场景设置合理的参数(例如连接池参数),如果用户不合理地使用了一些功能(例如Lua和事务)还是会产生很多问题,本文对这些问题逐一说明:

一.无法从连接池获取到Jedis连接

1.异常堆栈

(1) 连接池参数blockWhenExhausted = true(默认)

如果连接池没有可用Jedis连接,会等待maxWaitMillis(毫秒),如果依然没有获取到可用Jedis连接,会抛出如下异常:

  1. 1
    <span class="pln">redis</span><span class="pun">.</span><span class="pln">clients</span><span class="pun">.</span><span class="pln">jedis</span><span class="pun">.</span><span class="pln">exceptions</span><span class="pun">.</span><span class="typ">JedisConnectionException</span><span class="pun">:</span> <span class="typ">Could</span> <span class="kwd"><span class="hljs-keyword">not</span></span> <span class="kwd"><span class="hljs-builtin-name">get</span></span><span class="pln"> a<span class="hljs-built_in"> resource </span></span><span class="kwd"><span class="hljs-keyword">from</span></span><span class="pln"> the pool</span>
  2. 1
        <span class="pun">…</span>
  3. 1
    <span class="typ">Caused</span> <span class="kwd">by</span><span class="pun">:</span><span class="pln"> java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">util</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">NoSuchElementException</span></span><span class="pun">:</span> <span class="typ">Timeout</span><span class="pln"> waiting </span><span class="kwd"><span class="hljs-keyword">for</span></span><span class="pln"> idle </span><span class="kwd">object</span>
  4. 1
    <span class="pln">    at org</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">apache</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">commons</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">pool2</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">impl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">GenericObjectPool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">borrowObject</span></span><span class="pun">(</span><span class="typ">GenericObjectPool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">449</span></span><span class="pun">)</span>

(2) 连接池参数blockWhenExhausted = false

设置该参数,如果连接池没有可用的Jedis连接,立即抛出异常:

  1. 1
    <span class="pln">redis</span><span class="pun">.</span><span class="pln">clients</span><span class="pun">.</span><span class="pln">jedis</span><span class="pun">.</span><span class="pln">exceptions</span><span class="pun">.</span><span class="typ">JedisConnectionException</span><span class="pun">:</span> <span class="typ">Could</span> <span class="kwd"><span class="hljs-keyword">not</span></span> <span class="kwd"><span class="hljs-builtin-name">get</span></span><span class="pln"> a<span class="hljs-built_in"> resource </span></span><span class="kwd"><span class="hljs-keyword">from</span></span><span class="pln"> the pool</span>
  2. 1
        <span class="pun">…</span>
  3. 1
    <span class="typ">Caused</span> <span class="kwd">by</span><span class="pun">:</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">util</span><span class="pun">.</span><span class="typ">NoSuchElementException</span><span class="pun">:</span> <span class="typ"><span class="hljs-built_in">Pool</span></span><span class="pln"> exhausted</span>
  4. 1
    <span class="pln">    at org</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">apache</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">commons</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">pool2</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">impl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">GenericObjectPool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">borrowObject</span></span><span class="pun">(</span><span class="typ">GenericObjectPool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">464</span></span><span class="pun">)</span>

2.异常描述

1

上述异常是客户端没有从连接池(最大数量maxTotal)获得可用的Jedis连接造成的,可能有如下原因:

(1) 连接泄露 (较为常见

JedisPool默认的maxTotal=8,下面的代码从JedisPool中获取了8个Jedis资源,但是没有归还资源,当第9次尝试获取Jedis资源的时候,无法获得(jedisPool.getResource().ping())

  1. 1
    <span class="typ"><span class="hljs-attribute">GenericObjectPoolConfig</span></span><span class="pln"><span class="hljs-attribute"> poolConfig</span> </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">GenericObjectPoolConfig</span><span class="pun">();</span>
  2. 1
    <span class="typ"><span class="hljs-keyword">JedisPool</span></span><span class="pln"> <span class="hljs-keyword">jedisPool </span></span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ"><span class="hljs-keyword">JedisPool</span></span><span class="pun"><span class="hljs-keyword">(</span></span><span class="pln"><span class="hljs-keyword">poolConfig</span></span><span class="pun"><span class="hljs-keyword">,</span></span> <span class="str"><span class="hljs-string">"127.0.0.1"</span></span><span class="pun">,</span> <span class="lit"><span class="hljs-number">6379</span></span><span class="pun">)<span class="hljs-comment">;</span></span>
  3. 1
    <span class="com"><span class="hljs-comment">//向JedisPool借用8次连接,但是没有执行归还操作。</span></span>
  4. 1
    <span class="kwd"><span class="hljs-keyword">for</span></span> <span class="pun">(</span><span class="kwd">int</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">=</span> <span class="lit"><span class="hljs-number">0</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">&lt;</span> <span class="lit"><span class="hljs-number">8</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span></span><span class="pun">++)</span> <span class="pun">{</span>
  5. 1
        <span class="typ"><span class="hljs-keyword">Jedis</span></span><span class="pln"> <span class="hljs-keyword">jedis </span></span><span class="pun">=</span> <span class="kwd">null</span><span class="pun"><span class="hljs-comment">;</span></span>
  6. 1
        <span class="kwd"><span class="hljs-keyword">try</span></span> <span class="pun">{</span>
  7. 1
    <span class="pln"><span class="hljs-attribute">        jedis</span> </span><span class="pun">=</span><span class="pln"> jedisPool</span><span class="pun">.</span><span class="pln">getResource</span><span class="pun">();</span>
  8. 1
    <span class="pln">        jedis</span><span class="pun">.</span><span class="pln">ping</span><span class="pun">()<span class="hljs-comment">;</span></span>
  9. 1
        <span class="pun">}</span> <span class="kwd"><span class="hljs-keyword">catch</span></span> <span class="pun">(</span><span class="typ"><span class="hljs-built_in">Exception</span></span><span class="pln"> <span class="hljs-literal">e</span></span><span class="pun">)</span> <span class="pun">{</span>
  10. 1
    <span class="pln">        <span class="hljs-selector-tag">logger</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">error</span></span><span class="pun">(</span><span class="pln"><span class="hljs-selector-tag">e</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">getMessage</span></span><span class="pun">(),</span><span class="pln"> <span class="hljs-selector-tag">e</span></span><span class="pun">);</span>
  11. 1
        <span class="pun">}</span>
  12. 1
    <span class="pun">}</span>
  13. 1
    <span class="pln"><span class="hljs-selector-tag">jedisPool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">getResource</span></span><span class="pun">()<span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">ping</span></span><span class="pun">();</span>

所以推荐使用的代码规范是:

  1. 1
    <span class="pun">执行命令如下:</span>
  2. 1
    <span class="typ"><span class="hljs-keyword">Jedis</span></span><span class="pln"> <span class="hljs-keyword">jedis </span></span><span class="pun">=</span> <span class="kwd">null</span><span class="pun"><span class="hljs-comment">;</span></span>
  3. 1
    <span class="kwd"><span class="hljs-keyword">try</span></span> <span class="pun">{</span>
  4. 1
    <span class="pln"><span class="hljs-attribute">    jedis</span> </span><span class="pun">=</span><span class="pln"> jedisPool</span><span class="pun">.</span><span class="pln">getResource</span><span class="pun">();</span>
  5. 1
        <span class="com"><span class="hljs-comment">//具体的命令</span></span>
  6. 1
    <span class="pln">    <span class="hljs-selector-tag">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">executeCommand</span></span><span class="pun">()</span>
  7. 1
    <span class="pun">}</span> <span class="kwd"><span class="hljs-keyword">catch</span></span> <span class="pun">(</span><span class="typ"><span class="hljs-built_in">Exception</span></span><span class="pln"> <span class="hljs-literal">e</span></span><span class="pun">)</span> <span class="pun">{</span>
  8. 1
        <span class="com">//如果命令有<span class="hljs-built_in">key</span>最好把<span class="hljs-built_in">key</span>也在错误日志打印出来,对于集群版来说通过<span class="hljs-built_in">key</span>可以帮助定位到具体节点。</span>
  9. 1
    <span class="pln">    <span class="hljs-selector-tag">logger</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">error</span></span><span class="pun">(</span><span class="pln"><span class="hljs-selector-tag">e</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">getMessage</span></span><span class="pun">(),</span><span class="pln"> <span class="hljs-selector-tag">e</span></span><span class="pun">);</span>
  10. 1
    <span class="pun">}</span> <span class="kwd"><span class="hljs-keyword">finally</span></span> <span class="pun">{</span>
  11. 1
        <span class="com"><span class="hljs-comment">//注意这里不是关闭连接,在JedisPool模式下,Jedis会被归还给资源池。</span></span>
  12. 1
        <span class="kwd"><span class="hljs-keyword">if</span></span> <span class="pun">(</span><span class="pln">jedis </span><span class="pun">!=</span> <span class="kwd"><span class="hljs-literal">null</span></span><span class="pun">)</span>
  13. 1
    <span class="pln">        jedis</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()<span class="hljs-comment">;</span></span>
  14. 1
    <span class="pun">}</span>

(2) 业务并发量大,maxTotal设置得过小了。

举个例子:

  • 一次命令时间(borrow|return resource + Jedis执行命令(含网络) )的平均耗时约为1ms,一个连接的QPS大约是1000
  • 业务期望的QPS是50000

那么理论上需要的资源池大小是50000 / 1000 = 50个,实际maxTotal可以根据理论值进行微调。

(3) Jedis连接阻塞

例如Redis发生了阻塞(例如慢查询等原因),所有连接在超时时间范围内等待,并发量较大时,会造成连接池资源不足。

(4) Jedis连接被拒绝

从池子里拿连接,由于没有空闲连接,需要重新生成一个Jedis连接,但是连接被拒绝:

  1. 1
    <span class="pln">redis</span><span class="pun">.</span><span class="pln">clients</span><span class="pun">.</span><span class="pln">jedis</span><span class="pun">.</span><span class="pln">exceptions</span><span class="pun">.</span><span class="typ">JedisConnectionException</span><span class="pun">:</span> <span class="typ">Could</span> <span class="kwd"><span class="hljs-keyword">not</span></span> <span class="kwd"><span class="hljs-builtin-name">get</span></span><span class="pln"> a<span class="hljs-built_in"> resource </span></span><span class="kwd"><span class="hljs-keyword">from</span></span><span class="pln"> the pool</span>
  2. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">util</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Pool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">getResource</span></span><span class="pun">(</span><span class="typ">Pool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">50</span></span><span class="pun">)</span>
  3. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisPool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">getResource</span></span><span class="pun">(</span><span class="typ">JedisPool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">99</span></span><span class="pun">)</span>
  4. 1
    <span class="pln">    <span class="hljs-selector-tag">at</span> </span><span class="typ"><span class="hljs-selector-tag">TestAdmin</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">main</span></span><span class="pun">(</span><span class="typ"><span class="hljs-selector-tag">TestAdmin</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun"><span class="hljs-selector-pseudo">:</span></span><span class="lit"><span class="hljs-selector-pseudo">14</span></span><span class="pun"><span class="hljs-selector-pseudo">)</span></span>
  5. 1
    <span class="typ">Caused</span> <span class="kwd">by</span><span class="pun">:</span><span class="pln"> redis</span><span class="pun">.</span><span class="pln">clients</span><span class="pun">.</span><span class="pln">jedis</span><span class="pun">.</span><span class="pln">exceptions</span><span class="pun">.</span><span class="typ">JedisConnectionException</span><span class="pun">:</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">net</span><span class="pun">.</span><span class="typ">ConnectException</span><span class="pun">:</span> <span class="typ"><span class="hljs-built_in">Connection</span></span><span class="pln"> refused</span>
  6. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Connection</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">connect</span></span><span class="pun">(</span><span class="typ">Connection</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">164</span></span><span class="pun">)</span>
  7. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">BinaryClient</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">connect</span></span><span class="pun">(</span><span class="typ">BinaryClient</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">80</span></span><span class="pun">)</span>
  8. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">BinaryJedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">connect</span></span><span class="pun">(</span><span class="typ">BinaryJedis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">1676</span></span><span class="pun">)</span>
  9. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisFactory</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">makeObject</span></span><span class="pun">(</span><span class="typ">JedisFactory</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">87</span></span><span class="pun">)</span>
  10. 1
    <span class="pln">    at org</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">apache</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">commons</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">pool2</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">impl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">GenericObjectPool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">create</span></span><span class="pun">(</span><span class="typ">GenericObjectPool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">861</span></span><span class="pun">)</span>
  11. 1
    <span class="pln">    at org</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">apache</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">commons</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">pool2</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">impl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">GenericObjectPool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">borrowObject</span></span><span class="pun">(</span><span class="typ">GenericObjectPool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">435</span></span><span class="pun">)</span>
  12. 1
    <span class="pln">    at org</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">apache</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">commons</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">pool2</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">impl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">GenericObjectPool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">borrowObject</span></span><span class="pun">(</span><span class="typ">GenericObjectPool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">363</span></span><span class="pun">)</span>
  13. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">util</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Pool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">getResource</span></span><span class="pun">(</span><span class="typ">Pool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">48</span></span><span class="pun">)</span>
  14. 1
        <span class="pun">...</span> <span class="lit"><span class="hljs-number">2</span></span><span class="pln"> more</span>
  15. 1
    <span class="typ">Caused</span> <span class="kwd">by</span><span class="pun">:</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">net</span><span class="pun">.</span><span class="typ">ConnectException</span><span class="pun">:</span> <span class="typ"><span class="hljs-built_in">Connection</span></span><span class="pln"> refused</span>
  16. 1
    <span class="pln">    at java</span><span class="pun">.</span><span class="pln">net</span><span class="pun">.</span><span class="typ">PlainSocketImpl</span><span class="pun">.</span><span class="pln">socketConnect</span><span class="pun">(</span><span class="typ">Native</span> <span class="typ"><span class="hljs-function"><span class="hljs-keyword">Method</span></span></span><span class="pun"><span class="hljs-function">)</span></span>
  17. 1
    <span class="pln">    at java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">net</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">AbstractPlainSocketImpl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">doConnect</span></span><span class="pun">(</span><span class="typ">AbstractPlainSocketImpl</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">339</span></span><span class="pun">)</span>
  18. 1
    <span class="pln">    at java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">net</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">AbstractPlainSocketImpl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">connectToAddress</span></span><span class="pun">(</span><span class="typ">AbstractPlainSocketImpl</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">200</span></span><span class="pun">)</span>
  19. 1
    <span class="pln">    at java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">net</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">AbstractPlainSocketImpl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">connect</span></span><span class="pun">(</span><span class="typ">AbstractPlainSocketImpl</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">182</span></span><span class="pun">)</span>
  20. 1
    <span class="pln">    at java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">net</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">SocksSocketImpl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">connect</span></span><span class="pun">(</span><span class="typ">SocksSocketImpl</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">392</span></span><span class="pun">)</span>
  21. 1
    <span class="pln">    at java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">net</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Socket</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">connect</span></span><span class="pun">(</span><span class="typ">Socket</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">579</span></span><span class="pun">)</span>
  22. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Connection</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">connect</span></span><span class="pun">(</span><span class="typ">Connection</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">158</span></span><span class="pun">)</span>
  23. 1
        <span class="pun">...</span> <span class="lit"><span class="hljs-number">9</span></span><span class="pln"> more</span>

可以从at redis.clients.jedis.Connection.connect(Connection.java:158)中看到实际是一个Socket连接:

  1. 1
    <span class="pln"> <span class="hljs-built_in">socket</span></span><span class="pun">.</span><span class="pln">setSoLinger</span><span class="pun">(</span><span class="kwd"><span class="hljs-literal">true</span></span><span class="pun">,</span> <span class="lit"><span class="hljs-number">0</span></span><span class="pun">);</span> <span class="com"><span class="hljs-comment">// Control calls close () method,</span></span>
  2. 1
            <span class="com"><span class="hljs-comment">// the underlying socket is closed</span></span>
  3. 1
            <span class="com"><span class="hljs-comment">// immediately</span></span>
  4. 1
            <span class="com"><span class="hljs-comment">// &lt;-@wjw_add</span></span>
  5. 1
     
  6. 1
    <span class="lit"><span class="hljs-number">158</span></span><span class="pun">:</span><span class="pln">  socket</span><span class="pun">.</span><span class="pln"><span class="hljs-built_in">connect</span></span><span class="pun">(</span><span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ">InetSocketAddress</span><span class="pun">(</span><span class="pln">host</span><span class="pun">,</span><span class="pln"> port</span><span class="pun">),</span><span class="pln"> connectionTimeout</span><span class="pun">);</span>

一般这种需要检查Redis的域名配置是否正确,排查该段时间网络是否正常

(4) 其他问题

例如丢包、DNS、客户端TCP参数配置,具体可以参考:Jedis介绍及常见问题分析

3.解决方法

从上述分析,可以看出这个问题的原因比较复杂,请不要简单地认为连接池不够就盲目加大maxTotal,需要具体问题具体分析。

连接池参数优化可以参考:JedisPool资源池优化

4.处理途径

请客户先按照上述表述进行问题定位和故障排除,如无法自行解决故障,请提交工单。

二、客户端缓冲区异常

1.异常堆栈

  1. 1
    <span class="pln"><span class="hljs-selector-tag">redis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisConnectionException</span></span><span class="pun">:</span> <span class="typ"><span class="hljs-selector-tag">Unexpected</span></span> <span class="kwd"><span class="hljs-selector-tag">end</span></span><span class="pln"> <span class="hljs-selector-tag">of</span> <span class="hljs-selector-tag">stream</span></span><span class="pun">.</span>
  2. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">util</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">RedisInputStream</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">ensureFill</span></span><span class="pun">(</span><span class="typ">RedisInputStream</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">199</span></span><span class="pun">)</span>
  3. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">util</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">RedisInputStream</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">readByte</span></span><span class="pun">(</span><span class="typ">RedisInputStream</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">40</span></span><span class="pun">)</span>
  4. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">process</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">151</span></span><span class="pun">)</span>
  5. 1
    <span class="pun">......</span>

2.异常描述

这个异常是客户端缓冲区异常,产生这个问题可能有三个原因:

(1) 常见原因:多个线程使用一个Jedis连接,正常的情况是一个线程使用一个Jedis连接,可以使用JedisPool管理Jedis连接,实现线程安全,避免出现这种情况。例如下面代码就是两个线程共用了一个Jedis连接:

  1. 1
    <span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ"><span class="hljs-type">Thread</span></span><span class="pun">(</span><span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ"><span class="hljs-type">Runnable</span></span><span class="pun">()</span> <span class="pun">{</span>
  2. 1
     
  3. 1
        <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span> <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="pln"><span class="hljs-function"> <span class="hljs-title">run</span></span></span><span class="pun"><span class="hljs-function"><span class="hljs-params">()</span></span></span> <span class="pun">{</span>
  4. 1
            <span class="kwd"><span class="hljs-keyword">for</span></span> <span class="pun">(</span><span class="kwd">int</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">=</span> <span class="lit"><span class="hljs-number">0</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">&lt;</span> <span class="lit"><span class="hljs-number">100</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span></span><span class="pun">++)</span> <span class="pun">{</span>
  5. 1
    <span class="pln">            jedis</span><span class="pun">.</span><span class="kwd"><span class="hljs-builtin-name">get</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"hello"</span></span><span class="pun">);</span>
  6. 1
            <span class="pun">}</span>
  7. 1
        <span class="pun">}</span>
  8. 1
    <span class="pun">}).</span><span class="pln">start</span><span class="pun">()<span class="hljs-comment">;</span></span>
  9. 1
     
  10. 1
    <span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ"><span class="hljs-type">Thread</span></span><span class="pun">(</span><span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ"><span class="hljs-type">Runnable</span></span><span class="pun">()</span> <span class="pun">{</span>
  11. 1
     
  12. 1
        <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span> <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="pln"><span class="hljs-function"> <span class="hljs-title">run</span></span></span><span class="pun"><span class="hljs-function"><span class="hljs-params">()</span></span></span> <span class="pun">{</span>
  13. 1
            <span class="kwd"><span class="hljs-keyword">for</span></span> <span class="pun">(</span><span class="kwd">int</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">=</span> <span class="lit"><span class="hljs-number">0</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">&lt;</span> <span class="lit"><span class="hljs-number">100</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span></span><span class="pun">++)</span> <span class="pun">{</span>
  14. 1
    <span class="pln">            <span class="hljs-selector-tag">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">hget</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"haskey"</span></span><span class="pun">,</span> <span class="str"><span class="hljs-string">"f"</span></span><span class="pun">);</span>
  15. 1
            <span class="pun">}</span>
  16. 1
        <span class="pun">}</span>
  17. 1
    <span class="pun">}).</span><span class="pln">start</span><span class="pun">()<span class="hljs-comment">;</span></span>

(2) 客户端缓冲区满了

Redis有三种客户端缓冲区:

  • 普通客户端缓冲区(normal):用于接受普通的命令,例如get、set、mset、hgetall、zrange等。
  • slave客户端缓冲区(slave):用于同步master节点的写命令,完成复制。
  • 发布订阅缓冲区(pubsub):pubsub不是普通的命令,因此有单独的缓冲区。

客户端缓冲区

Redis客户端缓冲区配置的格式是:

  1. 1
    <span class="pln">client</span><span class="pun">-</span><span class="pln">output</span><span class="pun">-</span><span class="pln">buffer</span><span class="pun">-</span><span class="pln">limit </span><span class="str">&lt;<span class="hljs-class"><span class="hljs-keyword">class</span>&gt;</span></span> <span class="pun"><span class="hljs-class">&lt;</span></span><span class="pln"><span class="hljs-class">hard <span class="hljs-title">limit</span></span></span><span class="pun"><span class="hljs-class">&gt;</span></span> <span class="pun"><span class="hljs-class">&lt;</span></span><span class="pln"><span class="hljs-class">soft <span class="hljs-title">limit</span></span></span><span class="pun"><span class="hljs-class">&gt;</span></span> <span class="pun"><span class="hljs-class">&lt;</span></span><span class="pln"><span class="hljs-class">soft <span class="hljs-title">seconds</span></span></span><span class="pun"><span class="hljs-class">&gt;</span></span>
  • class: 客户端类型:可选值为normal、slave 和 pubsub。
  • hard limit: 如果客户端使用的输出缓冲区大于hard limit,客户端会被立即关闭,单位为秒。
  • soft limit和soft seconds: 如果客户端使用的输出缓冲区超过了soft limit并且持续了soft limit秒,客户端会被立即关闭,单位为秒。

例如下面是一份Redis缓冲区的配置,所以当条件满足时,客户端连接会被关闭,就会出现

1
Unexpected end of stream

  1. 1
    <span class="pln">redis</span><span class="pun">&gt;</span><span class="pln"><span class="hljs-built_in"> config </span></span><span class="kwd"><span class="hljs-builtin-name">get</span></span><span class="pln"> client</span><span class="pun">-</span><span class="pln">output</span><span class="pun">-</span><span class="pln">buffer</span><span class="pun">-</span><span class="pln">limit</span>
  2. 1
    <span class="lit"><span class="hljs-number">1</span></span><span class="pun">)</span> <span class="str"><span class="hljs-string">"client-output-buffer-limit"</span></span>
  3. 1
    <span class="lit"><span class="hljs-number">2</span></span><span class="pun">)</span> <span class="str">"normal <span class="hljs-number">524288000</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> slave <span class="hljs-number">2147483648</span> <span class="hljs-number">536870912</span> <span class="hljs-number">480</span> pubsub <span class="hljs-number">33554432</span> <span class="hljs-number">8388608 60</span>"</span>

(3) 长时间闲置连接会被服务端主动断开,可以查询timeout配置的设置以及自身连接池配置确定是否需要做空闲检测。

3.解决方法和处理途径

客户:排查自身代码是否使用JedisPool管理Jedis连接,是否存在并发操作Jedis的情况。

工单: 排查是否上述(2)(3)原因,将阿里云Redis中timeout设置为

1
0

,表示不主动关闭空闲连接,将缓冲区设置为

1
0 0 0

,表示不对客户端缓冲区进行限制。经过上述设置,一般可以解决问题。

三、非法的客户端地址 (阿里云Redis提供客户端白名单功能)

1.异常堆栈

  1. 1
    <span class="typ">Caused</span> <span class="kwd">by</span><span class="pun">:</span><span class="pln"> redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisDataException</span></span><span class="pun">:</span><span class="pln"> ERR illegal address</span>
  2. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">processError</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">117</span></span><span class="pun">)</span>
  3. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">process</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">151</span></span><span class="pun">)</span>
  4. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">read</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">205</span></span><span class="pun">)</span>
  5. 1
        <span class="pun">......</span>

2.异常描述

Redis实例配置了白名单,但当前访问Redis的客户端(IP)不在白名单中。

3.解决方法

添加该客户端(IP)的白名单。

4.处理途径

客户自行操作或者提交工单。

四、客户端连接数达到最大值

1.异常堆栈

  1. 1
    <span class="pln">redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisDataException</span></span><span class="pun">:</span><span class="pln"> ERR max number of clients reached</span>

2.异常描述

客户端连接数超过了Redis实例配置的最大maxclients。

3.解决方法

用户可提工单,工作人员可临时调大最大连接数,帮助用户找到连接数暴涨的原因(因为上述调整只是临时调整)。

4.处理途径

  • 工单:临时调整最大连接数,协助定位问题。
  • 客户:定位自身问题(可以定位连接最多的客户端),找到问题原因(例如连接池配置等)

五、客户端读写超时

1.异常堆栈

  1. 1
    <span class="pln">redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisConnectionException</span></span><span class="pun">:</span><span class="pln"> java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">net</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">SocketTimeoutException</span></span><span class="pun">:</span> <span class="typ">Read</span><span class="pln"> timed </span><span class="kwd">out</span>

2.异常描述

问题原因可能有如下几种:(1) 读写超时设置的过短。(2) 有慢查询或者Redis发生阻塞。(3) 网络不稳定。

3.解决方法

客户提供读写超时时间,提交工单定位相关原因。

4.处理途径

工单。

六、密码相关的异常

1.异常堆栈

Redis设置了密码鉴权,客户端请求没有提供密码:

  1. 1
    <span class="typ">Exception</span> <span class="kwd"><span class="hljs-keyword">in</span></span><span class="pln"> thread </span><span class="str"><span class="hljs-string">"main"</span></span><span class="pln"> redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisDataException</span></span><span class="pun">:</span><span class="pln"> NOAUTH </span><span class="typ">Authentication</span><span class="pln"> required</span><span class="pun">.</span>
  2. 1
    <span class="pln">     at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">processError</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">127</span></span><span class="pun">)</span>
  3. 1
    <span class="pln">     at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">process</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">161</span></span><span class="pun">)</span>
  4. 1
    <span class="pln">     at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">read</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">215</span></span><span class="pun">)</span>

Redis没有设置密码鉴权,客户端请求中包含了密码:

  1. 1
    <span class="typ">Exception</span> <span class="kwd"><span class="hljs-keyword">in</span></span><span class="pln"> thread </span><span class="str"><span class="hljs-string">"main"</span></span><span class="pln"> redis</span><span class="pun">.</span><span class="pln">clients</span><span class="pun">.</span><span class="pln">jedis</span><span class="pun">.</span><span class="pln">exceptions</span><span class="pun">.</span><span class="typ">JedisDataException</span><span class="pun">:</span><span class="pln"> ERR </span><span class="typ"><span class="hljs-built_in">Client</span></span><span class="pln"> sent AUTH</span><span class="pun">,</span><span class="pln"> but </span><span class="kwd"><span class="hljs-literal">no</span></span><span class="pln"> password </span><span class="kwd">is</span> <span class="kwd">set</span>
  2. 1
    <span class="pln">     at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">processError</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">127</span></span><span class="pun">)</span>
  3. 1
    <span class="pln">     at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">process</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">161</span></span><span class="pun">)</span>
  4. 1
    <span class="pln">     at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">read</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">215</span></span><span class="pun">)</span>

客户端传输了错误的密码:

  1. 1
    <span class="pln">redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisDataException</span></span><span class="pun">:</span><span class="pln"> ERR invalid password</span>
  2. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">processError</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">117</span></span><span class="pun">)</span>
  3. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">process</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">151</span></span><span class="pun">)</span>
  4. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">read</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">205</span></span><span class="pun">)</span>

2.解决方法:弄清楚到底有没有设置密码鉴权,是否提供了正确的密码。

七、事务异常

1.异常堆栈

  1. 1
    <span class="pln">redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisDataException</span></span><span class="pun">:</span><span class="pln"> EXECABORT </span><span class="typ">Transaction</span><span class="pln"> discarded because of previous errors</span>

2.异常描述

这个是Redis的事务异常:事务中包含了错误的命令,例如如下sett是个不存在的命令。

  1. 1
    <span class="lit">127<span class="hljs-selector-class">.0</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="lit"><span class="hljs-selector-class">0</span><span class="hljs-selector-class">.1</span></span><span class="pun"><span class="hljs-selector-pseudo">:</span></span><span class="lit"><span class="hljs-selector-pseudo">6379</span></span><span class="pun">&gt;</span><span class="pln"> <span class="hljs-selector-tag">multi</span></span>
  2. 1
    <span class="pln"><span class="hljs-attribute">OK</span></span>
  3. 1
    <span class="lit">127<span class="hljs-selector-class">.0</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="lit"><span class="hljs-selector-class">0</span><span class="hljs-selector-class">.1</span></span><span class="pun"><span class="hljs-selector-pseudo">:</span></span><span class="lit"><span class="hljs-selector-pseudo">6379</span></span><span class="pun">&gt;</span><span class="pln"> <span class="hljs-selector-tag">sett</span> <span class="hljs-selector-tag">key</span> <span class="hljs-selector-tag">world</span></span>
  4. 1
    <span class="pun">(</span><span class="pln">error</span><span class="pun">)</span><span class="pln"> ERR unknown <span class="hljs-keyword">command</span> </span><span class="str"><span class="hljs-string">'sett'</span></span>
  5. 1
    <span class="lit">127<span class="hljs-selector-class">.0</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="lit"><span class="hljs-selector-class">0</span><span class="hljs-selector-class">.1</span></span><span class="pun"><span class="hljs-selector-pseudo">:</span></span><span class="lit"><span class="hljs-selector-pseudo">6379</span></span><span class="pun">&gt;</span><span class="pln"> <span class="hljs-selector-tag">incr</span> <span class="hljs-selector-tag">counter</span></span>
  6. 1
    <span class="pln"><span class="hljs-attribute">QUEUED</span></span>
  7. 1
    <span class="lit">127<span class="hljs-selector-class">.0</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="lit"><span class="hljs-selector-class">0</span><span class="hljs-selector-class">.1</span></span><span class="pun"><span class="hljs-selector-pseudo">:</span></span><span class="lit"><span class="hljs-selector-pseudo">6379</span></span><span class="pun">&gt;</span> <span class="kwd"><span class="hljs-selector-tag">exec</span></span>
  8. 1
    <span class="pun">(</span><span class="pln"><span class="hljs-keyword">error</span></span><span class="pun">)</span><span class="pln"> EXECABORT </span><span class="typ">Transaction</span><span class="pln"> discarded because <span class="hljs-keyword">of</span> previous errors</span><span class="pun">.</span>

3.解决方法和处理途径

客户修复自身代码错误。

八、类转换错误

1.异常堆栈

  1. 1
    <span class="pln">java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">lang</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">ClassCastException</span></span><span class="pun">:</span><span class="pln"> java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">lang</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Long</span></span><span class="pln"> cannot be cast to java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">util</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">List</span></span>
  2. 1
    <span class="pln">         at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Connection</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">getBinaryMultiBulkReply</span></span><span class="pun">(</span><span class="typ">Connection</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">199</span></span><span class="pun">)</span>
  3. 1
    <span class="pln">         at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">hgetAll</span></span><span class="pun">(</span><span class="typ">Jedis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">851</span></span><span class="pun">)</span>
  4. 1
    <span class="pln">         at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">ShardedJedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">hgetAll</span></span><span class="pun">(</span><span class="typ">ShardedJedis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">198</span></span><span class="pun">)</span>
  1. 1
    <span class="pln">java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">lang</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">ClassCastException</span></span><span class="pun">:</span><span class="pln"> java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">util</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">ArrayList</span></span><span class="pln"> cannot be cast to </span><span class="pun">[</span><span class="pln">B</span>
  2. 1
    <span class="pln">         at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Connection</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">getBinaryBulkReply</span></span><span class="pun">(</span><span class="typ">Connection</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">182</span></span><span class="pun">)</span>
  3. 1
    <span class="pln">         at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Connection</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">getBulkReply</span></span><span class="pun">(</span><span class="typ">Connection</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">171</span></span><span class="pun">)</span>
  4. 1
    <span class="pln">         at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">rpop</span></span><span class="pun">(</span><span class="typ">Jedis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">1109</span></span><span class="pun">)</span>
  5. 1
    <span class="pln">         at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">ShardedJedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">rpop</span></span><span class="pun">(</span><span class="typ">ShardedJedis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">258</span></span><span class="pun">)</span>
  6. 1
    <span class="pun">.......</span>

2.异常描述

Jedis正确的使用方法是:一个线程操作一个Jedis,如果多个线程操作同一个Jedis连接就会发生此类错误。使用JedisPool可避免此类问题。例如如下代码在两个线程并发使用了一个Jedis(get、hgetAll返回不同的类型)。

  1. 1
     
  2. 1
    <span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ"><span class="hljs-type">Thread</span></span><span class="pun">(</span><span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ"><span class="hljs-type">Runnable</span></span><span class="pun">()</span> <span class="pun">{</span>
  3. 1
     
  4. 1
        <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span> <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="pln"><span class="hljs-function"> <span class="hljs-title">run</span></span></span><span class="pun"><span class="hljs-function"><span class="hljs-params">()</span></span></span> <span class="pun">{</span>
  5. 1
            <span class="kwd"><span class="hljs-keyword">for</span></span> <span class="pun">(</span><span class="kwd">int</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">=</span> <span class="lit"><span class="hljs-number">0</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">&lt;</span> <span class="lit"><span class="hljs-number">100</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span></span><span class="pun">++)</span> <span class="pun">{</span>
  6. 1
    <span class="pln">            jedis</span><span class="pun">.</span><span class="kwd"><span class="hljs-builtin-name">set</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"hello"</span></span><span class="pun">,</span> <span class="str"><span class="hljs-string">"world"</span></span><span class="pun">);</span>
  7. 1
    <span class="pln">            jedis</span><span class="pun">.</span><span class="kwd"><span class="hljs-builtin-name">get</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"hello"</span></span><span class="pun">);</span>
  8. 1
            <span class="pun">}</span>
  9. 1
        <span class="pun">}</span>
  10. 1
    <span class="pun">}).</span><span class="pln">start</span><span class="pun">()<span class="hljs-comment">;</span></span>
  11. 1
     
  12. 1
    <span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ"><span class="hljs-type">Thread</span></span><span class="pun">(</span><span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ"><span class="hljs-type">Runnable</span></span><span class="pun">()</span> <span class="pun">{</span>
  13. 1
     
  14. 1
        <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span> <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="pln"><span class="hljs-function"> <span class="hljs-title">run</span></span></span><span class="pun"><span class="hljs-function"><span class="hljs-params">()</span></span></span> <span class="pun">{</span>
  15. 1
            <span class="kwd"><span class="hljs-keyword">for</span></span> <span class="pun">(</span><span class="kwd">int</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">=</span> <span class="lit"><span class="hljs-number">0</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">&lt;</span> <span class="lit"><span class="hljs-number">100</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span></span><span class="pun">++)</span> <span class="pun">{</span>
  16. 1
    <span class="pln">            <span class="hljs-selector-tag">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">hset</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"hashkey"</span></span><span class="pun">,</span> <span class="str"><span class="hljs-string">"f"</span></span><span class="pun">,</span> <span class="str"><span class="hljs-string">"v"</span></span><span class="pun">);</span>
  17. 1
    <span class="pln">            jedis</span><span class="pun">.</span><span class="pln">hgetAll</span><span class="pun">(</span><span class="str"><span class="hljs-string">"hashkey"</span></span><span class="pun">)<span class="hljs-comment">;</span></span>
  18. 1
            <span class="pun">}</span>
  19. 1
        <span class="pun">}</span>
  20. 1
    <span class="pun">}).</span><span class="pln">start</span><span class="pun">()<span class="hljs-comment">;</span></span>

3.解决方法和处理途径

客户排查自身代码是否存在上述问题

九、命令使用错误

1.异常堆栈

  1. 1
    <span class="typ">Exception</span> <span class="kwd"><span class="hljs-keyword">in</span></span><span class="pln"> thread </span><span class="str"><span class="hljs-string">"main"</span></span><span class="pln"> redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisDataException</span></span><span class="pun">:</span><span class="pln"> WRONGTYPE </span><span class="typ">Operation</span><span class="pln"> against <span class="hljs-selector-tag">a</span> key holding the wrong kind of value</span>
  2. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">processError</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">127</span></span><span class="pun">)</span>
  3. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">process</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">161</span></span><span class="pun">)</span>
  4. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Protocol</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">read</span></span><span class="pun">(</span><span class="typ">Protocol</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">215</span></span><span class="pun">)</span>
  5. 1
    <span class="pun">.....</span>

2.异常描述

例如key=”hello”是字符串类型的键,而hgetAll返回哈希类型的键,所以出现了错误。

  1. 1
    <span class="pln">jedis</span><span class="pun">.</span><span class="kwd"><span class="hljs-builtin-name">set</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"hello"</span></span><span class="pun">,</span><span class="str"><span class="hljs-string">"world"</span></span><span class="pun">);</span>
  2. 1
    <span class="pln">jedis</span><span class="pun">.</span><span class="pln">hgetAll</span><span class="pun">(</span><span class="str"><span class="hljs-string">"hello"</span></span><span class="pun">)<span class="hljs-comment">;</span></span>

3.解决方法和处理途径

请客户修改自身代码错误。

十、Redis使用的内存超过maxmemory配置

1.异常堆栈

  1. 1
    <span class="pln">redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisDataException</span></span><span class="pun">:</span><span class="pln"> OOM command </span><span class="kwd">not</span><span class="pln"> allowed </span><span class="kwd">when</span><span class="pln"> used memory </span><span class="pun">&gt;</span> <span class="str"><span class="hljs-string">'maxmemory'</span></span><span class="pun">.</span>

2.异常描述

Redis节点(如果是集群,则是其中一个节点)使用内存大于该实例的内存规格(maxmemory配置)。

3.解决方法

原因可能有如下几种:

  • 业务数据正常增加
  • 客户端缓冲区异常:例如monitor、pub/sub使用不当等等
  • 纯缓存使用场景,但是maxmemory-policy配置有误(例如没有设置过期键的业务配置volatile-lru)

紧急处理,可以提交工单临时调整maxmeory,后续咨询客户是否升配或者调整配置。

4.处理途径

  • 客户:找到内存增大的原因。
  • 工单:协助临时调整maxmeomry,如果客户需要调整配置,可以协助解决。

十一、Redis正在加载持久化文件

1.异常堆栈

  1. 1
    <span class="pln">redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisDataException</span></span><span class="pun">:</span><span class="pln"> LOADING </span><span class="typ">Redis</span> <span class="kwd">is</span><span class="pln"> loading the dataset </span><span class="kwd"><span class="hljs-keyword">in</span></span><span class="pln"> memory</span>

2.异常描述

Jedis调用Redis时,如果Redis正在加载持久化文件,无法进行正常的读写。

3.解决方法

正常情况下,阿里云Redis不会出现这种情况,如果出现,则提交工单处理。

4.处理途径

工单。

十二、Lua脚本超时

1.异常堆栈

  1. 1
    <span class="pln">redis</span><span class="pun">.</span><span class="pln">clients</span><span class="pun">.</span><span class="pln">jedis</span><span class="pun">.</span><span class="pln">exceptions</span><span class="pun">.</span><span class="typ">JedisDataException</span><span class="pun">:</span><span class="pln"> BUSY </span><span class="typ">Redis</span> <span class="kwd">is</span><span class="pln"> busy running a script</span><span class="pun">.</span> <span class="typ">You</span><span class="pln"> can only call<span class="hljs-built_in"> SCRIPT </span>KILL </span><span class="kwd"><span class="hljs-keyword">or</span></span><span class="pln"> SHUTDOWN NOSAVE</span><span class="pun">.</span>

2.异常描述

如果Redis当前正在执行Lua脚本,并且超过了lua-time-limit,此时Jedis调用Redis时,会收到上述异常。

3.解决方法

按照异常提示:

1
You can only call SCRIPT KILL or SHUTDOWN NOSAVE.

,使用

1
script kill

终止Lua脚本。

4.处理途径

客户最好自行处理,如果解决不了,值班人员可以协助操作。

十三 连接超时

1.异常堆栈

  1. 1
    <span class="pln">redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisConnectionException</span></span><span class="pun">:</span><span class="pln"> java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">net</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">SocketTimeoutException</span></span><span class="pun">:</span><span class="pln"> connect timed </span><span class="kwd">out</span>

2.异常描述

可能原因有如下几种:

  • 连接超时设置的过短。
  • tcp-backlog满,造成新的连接失败。
  • 客户端与服务端网络故障。

3.解决方法

客户提供连接超时时间,提交工单定位相关原因。

4.处理途径

工单。

十四 Lua脚本写超时

1.异常堆栈

  1. 1
    <span class="pun">(</span><span class="pln">error</span><span class="pun">)</span><span class="pln"> UNKILLABLE </span><span class="typ">Sorry</span><span class="pln"> the<span class="hljs-built_in"> script </span>already executed write commands against the dataset</span><span class="pun">.</span> <span class="typ">You</span><span class="pln"> can either wait the<span class="hljs-built_in"> script </span>termination </span><span class="kwd"><span class="hljs-keyword">or</span></span><span class="pln"> kill the<span class="hljs-built_in"> server </span></span><span class="kwd"><span class="hljs-keyword">in</span></span><span class="pln"> a hard way </span><span class="kwd">using</span><span class="pln"> the SHUTDOWN NOSAVE command</span><span class="pun">.</span>

2.异常描述

如果Redis当前正在执行Lua脚本,并且超过了lua-time-limit,并且已经执行过写命令,此时Jedis调用Redis时,会收到上述异常

3.解决方法

提交工单做紧急处理,管理员需要重启或者切换Redis节点。

4.处理途径

工单。

十五、类加载错误

1.异常堆栈

例如找不到类和方法:

  1. 1
    <span class="typ">Exception</span> <span class="kwd"><span class="hljs-keyword">in</span></span><span class="pln"> thread </span><span class="str"><span class="hljs-string">"commons-pool-EvictionTimer"</span></span><span class="pln"> java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">lang</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">NoClassDefFoundError</span></span><span class="pun">:</span><span class="pln"> redis</span><span class="pun">/</span><span class="pln">clients</span><span class="pun">/</span><span class="pln">util</span><span class="pun">/</span><span class="typ">IOUtils</span>
  2. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">Connection</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">disconnect</span></span><span class="pun">(</span><span class="typ">Connection</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">226</span></span><span class="pun">)</span>
  3. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">BinaryClient</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">disconnect</span></span><span class="pun">(</span><span class="typ">BinaryClient</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">941</span></span><span class="pun">)</span>
  4. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">BinaryJedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">disconnect</span></span><span class="pun">(</span><span class="typ">BinaryJedis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">1771</span></span><span class="pun">)</span>
  5. 1
    <span class="pln">    at redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisFactory</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">destroyObject</span></span><span class="pun">(</span><span class="typ">JedisFactory</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">91</span></span><span class="pun">)</span>
  6. 1
    <span class="pln">    at         org</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">apache</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">commons</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">pool2</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">impl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">GenericObjectPool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">destroy</span></span><span class="pun">(</span><span class="typ">GenericObjectPool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">897</span></span><span class="pun">)</span>
  7. 1
    <span class="pln">    at org</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">apache</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">commons</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">pool2</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">impl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">GenericObjectPool</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">evict</span></span><span class="pun">(</span><span class="typ">GenericObjectPool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">793</span></span><span class="pun">)</span>
  8. 1
    <span class="pln">    at org</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">apache</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">commons</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">pool2</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">impl</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">BaseGenericObjectPool</span><span class="hljs-variable">$Evictor</span></span><span class="pun">.</span><span class="pln">run</span><span class="pun">(</span><span class="typ">BaseGenericObjectPool</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">1036</span></span><span class="pun">)</span>
  9. 1
    <span class="pln">    at java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">util</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">TimerThread</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">mainLoop</span></span><span class="pun">(</span><span class="typ">Timer</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">555</span></span><span class="pun">)</span>
  10. 1
    <span class="pln">    at java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">util</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">TimerThread</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">run</span></span><span class="pun">(</span><span class="typ">Timer</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">java</span></span><span class="pun">:</span><span class="lit"><span class="hljs-number">505</span></span><span class="pun">)</span>
  11. 1
    <span class="typ">Caused</span> <span class="kwd">by</span><span class="pun">:</span><span class="pln"> java</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">lang</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">ClassNotFoundException</span></span><span class="pun">:</span><span class="pln"> redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">util</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">IOUtils</span></span>
  12. 1
    <span class="pun">......</span>

2.异常描述

运行时,Jedis执行命令,抛出异常,提示某个类找不到。此类问题一般都是由于加载多个jedis版本(例如jedis 2.9.0和jedis 2.6),在编译期间代码未出现问题,但类加载器在运行时加载了低版本的Jedis,造成运行时找不到类。

3.解决方法

通常此类问题,可以将重复的Jedis排除掉,例如利用maven的依赖树,把无用的依赖去掉或者exclusion掉。

4.处理途径

客户排查自身代码。

十六、服务端命令不支持

1.异常堆栈

例如客户端执行了geoadd命令,但是服务端返回不支持此命令。

  1. 1
    <span class="pln">redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisDataException</span></span><span class="pun">:</span><span class="pln"> ERR unknown command </span><span class="str"><span class="hljs-string">'GEOADD'</span></span>

2.异常描述

该命令不能被Redis端识别,可能有两个原因:

  • 社区版的一些命令,阿里云Redis的不支持,或者只在某些小版本上支持,例如geoadd是Redis 3.2添加的地理信息API。
  • 命令本身是错误的,不过对于Jedis来说还好,Jedis不支持直接组装命令,每个API都有固定的函数。

3.解决方法

咨询是否有Redis版本支持该命令,如支持可以让客户做小版本升级。

4.处理途径

  • 管理员:确认版本是否支持该命令。
  • 客户:确认后,做小版本升级。

十七、pipeline错误使用

1.异常堆栈

  1. 1
    <span class="pln">redis</span><span class="pun">.</span><span class="pln">clients</span><span class="pun">.</span><span class="pln">jedis</span><span class="pun">.</span><span class="pln">exceptions</span><span class="pun">.</span><span class="typ">JedisDataException</span><span class="pun">:</span> <span class="typ">Please</span><span class="pln"> close pipeline </span><span class="kwd"><span class="hljs-keyword">or</span></span><span class="pln"> multi <span class="hljs-keyword">block</span> before calling </span><span class="kwd">this</span><span class="pln"> <span class="hljs-function"><span class="hljs-keyword">method</span></span></span><span class="pun"><span class="hljs-function">.</span></span>

2.异常描述

在pipeline.sync()执行之前,通过response.get()获取值,在pipeline.sync()执行前,命令没有执行(可以通过monitor做验证),下面代码就会引起上述异常。

  1. 1
    <span class="typ"><span class="hljs-keyword">Jedis</span></span><span class="pln"> <span class="hljs-keyword">jedis </span></span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ"><span class="hljs-keyword">Jedis</span></span><span class="pun"><span class="hljs-keyword">(</span></span><span class="str"><span class="hljs-keyword">"127.0.0.1"</span></span><span class="pun"><span class="hljs-keyword">,</span></span> <span class="lit"><span class="hljs-number">6379</span></span><span class="pun">)<span class="hljs-comment">;</span></span>
  2. 1
    <span class="typ"><span class="hljs-attribute">Pipeline</span></span><span class="pln"><span class="hljs-attribute"> pipeline</span> </span><span class="pun">=</span><span class="pln"> jedis</span><span class="pun">.</span><span class="pln">pipelined</span><span class="pun">();</span>
  3. 1
    <span class="pln">pipeline</span><span class="pun"><span class="hljs-meta">.</span></span><span class="kwd"><span class="hljs-meta">set</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"hello"</span></span><span class="pun">,</span> <span class="str"><span class="hljs-string">"world"</span></span><span class="pun">)<span class="hljs-comment">;</span></span>
  4. 1
    <span class="pln">pipeline</span><span class="pun"><span class="hljs-meta">.</span></span><span class="kwd"><span class="hljs-meta">set</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"java"</span></span><span class="pun">,</span> <span class="str"><span class="hljs-string">"jedis"</span></span><span class="pun">)<span class="hljs-comment">;</span></span>
  5. 1
     
  6. 1
    <span class="typ"><span class="hljs-built_in">Response</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">&gt;</span><span class="pln"> pipeString </span><span class="pun">=</span><span class="pln"> pipeline</span><span class="pun">.</span><span class="kwd"><span class="hljs-keyword">get</span></span><span class="pun">(</span><span class="str"><span class="hljs-string">"java"</span></span><span class="pun">);</span>
  7. 1
    <span class="com"><span class="hljs-comment">//这个get必须在sync之后,如果是批量获取值建议直接用List&lt;Object&gt; objectList = pipeline.syncAndReturnAll();</span></span>
  8. 1
    <span class="typ"><span class="hljs-selector-tag">System</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="kwd"><span class="hljs-selector-class">out</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">println</span></span><span class="pun">(</span><span class="pln"><span class="hljs-selector-tag">pipeString</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="kwd"><span class="hljs-selector-class">get</span></span><span class="pun">());</span>
  9. 1
    <span class="com"><span class="hljs-comment">//命令此时真正执行</span></span>
  10. 1
    <span class="pln">pipeline</span><span class="pun">.</span><span class="pln">sync</span><span class="pun">()<span class="hljs-comment">;</span></span>

Jedis中Reponse的get()方法,有一个判断,如果set=false就会报错,而response中的set初始化为false。

  1. 1
    <span class="kwd"><span class="hljs-keyword">public</span></span><span class="pln"> T </span><span class="kwd"><span class="hljs-keyword">get</span></span><span class="pun">()</span> <span class="pun">{</span>
  2. 1
      <span class="com">// <span class="hljs-keyword">if</span> response has dependency response <span class="hljs-keyword">and</span> dependency <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> built,</span>
  3. 1
      <span class="com">// <span class="hljs-keyword">build </span><span class="hljs-keyword">it </span>first <span class="hljs-keyword">and </span>no more!!</span>
  4. 1
      <span class="kwd"><span class="hljs-keyword">if</span></span> <span class="pun">(</span><span class="pln">dependency </span><span class="pun">!=</span> <span class="kwd"><span class="hljs-literal">null</span></span> <span class="pun">&amp;&amp;</span><span class="pln"> dependency</span><span class="pun">.</span><span class="kwd"><span class="hljs-builtin-name">set</span></span> <span class="pun">&amp;&amp;</span> <span class="pun">!</span><span class="pln">dependency</span><span class="pun">.</span><span class="pln">built</span><span class="pun">)</span> <span class="pun">{</span>
  5. 1
    <span class="pln">    dependency</span><span class="pun">.</span><span class="pln">build</span><span class="pun">()<span class="hljs-comment">;</span></span>
  6. 1
      <span class="pun">}</span>
  7. 1
      <span class="kwd"><span class="hljs-keyword">if</span></span> <span class="pun">(!</span><span class="kwd"><span class="hljs-keyword">set</span></span><span class="pun">)</span> <span class="pun">{</span>
  8. 1
        <span class="kwd"><span class="hljs-keyword">throw</span></span> <span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ"><span class="hljs-type">JedisDataException</span></span><span class="pun">(</span>
  9. 1
            <span class="str"><span class="hljs-string">"Please close pipeline or multi block before calling this method."</span></span><span class="pun">)<span class="hljs-comment">;</span></span>
  10. 1
      <span class="pun">}</span>
  11. 1
      <span class="kwd"><span class="hljs-meta">if</span></span> <span class="pun">(!</span><span class="pln"><span class="hljs-keyword">built</span></span><span class="pun"><span class="hljs-keyword">)</span></span> <span class="pun">{</span>
  12. 1
    <span class="pln">    <span class="hljs-keyword">build</span></span><span class="pun">();</span>
  13. 1
      <span class="pun">}</span>
  14. 1
      <span class="kwd"><span class="hljs-keyword">if</span></span> <span class="pun">(</span><span class="pln"><span class="hljs-keyword">exception</span> </span><span class="pun">!=</span> <span class="kwd"><span class="hljs-keyword">null</span></span><span class="pun">)</span> <span class="pun">{</span>
  15. 1
        <span class="kwd"><span class="hljs-keyword">throw</span></span><span class="pln"> <span class="hljs-keyword">exception</span></span><span class="pun">;</span>
  16. 1
      <span class="pun">}</span>
  17. 1
      <span class="kwd"><span class="hljs-keyword">return</span></span><span class="pln"> response</span><span class="pun">;</span>
  18. 1
    <span class="pun">}</span>

pipeline.sync()会每个结果设置set=true。

  1. 1
    <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span> <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="pln"><span class="hljs-function"> <span class="hljs-title">sync</span></span></span><span class="pun"><span class="hljs-function"><span class="hljs-params">()</span></span></span> <span class="pun">{</span>
  2. 1
      <span class="kwd"><span class="hljs-keyword">if</span></span> <span class="pun"><span class="hljs-comment">(</span></span><span class="pln"><span class="hljs-comment">getPipelinedResponseLength</span></span><span class="pun"><span class="hljs-comment">()</span></span> <span class="pun">&gt;</span> <span class="lit"><span class="hljs-number">0</span></span><span class="pun">)</span> <span class="pun">{</span>
  3. 1
        <span class="typ"><span class="hljs-built_in">List</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-built_in">Object</span></span><span class="pun">&gt;</span><span class="pln"> unformatted </span><span class="pun">=</span><span class="pln"> client</span><span class="pun">.</span><span class="pln">getAll</span><span class="pun">();</span>
  4. 1
        <span class="kwd"><span class="hljs-keyword">for</span></span> <span class="pun">(</span><span class="typ">Object</span><span class="pln"> o </span><span class="pun">:</span><span class="pln"> <span class="hljs-type">unformatted</span></span><span class="pun">)</span> <span class="pun">{</span>
  5. 1
    <span class="pln">      generateResponse</span><span class="pun">(</span><span class="pln"><span class="hljs-name">o</span></span><span class="pun">)<span class="hljs-comment">;</span></span>
  6. 1
        <span class="pun">}</span>
  7. 1
      <span class="pun">}</span>
  8. 1
    <span class="pun">}</span>

其中generateResponse(o):

  1. 1
    <span class="kwd"><span class="hljs-keyword">protected</span></span> <span class="typ">Response</span><span class="pun">&lt;?&gt;</span><span class="pln"> generateResponse</span><span class="pun">(</span><span class="typ"><span class="hljs-keyword">Object</span></span><span class="pln"> data</span><span class="pun">)</span> <span class="pun"><span class="hljs-comment">{</span></span>
  2. 1
      <span class="typ"><span class="hljs-built_in">Response</span></span><span class="pun">&lt;?&gt;</span><span class="pln"> <span class="hljs-built_in">response</span> </span><span class="pun">=</span><span class="pln"> pipelinedResponses</span><span class="pun">.</span><span class="pln">poll</span><span class="pun">();</span>
  3. 1
      <span class="kwd"><span class="hljs-keyword">if</span></span> <span class="pun">(</span><span class="pln"><span class="hljs-built_in">response</span> </span><span class="pun">!=</span> <span class="kwd"><span class="hljs-literal">null</span></span><span class="pun">)</span> <span class="pun">{</span>
  4. 1
    <span class="pln">    response</span><span class="pun"><span class="hljs-meta">.</span></span><span class="kwd"><span class="hljs-meta">set</span></span><span class="pun">(</span><span class="pln"><span class="hljs-meta">data</span></span><span class="pun">)<span class="hljs-comment">;</span></span>
  5. 1
      <span class="pun">}</span>
  6. 1
      <span class="kwd"><span class="hljs-keyword">return</span></span><span class="pln"> response</span><span class="pun">;</span>
  7. 1
    <span class="pun">}</span>

其中response.set(data);

  1. 1
    <span class="kwd"><span class="hljs-keyword">public</span></span> <span class="kwd"><span class="hljs-literal">void</span></span> <span class="kwd"><span class="hljs-built_in">set</span></span><span class="pun">(</span><span class="typ">Object</span><span class="pln"> <span class="hljs-built_in">data</span></span><span class="pun">)</span> <span class="pun">{</span>
  2. 1
        <span class="kwd"><span class="hljs-keyword">this</span></span><span class="pun">.</span><span class="pln"><span class="hljs-keyword">data</span> </span><span class="pun">=</span><span class="pln"> <span class="hljs-keyword">data</span></span><span class="pun">;</span>
  3. 1
        <span class="kwd"><span class="hljs-builtin-name">set</span></span> <span class="pun">=</span> <span class="kwd"><span class="hljs-literal">true</span></span><span class="pun">;</span>
  4. 1
    <span class="pun">}</span>

3.解决方法

对于批量结果的解析,建议使用pipeline.syncAndReturnAll()来实现,下面操作模拟了批量hgetAll

  1. 1
     
  2. 1
    <span class="com">/**</span>
  3. 1
    <span class="com"><span class="hljs-bullet">* </span>pipeline模拟批量hgetAll</span>
  4. 1
    <span class="com"><span class="hljs-bullet">* </span>@param keyList</span>
  5. 1
    <span class="com">* @<span class="hljs-keyword">return</span></span>
  6. 1
    <span class="com"><span class="hljs-comment">*/</span></span>
  7. 1
    <span class="kwd"><span class="hljs-keyword">public</span></span> <span class="typ"><span class="hljs-built_in">Map</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">,</span> <span class="typ"><span class="hljs-built_in">Map</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">,</span> <span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">&gt;&gt;</span><span class="pln"> mHgetAll</span><span class="pun">(</span><span class="typ"><span class="hljs-built_in">List</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">&gt;</span><span class="pln"> keyList</span><span class="pun">)</span> <span class="pun">{</span>
  8. 1
    <span class="com"><span class="hljs-comment">// 1.生成pipeline对象</span></span>
  9. 1
    <span class="typ"><span class="hljs-attribute">Pipeline</span></span><span class="pln"><span class="hljs-attribute"> pipeline</span> </span><span class="pun">=</span><span class="pln"> jedis</span><span class="pun">.</span><span class="pln">pipelined</span><span class="pun">();</span>
  10. 1
    <span class="com"><span class="hljs-comment">// 2.pipeline执行命令,注意此时命令并未真正执行</span></span>
  11. 1
    <span class="kwd"><span class="hljs-keyword">for</span></span> <span class="pun">(</span><span class="typ"><span class="hljs-keyword">String</span></span><span class="pln"> <span class="hljs-built_in">key</span> </span><span class="pun">:</span><span class="pln"> keyList</span><span class="pun">)</span> <span class="pun">{</span>
  12. 1
    <span class="pln">  pipeline</span><span class="pun">.</span><span class="pln">hgetAll</span><span class="pun">(</span><span class="pln">key</span><span class="pun">)<span class="hljs-comment">;</span></span>
  13. 1
    <span class="pun">}</span>
  14. 1
    <span class="com"><span class="hljs-comment">// 3.执行命令 syncAndReturnAll()返回结果</span></span>
  15. 1
    <span class="typ"><span class="hljs-built_in">List</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-built_in">Object</span></span><span class="pun">&gt;</span><span class="pln"> objectList </span><span class="pun">=</span><span class="pln"> pipeline</span><span class="pun">.</span><span class="pln">syncAndReturnAll</span><span class="pun">();</span>
  16. 1
    <span class="kwd"><span class="hljs-keyword">if</span></span> <span class="pun">(</span><span class="pln"><span class="hljs-attr">objectList</span> </span><span class="pun">==</span> <span class="kwd"><span class="hljs-literal">null</span></span> <span class="pun">||</span><span class="pln"> objectList</span><span class="pun">.</span><span class="pln">isEmpty</span><span class="pun">())</span> <span class="pun">{</span>
  17. 1
      <span class="kwd"><span class="hljs-function"><span class="hljs-keyword">return</span></span></span> <span class="typ"><span class="hljs-function">Collections</span></span><span class="pun"><span class="hljs-function">.</span></span><span class="pln"><span class="hljs-function"><span class="hljs-title">emptyMap</span></span></span><span class="pun"><span class="hljs-function"><span class="hljs-params">()</span></span>;</span>
  18. 1
    <span class="pun">}</span>
  19. 1
     
  20. 1
    <span class="com"><span class="hljs-comment">// 4.解析结果</span></span>
  21. 1
    <span class="typ"><span class="hljs-built_in">Map</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">,</span><span class="typ"><span class="hljs-built_in">Map</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">,</span> <span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">&gt;&gt;</span><span class="pln"> resultMap </span><span class="pun">=</span> <span class="kwd"><span class="hljs-keyword">new</span></span> <span class="typ">HashMap</span><span class="pun">&lt;</span><span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">,</span> <span class="typ"><span class="hljs-built_in">Map</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">,</span><span class="typ"><span class="hljs-built_in">String</span></span><span class="pun">&gt;&gt;();</span>
  22. 1
    <span class="kwd"><span class="hljs-keyword">for</span></span> <span class="pun">(</span><span class="kwd">int</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">=</span> <span class="lit"><span class="hljs-number">0</span></span><span class="pun">;</span><span class="pln"> <span class="hljs-built_in">i</span> </span><span class="pun">&lt;</span><span class="pln"> objectList</span><span class="pun">.</span><span class="pln"><span class="hljs-built_in">size</span></span><span class="pun">();</span><span class="pln"> <span class="hljs-built_in">i</span></span><span class="pun">++)</span> <span class="pun">{</span>
  23. 1
      <span class="typ"><span class="hljs-built_in">Object</span></span> <span class="kwd"><span class="hljs-built_in">object</span></span> <span class="pun">=</span><span class="pln"> objectList</span><span class="pun">.</span><span class="kwd"><span class="hljs-keyword">get</span></span><span class="pun">(</span><span class="pln">i</span><span class="pun">);</span>
  24. 1
      <span class="typ"><span class="hljs-meta">Map</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-keyword">String</span></span><span class="pun"><span class="hljs-keyword">,</span></span> <span class="typ"><span class="hljs-keyword">String</span></span><span class="pun"><span class="hljs-keyword">&gt;</span></span><span class="pln"> <span class="hljs-meta">map</span> </span><span class="pun">=</span> <span class="pun">(</span><span class="typ"><span class="hljs-meta">Map</span></span><span class="pun">&lt;</span><span class="typ"><span class="hljs-keyword">String</span></span><span class="pun"><span class="hljs-keyword">,</span></span> <span class="typ"><span class="hljs-keyword">String</span></span><span class="pun"><span class="hljs-keyword">&gt;)</span></span> <span class="kwd">object</span><span class="pun"><span class="hljs-comment">;</span></span>
  25. 1
      <span class="typ"><span class="hljs-keyword">String</span></span><span class="pln"> <span class="hljs-built_in">key</span> </span><span class="pun">=</span><span class="pln"> keyList</span><span class="pun">.</span><span class="kwd"><span class="hljs-built_in">get</span></span><span class="pun">(</span><span class="pln">i</span><span class="pun">);</span>
  26. 1
    <span class="pln">  resultMap</span><span class="pun">.</span><span class="pln"><span class="hljs-built_in">put</span></span><span class="pun">(</span><span class="pln"><span class="hljs-built_in">key</span></span><span class="pun">,</span><span class="pln"> <span class="hljs-built_in">map</span></span><span class="pun">);</span>
  27. 1
    <span class="pun">}</span>
  28. 1
    <span class="kwd"><span class="hljs-keyword">return</span></span><span class="pln"> resultMap</span><span class="pun">;</span>
  29. 1
    <span class="pun">}</span>

4.处理途径

修改业务代码。

十八、管理员命令,普通用户不能执行

1.异常堆栈

命令role不能被普通用户执行,详情可参考暂未开放的Redis命令

  1. 1
    <span class="pln">redis</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">clients</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">jedis</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">exceptions</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="typ"><span class="hljs-selector-class">JedisDataException</span></span><span class="pun">:</span><span class="pln"> ERR command role </span><span class="kwd">not</span><span class="pln"> support </span><span class="kwd"><span class="hljs-keyword">for</span></span><span class="pln"> <span class="hljs-attribute">normal</span> user</span>

2.异常描述

该命令尚未开放。

3.解决方法:

不能使用该命令,如果有需求或者疑问可以联系值班人员。

4.处理途径

从文档中确认该命令是否开放。

其他问题:

1.Jedis版本如何选择?

原则上选择最新的release版本,但最好选择发行一段时间后的版本,因为jedis历史上出现过一次问题较大的release版本,目前来说2.9.0比较稳定。

  1. 1
    <span class="tag"><span class="hljs-section">&lt;dependency&gt;</span></span>
  2. 1
        <span class="tag"><span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span></span><span class="pln">redis.clients</span><span class="tag"><span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span></span>
  3. 1
        <span class="tag"><span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span></span><span class="pln">jedis</span><span class="tag"><span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span></span>
  4. 1
        <span class="tag">&lt;<span class="hljs-built_in">version</span>&gt;</span><span class="pln"><span class="hljs-number">2.9</span><span class="hljs-number">.0</span></span><span class="tag">&lt;/<span class="hljs-built_in">version</span>&gt;</span>
  5. 1
        <span class="tag">&lt;<span class="hljs-keyword">type</span>&gt;</span><span class="pln">jar</span><span class="tag">&lt;/<span class="hljs-keyword">type</span>&gt;</span>
  6. 1
        <span class="tag"><span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span></span><span class="pln">compile</span><span class="tag"><span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span></span>
  7. 1
    <span class="tag"><span class="hljs-section">&lt;/dependency&gt;</span></span>

2.Jedis中的JedisCluster是阿里云Redis集群版的客户端吗?

不是,请使用阿里云集群版的客户端,直接使用Jedis和JedisPool即可。因为官方集群和阿里云Redis集群是不同的架构,具体参考:redis4.0、codis、阿里云redis 3种redis集群对比分析

连接池参数

1. 资源设置和使用

序号 参数名 含义 默认值 使用建议
1 maxTotal 资源池中最大连接数 8 -
2 maxIdle 资源池允许最大空闲的连接数 8 -
3 minIdle 资源池确保最少空闲的连接数 0 -
4 blockWhenExhausted 当资源池用尽后,调用者是否要等待。只有当为true时,下面的maxWaitMillis才会生效 true 建议使用默认值
5 maxWaitMillis 当资源池连接用尽后,调用者的最大等待时间(单位为毫秒) -1:表示永不超时 不建议使用默认值
6 testOnBorrow 向资源池借用连接时是否做连接有效性检测(ping),无效连接会被移除 false 业务量很大时候建议设置为false(设置为true会多一次ping的开销)。
7 testOnReturn 向资源池归还连接时是否做连接有效性检测(ping),无效连接会被移除 false 业务量很大时候建议设置为false(设置为true会多一次ping的开销)。
8 jmxEnabled 是否开启jmx监控,可用于监控 true 建议开启,但应用本身也要开启

2.空闲资源监测

空闲Jedis对象检测,下面四个参数组合来完成,testWhileIdle是该功能的开关。

序号 参数名 含义 默认值 使用建议
1 testWhileIdle 是否开启空闲资源监测 false 设置为true
2 timeBetweenEvictionRunsMillis 空闲资源的检测周期(单位为毫秒) -1:不检测 建议设置,周期自行选择,也可以默认也可以使用下面JedisPoolConfig中的配置
3 minEvictableIdleTimeMillis 资源池中资源最小空闲时间(单位为毫秒),达到此值后空闲资源将被移除 1000 * 60 * 30 = 30分钟 可根据自身业务决定,大部分默认值即可,也可以考虑使用下面JeidsPoolConfig中的配置
4 numTestsPerEvictionRun 做空闲资源检测时,每次的采样数 3 可根据自身应用连接数进行微调,如果设置为-1,就是对所有连接做空闲监测

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: