{"id":976,"date":"2021-03-30T09:58:14","date_gmt":"2021-03-30T01:58:14","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=976"},"modified":"2021-03-30T09:58:36","modified_gmt":"2021-03-30T01:58:36","slug":"zookeeper-distributed-coordination-service-for-distributed","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2021\/03\/zookeeper-distributed-coordination-service-for-distributed\/","title":{"rendered":"ZooKeeper\uff1a\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u7684\u5206\u5e03\u5f0f\u534f\u8c03\u670d\u52a1"},"content":{"rendered":"

1\u3001\u6982\u8ff0<\/h2>\n

\u5b98\u65b9\u6587\u6863<\/a><\/p>\n

ZooKeeper\u662f\u7528\u4e8e\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u7684\u5206\u5e03\u5f0f\u534f\u8c03\u670d\u52a1\u3002\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u57fa\u4e8ezookeeper\u6765\u5b9e\u73b0\u7528\u4e8e\u540c\u6b65\uff0c\u914d\u7f6e\u7ef4\u62a4\u4ee5\u53ca\u7ec4\u548c\u547d\u540d\u7684\u66f4\u9ad8\u7ea7\u522b\u7684\u670d\u52a1\u3002\u5b83\u7684\u8bbe\u8ba1\u6613\u4e8e\u7f16\u7a0b\uff0c\u5e76\u4f7f\u7528\u4e86\u6309\u7167\u6587\u4ef6\u7cfb\u7edf\u719f\u6089\u7684\u76ee\u5f55\u6811\u7ed3\u6784\u6837\u5f0f\u8bbe\u7f6e\u7684\u6570\u636e\u6a21\u578b\u3002<\/p>\n

\u4f17\u6240\u5468\u77e5\uff0c\u534f\u8c03\u670d\u52a1\u5f88\u96be\u505a\u5230\u3002\u5b83\u4eec\u7279\u522b\u5bb9\u6613\u51fa\u73b0\u8bf8\u5982\u6bd4\u8d5b\u6761\u4ef6\u548c\u6b7b\u9501\u4e4b\u7c7b\u7684\u9519\u8bef\u3002ZooKeeper\u80cc\u540e\u7684\u52a8\u673a\u662f\u51cf\u8f7b\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u4ece\u5934\u5f00\u59cb\u5b9e\u65bd\u534f\u8c03\u670d\u52a1\u7684\u8d23\u4efb\u3002<\/p>\n

ZooKeeper\u5141\u8bb8\u5206\u5e03\u5f0f\u8fdb\u7a0b\u901a\u8fc7\u5171\u4eab\u7684\u5206\u5c42\u540d\u79f0\u7a7a\u95f4\u76f8\u4e92\u534f\u8c03\uff0c\u8be5\u547d\u540d\u7a7a\u95f4\u7684\u7ec4\u7ec7\u65b9\u5f0f\u7c7b\u4f3c\u4e8e\u6807\u51c6\u6587\u4ef6\u7cfb\u7edf\u3002\u540d\u79f0\u7a7a\u95f4\u7531\u6570\u636e\u5bc4\u5b58\u5668\uff08\u5728ZooKeeper\u770b\u6765\uff0c\u79f0\u4e3aznode\uff09\u7ec4\u6210\uff0c\u5b83\u4eec\u7c7b\u4f3c\u4e8e\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u4e0e\u8bbe\u8ba1\u7528\u4e8e\u5b58\u50a8\u7684\u5178\u578b\u6587\u4ef6\u7cfb\u7edf\u4e0d\u540c\uff0cZooKeeper\u6570\u636e\u4fdd\u7559\u5728\u5185\u5b58\u4e2d\uff0c\u8fd9\u610f\u5473\u7740ZooKeeper\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u541e\u5410\u91cf\u548c\u4f4e\u5ef6\u8fdf\u6570\u3002<\/p>\n

\"\"<\/p>\n

Zookeeper \u7279\u70b9<\/h3>\n
    \n
  1. Zookeeper : \u4e00\u4e2a\u9886\u5bfc\u8005\uff08Leader\uff09 ,\u591a\u4e2a\u8ddf\u968f\u8005\uff08Follower\uff09 \u7ec4\u6210\u7684\u96c6\u7fa4\u3002<\/li>\n
  2. \u53ef\u9760\u6027\uff1a \u7fa4\u96c6\u4e2d\u53ea\u8981\u6709\u534a\u6570\u4ee5\u4e0a\u7684\u8282\u70b9\u5b58\u6d3b\uff0cZookeeper\u96c6\u7fa4\u5c31\u80fd\u6b63\u5e38\u670d\u52a1\u3002\u6240\u4ee5Zookeeper\u9002\u5408\u5b89\u88c5\u5947\u6570\u53f0\u670d\u52a1\u5668<\/strong><\/li>\n
  3. \u5168\u5c40\u6570\u636e\u4e00\u81f4\u6027\uff1a \u6bcf\u4e2aserver\u4fdd\u5b58\u4e00\u4efd\u76f8\u540c\u7684\u6570\u636e\u526f\u672c\uff0cClient \u65e0\u8bba\u8fde\u63a5\u5230\u54ea\u4e2a\u8282\u70b9\u6570\u636e\u90fd\u662f\u4e00\u81f4\u7684\u3002<\/li>\n
  4. \u987a\u5e8f\u4e00\u81f4\u6027\uff1a \u6765\u81ea\u5ba2\u6237\u7aef\u7684\u66f4\u65b0\u5c06\u6309\u7167\u5176\u53d1\u9001\u987a\u5e8f\u4f9d\u6b21\u6267\u884c\u3002<\/li>\n
  5. \u539f\u5b50\u6027\uff1a \u6570\u636e\u66f4\u65b0\u8981\u4e48\u6210\u529f\u8981\u4e48\u5931\u8d25\u3002<\/li>\n
  6. \u5b9e\u65f6\u6027\uff1a\u5728\u4e00\u5b9a\u65f6\u95f4\u8303\u56f4\u5185\uff0c\u5ba2\u6237\u7aef\u80fd\u8bfb\u5230\u6700\u65b0\u6570\u636e\u3002 <\/li>\n<\/ol>\n

    \u6570\u636e\u6a21\u578b\u548c\u5206\u5c42\u540d\u79f0\u7a7a\u95f4<\/h3>\n

    ZooKeeper\u63d0\u4f9b\u7684\u540d\u79f0\u7a7a\u95f4\u4e0e\u6807\u51c6\u6587\u4ef6\u7cfb\u7edf\u7684\u540d\u79f0\u7a7a\u95f4\u975e\u5e38\u76f8\u4f3c\u3002\u540d\u79f0\u662f\u7531\u659c\u6760\uff08\/\uff09\u5206\u9694\u7684\u4e00\u7cfb\u5217\u8def\u5f84\u5143\u7d20\u3002ZooKeeper\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u7531\u8def\u5f84\u6807\u8bc6\u3002<\/p>\n

    \"ZooKeeper\u7684\u5c42\u6b21\u547d\u540d\u7a7a\u95f4\"<\/p>\n

    \u6301\u4e45\u8282\u70b9\u548c\u77ed\u6682\u8282\u70b9<\/h3>\n

    ZooKeeper\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u5177\u6709\u4e0e\u5176\u5173\u8054\u7684\u6570\u636e<\/strong>\u4ee5\u53ca\u5b50\u8282\u70b9<\/strong>\u3002\u5c31\u50cf\u62e5\u6709\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf\u4e00\u6837\uff0c\u8be5\u6587\u4ef6\u7cfb\u7edf\u4e5f\u5141\u8bb8\u6587\u4ef6\u6210\u4e3a\u76ee\u5f55\u3002\uff08ZooKeeper\u65e8\u5728\u5b58\u50a8\u534f\u8c03\u6570\u636e\uff1a\u72b6\u6001\u4fe1\u606f\uff0c\u914d\u7f6e\uff0c\u4f4d\u7f6e\u4fe1\u606f\u7b49\uff0c\u56e0\u6b64\u5b58\u50a8\u5728\u6bcf\u4e2a\u8282\u70b9\u4e0a\u7684\u6570\u636e\u901a\u5e38\u5f88\u5c0f\uff0c\u5728\u5b57\u8282\u5230\u5343\u5b57\u8282\u8303\u56f4\u5185\uff081MB\uff09\uff09\u6bcf\u4e2a\u8282\u70b9\u79f0\u4e4b\u4e3aZnode\u3002<\/p>\n

    Znodes\u7ef4\u62a4\u4e00\u4e2a\u7edf\u8ba1\u4fe1\u606f\u7ed3\u6784\uff0c\u5176\u4e2d\u5305\u62ec\u7528\u4e8e\u6570\u636e\u66f4\u6539\uff0cACL\u66f4\u6539\u548c\u65f6\u95f4\u6233\u7684\u7248\u672c\u53f7\uff0c\u4ee5\u5141\u8bb8\u8fdb\u884c\u7f13\u5b58\u9a8c\u8bc1\u548c\u534f\u8c03\u7684\u66f4\u65b0\u3002znode\u7684\u6570\u636e\u6bcf\u6b21\u66f4\u6539\u65f6\uff0c\u7248\u672c\u53f7\u90fd\u4f1a\u589e\u52a0\u3002\u4f8b\u5982\uff0c\u6bcf\u5f53\u5ba2\u6237\u7aef\u68c0\u7d22\u6570\u636e\u65f6\uff0c\u5b83\u4e5f\u63a5\u6536\u6570\u636e\u7684\u7248\u672c\u3002<\/p>\n

    \u539f\u5b50\u5730\u8bfb\u53d6\u548c\u5199\u5165\u5b58\u50a8\u5728\u540d\u79f0\u7a7a\u95f4\u4e2d\u6bcf\u4e2aznode\u4e0a\u7684\u6570\u636e\u3002\u8bfb\u53d6\u5c06\u83b7\u53d6\u4e0eznode\u5173\u8054\u7684\u6240\u6709\u6570\u636e\u5b57\u8282\uff0c\u800c\u5199\u5165\u5c06\u66ff\u6362\u6240\u6709\u6570\u636e\u3002\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e00\u4e2a\u8bbf\u95ee\u63a7\u5236\u5217\u8868\uff08ACL\uff09\uff0c\u7528\u4e8e\u9650\u5236\u8c01\u53ef\u4ee5\u6267\u884c\u64cd\u4f5c\u3002<\/p>\n

    ZooKeeper\u8fd8\u5177\u6709\u77ed\u6682\u8282\u70b9\u7684\u6982\u5ff5\u3002\u53ea\u8981\u521b\u5efaznode\u7684\u4f1a\u8bdd\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\uff0c\u8fd9\u4e9bznode\u5c31\u5b58\u5728\u3002\u4f1a\u8bdd\u7ed3\u675f\u65f6\uff0c\u5c06\u5220\u9664znode\u3002<\/p>\n

    \"\"<\/p>\n

    \n

    1\uff09czxid-\u521b\u5efa\u8282\u70b9\u7684\u4e8b\u52a1zxid<\/p>\n

    \u6bcf\u6b21\u4fee\u6539ZooKeeper\u72b6\u6001\u90fd\u4f1a\u6536\u5230\u4e00\u4e2azxid\u5f62\u5f0f\u7684\u65f6\u95f4\u6233\uff0c\u4e5f\u5c31\u662fZooKeeper\u4e8b\u52a1ID\u3002<\/p>\n

    \u4e8b\u52a1ID\u662fZooKeeper\u4e2d\u6240\u6709\u4fee\u6539\u603b\u7684\u6b21\u5e8f\u3002\u6bcf\u4e2a\u4fee\u6539\u90fd\u6709\u552f\u4e00\u7684zxid\uff0c\u5982\u679czxid1\u5c0f\u4e8ezxid2\uff0c\u90a3\u4e48zxid1\u5728zxid2\u4e4b\u524d\u53d1\u751f\u3002<\/p>\n

    2\uff09ctime - znode\u88ab\u521b\u5efa\u7684\u6beb\u79d2\u6570(\u4ece1970\u5e74\u5f00\u59cb)<\/p>\n

    3\uff09mzxid - znode\u6700\u540e\u66f4\u65b0\u7684\u4e8b\u52a1zxid<\/p>\n

    4\uff09mtime - znode\u6700\u540e\u4fee\u6539\u7684\u6beb\u79d2\u6570(\u4ece1970\u5e74\u5f00\u59cb)<\/p>\n

    5\uff09pZxid-znode\u6700\u540e\u66f4\u65b0\u7684\u5b50\u8282\u70b9zxid<\/p>\n

    6\uff09cversion - znode\u5b50\u8282\u70b9\u53d8\u5316\u53f7\uff0cznode\u5b50\u8282\u70b9\u4fee\u6539\u6b21\u6570<\/p>\n

    7\uff09dataversion - znode\u6570\u636e\u53d8\u5316\u53f7<\/p>\n

    8\uff09aclVersion - znode\u8bbf\u95ee\u63a7\u5236\u5217\u8868\u7684\u53d8\u5316\u53f7<\/p>\n

    9\uff09ephemeralOwner- \u5982\u679c\u662f\u4e34\u65f6\u8282\u70b9\uff0c\u8fd9\u4e2a\u662fznode\u62e5\u6709\u8005\u7684session id\u3002\u5982\u679c\u4e0d\u662f\u4e34\u65f6\u8282\u70b9\u5219\u662f0\u3002<\/p>\n

    10\uff09dataLength- znode\u7684\u6570\u636e\u957f\u5ea6<\/p>\n

    11\uff09numChildren - znode\u5b50\u8282\u70b9\u6570\u91cf<\/p>\n<\/blockquote>\n

    \u6709\u6761\u4ef6\u7684\u66f4\u65b0\u548c\u76d1\u89c6<\/h3>\n

    ZooKeeper\u652f\u6301watches<\/strong>\u7684\u6982\u5ff5\u3002\u5ba2\u6237\u7aef\u53ef\u4ee5\u5728znode\u4e0a\u8bbe\u7f6ewatches<\/em>\u3002znode\u66f4\u6539\u65f6\uff0c\u5c06\u89e6\u53d1\u5e76\u5220\u9664\u76d1\u89c6\u3002\u89e6\u53d1\u76d1\u89c6\u540e\uff0c\u5ba2\u6237\u7aef\u4f1a\u6536\u5230\u4e00\u4e2a\u6570\u636e\u5305\uff0c\u8bf4\u660eznode\u5df2\u66f4\u6539\u3002\u5982\u679c\u5ba2\u6237\u7aef\u548c\u5176\u4e2d\u4e00\u4e2aZooKeeper\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u8fde\u63a5\u65ad\u5f00\uff0c\u5219\u5ba2\u6237\u7aef\u5c06\u6536\u5230\u672c\u5730\u901a\u77e5\u3002<\/p>\n

    3.6.0\u4e2d\u7684\u65b0\u589e\u529f\u80fd\uff1a<\/strong>\u5ba2\u6237\u7aef\u8fd8\u53ef\u4ee5\u5728znode\u4e0a\u8bbe\u7f6e\u6c38\u4e45\u7684\uff0c\u9012\u5f52\u7684watches<\/em>\uff0c\u8fd9\u4e9bwatches<\/em>\u5728\u89e6\u53d1\u65f6\u4e0d\u4f1a\u88ab\u5220\u9664\uff0c\u5e76\u4e14\u4f1a\u4ee5\u9012\u5f52\u65b9\u5f0f\u89e6\u53d1\u5df2\u6ce8\u518cznode\u4ee5\u53ca\u6240\u6709\u5b50znode\u7684\u66f4\u6539\u3002<\/p>\n

    \u7b80\u5355\u7684API<\/h3>\n

    ZooKeeper\u7684\u8bbe\u8ba1\u76ee\u6807\u4e4b\u4e00\u662f\u63d0\u4f9b\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u7f16\u7a0b\u754c\u9762\u3002\u56e0\u6b64\uff0c\u5b83\u4ec5\u652f\u6301\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n