{"id":1916,"date":"2023-04-01T09:28:18","date_gmt":"2023-04-01T01:28:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1916"},"modified":"2023-04-22T08:53:04","modified_gmt":"2023-04-22T00:53:04","slug":"synchronization-scheme-for-mysql-and-redis-caching","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/synchronization-scheme-for-mysql-and-redis-caching\/","title":{"rendered":"MySQL \u4e0e Redis \u7f13\u5b58\u7684\u540c\u6b65\u65b9\u6848"},"content":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecdMySQL\u4e0eRedis\u7f13\u5b58\u7684\u540c\u6b65\u7684\u4e24\u79cd\u65b9\u6848<\/p>\n<ul>\n<li>\u65b9\u68481\uff1a\u901a\u8fc7MySQL\u81ea\u52a8\u540c\u6b65\u5237\u65b0Redis\uff0cMySQL\u89e6\u53d1\u5668+UDF\u51fd\u6570\u5b9e\u73b0<\/li>\n<li>\u65b9\u68482\uff1a\u89e3\u6790MySQL\u7684binlog\u5b9e\u73b0\uff0c\u5c06\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u540c\u6b65\u5230Redis<\/li>\n<\/ul>\n<h2>\u65b9\u68481\uff08UDF\uff09<\/h2>\n<p><!-- more --><\/p>\n<p>\u573a\u666f\u5206\u6790\uff1a\u5f53\u6211\u4eec\u5bf9MySQL\u6570\u636e\u5e93\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\u65f6\uff0c\u540c\u65f6\u5c06\u76f8\u5e94\u7684\u6570\u636e\u540c\u6b65\u5230Redis\u4e2d\uff0c\u540c\u6b65\u5230Redis\u4e4b\u540e\uff0c\u67e5\u8be2\u7684\u64cd\u4f5c\u5c31\u4eceRedis\u4e2d\u67e5\u627e<\/p>\n<p>\u8fc7\u7a0b\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5728MySQL\u4e2d\u5bf9\u8981\u64cd\u4f5c\u7684\u6570\u636e\u8bbe\u7f6e\u89e6\u53d1\u5668Trigger\uff0c\u76d1\u542c\u64cd\u4f5c<\/li>\n<li>\u5ba2\u6237\u7aef\uff08NodeServer\uff09\u5411MySQL\u4e2d\u5199\u5165\u6570\u636e\u65f6\uff0c\u89e6\u53d1\u5668\u4f1a\u88ab\u89e6\u53d1\uff0c\u89e6\u53d1\u4e4b\u540e\u8c03\u7528MySQL\u7684UDF\u51fd\u6570<\/li>\n<li>UDF\u51fd\u6570\u53ef\u4ee5\u628a\u6570\u636e\u5199\u5165\u5230Redis\u4e2d\uff0c\u4ece\u800c\u8fbe\u5230\u540c\u6b65\u7684\u6548\u679c<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/MySQL\u89e6\u53d1\u5668+UDF\u51fd\u6570\u5b9e\u73b0MySQL\u4e0eRedis\u7f13\u5b58\u7684\u540c\u6b65.png\" alt=\"MySQL\u89e6\u53d1\u5668+UDF\u51fd\u6570\u5b9e\u73b0MySQL\u4e0eRedis\u7f13\u5b58\u7684\u540c\u6b65\" \/><\/p>\n<p>\u65b9\u6848\u5206\u6790\uff1a<\/p>\n<ul>\n<li>\u8fd9\u79cd\u65b9\u6848\u9002\u5408\u4e8e\u8bfb\u591a\u5199\u5c11\uff0c\u5e76\u4e14\u4e0d\u5b58\u5e76\u53d1\u5199\u7684\u573a\u666f<\/li>\n<li>\u56e0\u4e3aMySQL\u89e6\u53d1\u5668\u672c\u8eab\u5c31\u4f1a\u9020\u6210\u6548\u7387\u7684\u964d\u4f4e\uff0c\u5982\u679c\u4e00\u4e2a\u8868\u7ecf\u5e38\u88ab\u64cd\u4f5c\uff0c\u8fd9\u79cd\u65b9\u6848\u663e\u793a\u662f\u4e0d\u5408\u9002\u7684<\/li>\n<\/ul>\n<h2>\u65b9\u68482\uff08\u89e3\u6790binlog\uff09<\/h2>\n<h3>\u57fa\u672c\u539f\u7406<\/h3>\n<p>\u5728\u4ecb\u7ecd\u65b9\u68482\u4e4b\u524d\u6211\u4eec\u5148\u6765\u4ecb\u7ecd\u4e00\u4e0bMySQL\u590d\u5236\u7684\u539f\u7406\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<ul>\n<li>\u4e3b\u670d\u52a1\u5668\u64cd\u4f5c\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u5199\u5165Bin log<\/li>\n<li>\u4ece\u670d\u52a1\u5668\u8c03\u7528I\/O\u7ebf\u7a0b\u8bfb\u53d6\u4e3b\u670d\u52a1\u5668\u7684Bin log\uff0c\u5e76\u4e14\u5199\u5165\u5230\u81ea\u5df1\u7684Relay log\u4e2d\uff0c\u518d\u8c03\u7528SQL\u7ebf\u7a0b\u4eceRelay log\u4e2d\u89e3\u6790\u6570\u636e\uff0c\u4ece\u800c\u540c\u6b65\u5230\u81ea\u5df1\u7684\u6570\u636e\u5e93\u4e2d<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/MySQL\u4e3b\u4ece\u590d\u5236\u7684\u57fa\u672c\u539f\u7406.png\" alt=\"MySQL\u4e3b\u4ece\u590d\u5236\u7684\u57fa\u672c\u539f\u7406\" \/><\/p>\n<p>\u65b9\u68482\u5c31\u662f\uff1a<\/p>\n<ul>\n<li>\u4e0a\u9762MySQL\u7684\u6574\u4e2a\u590d\u5236\u6d41\u7a0b\u53ef\u4ee5\u603b\u7ed3\u4e3a\u4e00\u53e5\u8bdd\uff0c\u90a3\u5c31\u662f\uff1a\u4ece\u670d\u52a1\u5668\u8bfb\u53d6\u4e3b\u670d\u52a1\u5668Bin log\u4e2d\u7684\u6570\u636e\uff0c\u4ece\u800c\u540c\u6b65\u5230\u81ea\u5df1\u7684\u6570\u636e\u5e93\u4e2d<\/li>\n<li>\u6211\u4eec\u65b9\u68482\u4e5f\u662f\u5982\u6b64\uff0c\u5c31\u662f\u5728\u6982\u5ff5\u4e0a\u628a\u4e3b\u670d\u52a1\u5668\u6539\u4e3aMySQL\uff0c\u628a\u4ece\u670d\u52a1\u5668\u6539\u4e3aRedis\u800c\u5df2\uff08\u5982\u4e0b\u56fe\u6240\u793a\uff09\uff0c\u5f53MySQL\u4e2d\u6709\u6570\u636e\u5199\u5165\u65f6\uff0c\u6211\u4eec\u5c31\u89e3\u6790MySQL\u7684Bin log\uff0c\u7136\u540e\u5c06\u89e3\u6790\u51fa\u6765\u7684\u6570\u636e\u5199\u5165\u5230Redis\u4e2d\uff0c\u4ece\u800c\u8fbe\u5230\u540c\u6b65\u7684\u6548\u679c<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/\u89e3\u6790MySQL\u7684binlog\u5b9e\u73b0MySQL\u4e0eRedis\u7f13\u5b58\u7684\u540c\u6b65.png\" alt=\"\u89e3\u6790MySQL\u7684binlog\u5b9e\u73b0MySQL\u4e0eRedis\u7f13\u5b58\u7684\u540c\u6b65\" \/><\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u662f\u4e00\u4e2a\u4e91\u6570\u636e\u5e93\u5b9e\u4f8b\u5206\u6790\uff1a<\/p>\n<ul>\n<li>\u4e91\u6570\u636e\u5e93\u4e0e\u672c\u5730\u6570\u636e\u5e93\u662f\u4e3b\u4ece\u5173\u7cfb\u3002\u4e91\u6570\u636e\u5e93\u4f5c\u4e3a\u4e3b\u6570\u636e\u5e93\u4e3b\u8981\u63d0\u4f9b\u5199\uff0c\u672c\u5730\u6570\u636e\u5e93\u4f5c\u4e3a\u4ece\u6570\u636e\u5e93\u4ece\u4e3b\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e<\/li>\n<li>\u672c\u5730\u6570\u636e\u5e93\u8bfb\u53d6\u5230\u6570\u636e\u4e4b\u540e\uff0c\u89e3\u6790Bin log\uff0c\u7136\u540e\u5c06\u6570\u636e\u5199\u5165\u5199\u5165\u540c\u6b65\u5230Redis\u4e2d\uff0c\u7136\u540e\u5ba2\u6237\u7aef\u4eceRedis\u8bfb\u6570\u636e<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/\u89e3\u6790\u4e91\u6570\u636e\u5e93\u7684binlog\u5b9e\u73b0MySQL\u4e0eRedis\u7f13\u5b58\u7684\u540c\u6b65\u65b9\u6848.png\" alt=\"\u89e3\u6790\u4e91\u6570\u636e\u5e93\u7684binlog\u5b9e\u73b0MySQL\u4e0eRedis\u7f13\u5b58\u7684\u540c\u6b65\u65b9\u6848\" \/><\/p>\n<ul>\n<li>\u8fd9\u4e2a\u6280\u672f\u65b9\u6848\u7684\u96be\u70b9\u5c31\u5728\u4e8e\uff1a \u5982\u4f55\u89e3\u6790MySQL\u7684Bin Log\u3002\u4f46\u662f\u8fd9\u9700\u8981\u5bf9binlog\u6587\u4ef6\u4ee5\u53caMySQL\u6709\u975e\u5e38\u6df1\u5165\u7684\u7406\u89e3\uff0c\u540c\u65f6\u7531\u4e8ebinlog\u5b58\u5728<code>Statement\/Row\/Mixedlevel<\/code>\u591a\u79cd\u5f62\u5f0f\uff0c\u5206\u6790binlog\u5b9e\u73b0\u540c\u6b65\u7684\u5de5\u4f5c\u91cf\u662f\u975e\u5e38\u5927\u7684<\/li>\n<\/ul>\n<h3>Canal\u5f00\u6e90\u6280\u672f<\/h3>\n<p>Canal\u662f\u963f\u91cc\u5df4\u5df4\u65d7\u4e0b\u7684\u4e00\u6b3e\u5f00\u6e90\u9879\u76ee\uff0c\u7eafJava\u5f00\u53d1\u3002\u57fa\u4e8e\u6570\u636e\u5e93\u589e\u91cf\u65e5\u5fd7\u89e3\u6790\uff0c\u63d0\u4f9b\u589e\u91cf\u6570\u636e\u8ba2\u9605&amp;\u6d88\u8d39\uff0c\u76ee\u524d\u4e3b\u8981\u652f\u6301\u4e86MySQL\uff08\u4e5f\u652f\u6301MariaDB\uff09<\/p>\n<p>\u5f00\u6e90\u53c2\u8003\u5730\u5740\u6709\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/liukelin\/canal_mysql_nosql_sync\">https:\/\/github.com\/liukelin\/canal_mysql_nosql_sync<\/a><\/p>\n<p>\u5de5\u4f5c\u539f\u7406\uff08\u6a21\u4effMySQL\u590d\u5236\uff09\uff1a<\/p>\n<ul>\n<li>Canal\u6a21\u62dfmysql slave\u7684\u4ea4\u4e92\u534f\u8bae\uff0c\u4f2a\u88c5\u81ea\u5df1\u4e3amysql slave\uff0c\u5411mysql master\u53d1\u9001dump\u534f\u8bae<\/li>\n<li>mysql master\u6536\u5230dump\u8bf7\u6c42\uff0c\u5f00\u59cb\u63a8\u9001binary log\u7ed9slave\uff08\u4e5f\u5c31\u662fCanal\uff09<\/li>\n<li>Canal\u89e3\u6790binary log\u5bf9\u8c61\uff08\u539f\u59cb\u4e3abyte\u6d41\uff09<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/Canal\u6a21\u4effMySQL\u590d\u5236\u5de5\u4f5c\u539f\u7406.png\" alt=\"Canal\u6a21\u4effMySQL\u590d\u5236\u5de5\u4f5c\u539f\u7406\" \/><\/p>\n<p>\u67b6\u6784\uff1a<\/p>\n<ul>\n<li>eventParser (\u6570\u636e\u6e90\u63a5\u5165\uff0c\u6a21\u62dfslave\u534f\u8bae\u548cmaster\u8fdb\u884c\u4ea4\u4e92\uff0c\u534f\u8bae\u89e3\u6790)<\/li>\n<li>eventSink (Parser\u548cStore\u94fe\u63a5\u5668\uff0c\u8fdb\u884c\u6570\u636e\u8fc7\u6ee4\uff0c\u52a0\u5de5\uff0c\u5206\u53d1\u7684\u5de5\u4f5c)<\/li>\n<li>eventStore (\u6570\u636e\u5b58\u50a8)<\/li>\n<li>metaManager (\u589e\u91cf\u8ba2\u9605&amp;\u6d88\u8d39\u4fe1\u606f\u7ba1\u7406\u5668)<\/li>\n<li>server\u4ee3\u8868\u4e00\u4e2acanal\u8fd0\u884c\u5b9e\u4f8b\uff0c\u5bf9\u5e94\u4e8e\u4e00\u4e2ajvm<\/li>\n<li>instance\u5bf9\u5e94\u4e8e\u4e00\u4e2a\u6570\u636e\u961f\u5217 (1\u4e2aserver\u5bf9\u5e941..n\u4e2ainstance)<\/li>\n<li>instance\u6a21\u5757\uff1a<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/Canal\u4e4bServer\u53cainstance\u6a21\u5757\u67b6\u6784.png\" alt=\"Canal\u4e4bServer\u53cainstance\u6a21\u5757\u67b6\u6784\" \/><\/p>\n<p>\u5927\u81f4\u7684\u89e3\u6790\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>parse\u89e3\u6790MySQL\u7684Binlog\uff0c\u7136\u540e\u5c06\u6570\u636e\u653e\u5165\u5230sink\u4e2d<\/li>\n<li>sink\u5bf9\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\uff0c\u52a0\u5de5\uff0c\u5206\u53d1<\/li>\n<li>store\u4ecesink\u4e2d\u8bfb\u53d6\u89e3\u6790\u597d\u7684\u6570\u636e\u5b58\u50a8\u8d77\u6765<\/li>\n<li>\u7136\u540e\u81ea\u5df1\u7528\u8bbe\u8ba1\u4ee3\u7801\u5c06store\u4e2d\u7684\u6570\u636e\u540c\u6b65\u5199\u5165Redis\u4e2d\u5c31\u53ef\u4ee5\u4e86<\/li>\n<li>\u5176\u4e2dparse\/sink\u662f\u6846\u67b6\u5c01\u88c5\u597d\u7684\uff0c\u6211\u4eec\u505a\u7684\u662fstore\u7684\u6570\u636e\u8bfb\u53d6\u90a3\u4e00\u6b65<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/Canal\u89e3\u6790MySQL\u7684Binlog\u6d41\u7a0b.png\" alt=\"Canal\u89e3\u6790MySQL\u7684Binlog\u6d41\u7a0b\" \/><\/p>\n<p>\u4e0b\u9762\u662f\u8fd0\u884c\u62d3\u6251\u56fe<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/Canal\u8fd0\u884c\u62d3\u6251\u56fe.png\" alt=\"Canal\u8fd0\u884c\u62d3\u6251\u56fe\" \/><\/p>\n<p>MySQL\u8868\u7684\u540c\u6b65\uff0c\u91c7\u7528\u8d23\u4efb\u94fe\u6a21\u5f0f\uff0c\u6bcf\u5f20\u8868\u5bf9\u5e94\u4e00\u4e2aFilter\u3002\u4f8b\u5982zvsync\u4e2d\u8981\u7528\u5230\u7684\u7c7b\u8bbe\u8ba1\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/Canal\u8868\u540c\u6b65\u8bbe\u8ba1\u7c7b.png\" alt=\"Canal\u8868\u540c\u6b65\u8bbe\u8ba1\u7c7b\" \/><\/p>\n<p>\u4e0b\u9762\u662f\u5177\u4f53\u5316\u7684zvsync\u4e2d\u8981\u7528\u5230\u7684\u7c7b\uff0c\u6bcf\u5f53\u65b0\u589e\u6216\u8005\u5220\u9664\u8868\u65f6\uff0c\u76f4\u63a5\u8fdb\u884c\u589e\u5220\u5c31\u53ef\u4ee5\u4e86<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/Canal\u8868\u540c\u6b65\u5b9e\u73b0\u7c7b.png\" alt=\"Canal\u8868\u540c\u6b65\u5b9e\u73b0\u7c7b\" \/><\/p>\n<h2>\u9644\u52a0<\/h2>\n<p>\u672c\u6587\u4e0a\u9762\u6240\u4ecb\u7ecd\u7684\u90fd\u662f\u4eceMySQL\u4e2d\u540c\u6b65\u5230\u7f13\u5b58\u4e2d\u3002\u4f46\u662f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u53ef\u80fd\u6709\u4eba\u4f1a\u7528\u4e0b\u9762\u7684\u65b9\u6848\uff1a\u5ba2\u6237\u7aef\u6709\u6570\u636e\u6765\u4e86\u4e4b\u540e\uff0c\u5148\u5c06\u5176\u4fdd\u5b58\u5230Redis\u4e2d\uff0c\u7136\u540e\u518d\u540c\u6b65\u5230MySQL\u4e2d<\/p>\n<p>\u8fd9\u79cd\u65b9\u6848\u672c\u8eab\u4e5f\u662f\u4e0d\u5b89\u5168\/\u4e0d\u53ef\u9760\u7684\uff0c\u56e0\u6b64\u5982\u679cRedis\u5b58\u5728\u77ed\u6682\u7684\u5b95\u673a\u6216\u5931\u6548\uff0c\u90a3\u4e48\u4f1a\u4e22\u5931\u6570\u636e<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/\u5148\u5b58\u5165Redis\u518d\u540c\u6b65MySQL.png\" alt=\"\u5148\u5b58\u5165Redis\u518d\u540c\u6b65MySQL\" \/><\/p>\n<blockquote>\n<p>\u8f6c\u8f7d\u81f3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/mp.weixin.qq.com\/s\/GHl0zzHvx9-F_BkEo9gMfw\">https:\/\/mp.weixin.qq.com\/s\/GHl0zzHvx9-F_BkEo9gMfw<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecdMySQL\u4e0eRedis\u7f13\u5b58\u7684\u540c\u6b65\u7684\u4e24\u79cd\u65b9\u6848 \u65b9\u68481\uff1a\u901a\u8fc7MySQL\u81ea\u52a8\u540c\u6b65\u5237\u65b0Redis\uff0cMySQL\u89e6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,14],"tags":[],"class_list":["post-1916","post","type-post","status-publish","format-standard","hentry","category-mysql","category-redis"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1916"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1916\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}