{"id":201353,"date":"2025-05-10T09:21:12","date_gmt":"2025-05-10T01:21:12","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201353\/"},"modified":"2025-05-10T09:21:12","modified_gmt":"2025-05-10T01:21:12","slug":"redis%e8%bf%9e%e6%8e%a5%e8%b6%85%e6%97%b6%e5%bc%82%e5%b8%b8%e7%9a%84%e5%a4%84%e7%90%86%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201353\/","title":{"rendered":"Redis\u8fde\u63a5\u8d85\u65f6\u5f02\u5e38\u7684\u5904\u7406\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u8fde\u63a5\u8d85\u65f6\u5f02\u5e38\u7684\u5904\u7406\u65b9\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aRedis\u8fde\u63a5\u8d85\u65f6\u5f02\u5e38\u7684\u5904\u7406\u65b9\u6cd5\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><span style=\"color: #ff0000\"><strong>0\u3001\u95ee\u9898\u63cf\u8ff0<\/strong><\/span><\/p>\n<p>\u4f7f\u7528Jedis\u8fde\u63a5redis\u8fdb\u884c\u6570\u636e\u67e5\u8be2\u64cd\u4f5c\uff0c\u6b63\u5e38\u7684\u4ee3\u7801\u8fd0\u884c\u6ca1\u6709\u95ee\u9898\uff0c\u4f46\u662f\u65f6\u4e0d\u65f6\u4f1a\u62a5\u51fa\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n<blockquote>\n<p>Exception in thread &#8220;main&#8221; redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out<br \/> &nbsp;at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:202)<br \/> &nbsp;at redis.clients.util.RedisInputStream.read(RedisInputStream.java:181)<br \/> &nbsp;at redis.clients.jedis.Protocol.processBulkReply(Protocol.java:181)<br \/> &nbsp;at redis.clients.jedis.Protocol.process(Protocol.java:155)<br \/> &nbsp;at redis.clients.jedis.Protocol.processMultiBulkReply(Protocol.java:206)<br \/> &nbsp;at redis.clients.jedis.Protocol.process(Protocol.java:157)<br \/> &nbsp;at redis.clients.jedis.Protocol.processMultiBulkReply(Protocol.java:206)<br \/> &nbsp;at redis.clients.jedis.Protocol.process(Protocol.java:157)<br \/> &nbsp;at redis.clients.jedis.Protocol.read(Protocol.java:215)<br \/> &nbsp;at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)<br \/> &nbsp;at redis.clients.jedis.Connection.getRawObjectMultiBulkReply(Connection.java:285)<br \/> &nbsp;at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:291)<br \/> &nbsp;at redis.clients.jedis.BinaryJedis.hscan(BinaryJedis.java:3390)<br \/> &nbsp;at com.ict.mcg.filter.DuplicateClueFilterV2.hscan(DuplicateClueFilterV2.java:867)<br \/> &nbsp;at com.ict.mcg.filter.DuplicateClueFilterV2.collectRecentCluekeywords(DuplicateClueFilterV2.java:487)<br \/> &nbsp;at com.ict.mcg.main.GetCluesMain.run_online(GetCluesMain.java:208)<br \/> &nbsp;at com.ict.mcg.main.GetCluesMain.main(GetCluesMain.java:1685)<br \/> Caused by: java.net.SocketTimeoutException: Read timed out<br \/> &nbsp;at java.net.SocketInputStream.socketRead0(Native Method)<br \/> &nbsp;at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)<br \/> &nbsp;at java.net.SocketInputStream.read(SocketInputStream.java:171)<br \/> &nbsp;at java.net.SocketInputStream.read(SocketInputStream.java:141)<br \/> &nbsp;at java.net.SocketInputStream.read(SocketInputStream.java:127)<br \/> &nbsp;at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:196)<br \/> &nbsp;&#8230; 16 more<\/p>\n<\/blockquote>\n<p>\u7a76\u5176\u539f\u56e0\uff0c\u53ef\u4ee5\u5b9a\u4f4d\u4e3ajava.net.SocketTimeoutException: Read timed out\uff0c\u5373\u7f51\u7edc\u8fde\u63a5\u5f02\u5e38\uff1b<\/p>\n<p><span style=\"color: #ff0000\"><strong>1\u3001 \u53ef\u80fd\u7684\u539f\u56e0<\/strong><\/span><\/p>\n<p><strong>1.1 \u670d\u52a1\u5668\u8d44\u6e90\u5305\u62ec\u5185\u5b58\u3001\u78c1\u76d8\u3001cpu\u7b49\u5229\u7528\u7387\u9ad8<br \/><\/strong><\/p>\n<p>\u7ecf\u8fc7\u67e5\u770bredis\u90e8\u7f72\u673a\u5668\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u53d1\u73b0\u6574\u4f53\u673a\u5668\u8fd0\u884c\u72b6\u6001\u826f\u597d<\/p>\n<p><strong>1.2 \u670d\u52a1\u5668\u8bbe\u7f6e\u9632\u706b\u5899\uff0c\u5bfc\u81f4\u8fde\u63a5\u5931\u8d25<\/strong><\/p>\n<p>\u56e0\u4e3a\u6b63\u5e38\u7684\u4ee3\u7801\u6d41\u7a0b\u90fd\u53ef\u4ee5\u8dd1\u901a\uff0c\u6240\u4ee5\u9632\u706b\u5899\u8bbe\u7f6e\u6ca1\u6709\u95ee\u9898\uff1b<\/p>\n<p><strong>1.3 redis\u914d\u7f6e\u6587\u4ef6bind\u76d1\u542chost\u914d\u7f6e\u4e0d\u5f53<\/strong><\/p>\n<p>redis\u7684\u914d\u7f6e\u6587\u4ef6\u4e2dbind\u5bf9\u5e94host\u7684\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre>\n# By default Redis listens for connections from all the network interfaces\n# available on the server. It is possible to listen to just one or multiple\n# interfaces using the \"bind\" configuration directive, followed by one or\n# more IP addresses.\n#\n# Examples:\n#\n# bind 192.168.1.100 10.0.0.1\n# bind 127.0.0.1<\/pre>\n<p>\u9ed8\u8ba4\u7684bind\u7ed1\u5b9a\u7684host\u4e3a0.0.0.0\uff0c\u5373\u53ef\u4ee5\u76d1\u542c\u6bcf\u4e00\u4e2a\u53ef\u7528\u7684\u7f51\u7edc\u63a5\u53e3\uff1b\u76f8\u5f53\u4e8e\u914d\u7f6e\u4e3a\uff1a<\/p>\n<pre>\nbind 0.0.0.0<\/pre>\n<p>\u6211\u4eec\u7684\u914d\u7f6e\u6587\u4ef6\u4e5f\u914d\u7f6e\u6b63\u5e38\uff0c\u800c\u4e14\u6b63\u5e38\u7684\u4ee3\u7801\u6d41\u7a0b\u8fd0\u884c\u6b63\u5e38\uff0c\u4e5f\u53ef\u4ee5\u4f50\u8bc1\u8fd9\u4e00\u70b9\uff1b<\/p>\n<p><strong>1.4 Jedis\u4f7f\u7528\u914d\u7f6e\u95ee\u9898<\/strong><\/p>\n<p>\u76ee\u524dJedis\u7684\u8fde\u63a5\u6c60\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre>\nprivate static JedisPool getPool() {\n    if (pool == null) {\n      JedisPoolConfig config = new JedisPoolConfig();\n      \/\/\u63a7\u5236\u4e00\u4e2apool\u53ef\u5206\u914d\u591a\u5c11\u4e2ajedis\u5b9e\u4f8b\uff0c\u901a\u8fc7pool.getResource()\u6765\u83b7\u53d6\uff1b\n      \/\/\u5982\u679c\u8d4b\u503c\u4e3a-1\uff0c\u5219\u8868\u793a\u4e0d\u9650\u5236\uff1b\u5982\u679cpool\u5df2\u7ecf\u5206\u914d\u4e86maxActive\u4e2ajedis\u5b9e\u4f8b\uff0c\u5219\u6b64\u65f6pool\u7684\u72b6\u6001\u4e3aexhausted(\u8017\u5c3d)\u3002\n      config.setMaxActive(10);\n      \/\/\u63a7\u5236\u4e00\u4e2apool\u6700\u591a\u6709\u591a\u5c11\u4e2a\u72b6\u6001\u4e3aidle(\u7a7a\u95f2\u7684)\u7684jedis\u5b9e\u4f8b\u3002\n      config.setMaxIdle(2);\n      \/\/\u8868\u793a\u5f53borrow(\u5f15\u5165)\u4e00\u4e2ajedis\u5b9e\u4f8b\u65f6\uff0c\u6700\u5927\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u5982\u679c\u8d85\u8fc7\u7b49\u5f85\u65f6\u95f4\uff0c\u5219\u76f4\u63a5\u629b\u51faJedisConnectionException\uff1b\n      config.setMaxWait(1000 * 200000);\n      \/\/\u5728borrow\u4e00\u4e2ajedis\u5b9e\u4f8b\u65f6\uff0c\u662f\u5426\u63d0\u524d\u8fdb\u884cvalidate\u64cd\u4f5c\uff1b\u5982\u679c\u4e3atrue\uff0c\u5219\u5f97\u5230\u7684jedis\u5b9e\u4f8b\u5747\u662f\u53ef\u7528\u7684\uff1b\n      config.setTestOnBorrow(true);\n      config.setTestOnReturn(true);\n\n      \/\/\u76ee\u524dredis\u53ea\u6709\u4e00\u4e2a\u670d\u52a1\u5668\n      pool = new JedisPool(config, \"localhost\", 6379);\n    }\n    return pool;\n  }\n\n  private static Jedis getJedis() {\n    Jedis jedis = null;\n    int count = 0;\n    do {\n      try {\n        pool = getPool();\n        jedis = pool.getResource();\n      } catch(Exception e) {\n\/\/    System.out.println(e.getMessage());\n        e.printStackTrace();\n        pool.returnBrokenResource(jedis);\n      }\n\n      count++;\n    } while (jedis==null &amp;&amp; count \n\n<p>\u6784\u5efaJedisPool\u7684\u903b\u8f91\u4e2d\uff0c\u53ea\u662f\u8bbe\u7f6e\u4e86config.setMaxWait(1000 * 200000);\uff0c\u8fd9\u4e2a\u662f\u5f15\u5165\u65b0\u7684jedis\u5b9e\u4f8b\u7684\u6700\u5927\u7b49\u5f85\u65f6\u95f4\uff0c\u5e76\u6ca1\u6709\u8fdb\u884c\u5176\u4ed6\u76f8\u5173\u7684\u8fde\u63a5\u8d85\u65f6\u7684\u914d\u7f6e\uff1b\u4e8e\u662f\u67e5\u770bJedisPool\u7684\u6e90\u4ee3\u7801\uff0c\u53d1\u73b0\u5982\u4e0b\uff1a<\/p>\n\n<pre>\npublic JedisPool(final Config poolConfig, final String host) {\n    this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);\n  }\n\n  public JedisPool(String host, int port) {\n    this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);\n  }\n\n  public JedisPool(final String host) {\n    this(host, Protocol.DEFAULT_PORT);\n  }\n\n  public JedisPool(final Config poolConfig, final String host, int port,\n      int timeout, final String password) {\n    this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);\n  }\n\n  public JedisPool(final Config poolConfig, final String host, final int port) {\n    this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);\n  }\n\n  public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) {\n    this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE);\n  }\n\n  public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password,\n          final int database) {\n    super(poolConfig, new JedisFactory(host, port, timeout, password, database));\n  }<\/pre>\n<p>\u7531\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u770b\u5230\uff0cJedisPool\u6709\u591a\u4e2a\u91cd\u8f7d\u7684\u6784\u9020\u51fd\u6570\uff0c\u5e76\u4e14\u6784\u9020\u51fd\u6570\u4e2d\u9700\u8981\u4f20\u5165\u4e00\u4e2atimeout\u53c2\u6570\u4f5c\u4e3a\u8fde\u63a5\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u5982\u679c\u6ca1\u6709\u4f20\uff0c\u5219\u91c7\u7528Protocol.DEFAULT_TIMEOUT\u4f5c\u4e3a\u9ed8\u8ba4\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u7ee7\u7eed\u8ddf\u8e2a\u6e90\u7801\uff1a<\/p>\n<pre>\npublic final class Protocol {\n\n  public static final int DEFAULT_PORT = 6379;\n  public static final int DEFAULT_TIMEOUT = 2000;\n  public static final int DEFAULT_DATABASE = 0;\n\n  public static final String CHARSET = \"UTF-8\";\n\n  public static final byte DOLLAR_BYTE = '$';\n  public static final byte ASTERISK_BYTE = '*';\n  public static final byte PLUS_BYTE = '+';\n  public static final byte MINUS_BYTE = '-';\n  public static final byte COLON_BYTE = ':';\n\n  private Protocol() {\n \/\/ this prevent the class from instantiation\n  }<\/pre>\n<p>\u53ef\u4ee5\u5f97\u51fa\u7ed3\u8bba\uff0c\u9ed8\u8ba4JedisPool\u4e2d\u8fde\u63a5\u7684\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4\u4e3a2\u79d2\uff0c\u800c\u6211\u4eec\u8c03\u7528\u7684JedisPool\u6784\u9020\u51fd\u6570\uff0c\u6070\u6070\u91c7\u7528\u7684\u662f\u8fd9\u4e2a\u914d\u7f6e\uff0c\u53ea\u8981\u4e24\u79d2\u949f\u6ca1\u6709\u8fde\u63a5\u6210\u529f\uff0credis\u7684\u8fde\u63a5\u5c31\u65ad\u5f00\uff0c\u4ece\u800c\u62a5\u9519\uff0c\u8fd9\u5728\u6570\u636e\u5e93\u8bf7\u6c42\u5e76\u53d1\u91cf\u6bd4\u8f83\u5927\u7684\u65f6\u5019\u662f\u6709\u53ef\u80fd\u53d1\u751f\u7684\uff0c\u9042\u505a\u5982\u4e0b\u66f4\u6539\uff0c\u5728\u521b\u5efaJedisPool\u7684\u65f6\u5019\uff0c\u4f20\u5165\u4e00\u4e2a\u8f83\u5927\u7684\u8d85\u65f6\u65f6\u95f4\uff1a<\/p>\n<pre>\npool = new JedisPool(config, ParamUtil.REDIS_ADDRESS[0], ParamUtil.REDIS_PORT, 1000 * 10);<\/pre>\n<p><span style=\"color: #ff0000\"><strong>2\u3001\u603b\u7ed3<\/strong><\/span><\/p>\n<p>\u9047\u5230\u95ee\u9898\u8fd8\u662f\u591a\u67e5\uff0c\u591a\u770b\u6e90\u7801\uff0c\u591a\u770b\u6e90\u7801\u4e2d\u7684\u914d\u7f6e\uff0c\u4ed4\u7ec6\u4e00\u9879\u4e00\u9879\u5730\u6392\u67e5\u95ee\u9898\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u8fde\u63a5\u8d85\u65f6\u5f02\u5e38\u7684\u5904\u7406\u65b9\u6cd5&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201353","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201353"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201353\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}