{"id":1242,"date":"2022-02-23T23:28:59","date_gmt":"2022-02-23T15:28:59","guid":{"rendered":"http:\/\/www.eait.co\/?p=1242"},"modified":"2022-02-23T23:28:59","modified_gmt":"2022-02-23T15:28:59","slug":"zookeeper%e5%88%86%e5%b8%83%e5%bc%8f%e7%a7%92%e6%9d%80%e9%94%81","status":"publish","type":"post","link":"https:\/\/notes.coremix.net\/?p=1242","title":{"rendered":"Zookeeper\u5206\u5e03\u5f0f\u79d2\u6740\u9501"},"content":{"rendered":"<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\npackage MiaoSha;\r\n\r\nimport org.apache.curator.CuratorConnectionLossException;\r\nimport org.apache.curator.framework.CuratorFramework;\r\nimport org.apache.curator.framework.CuratorFrameworkFactory;\r\nimport org.apache.curator.framework.recipes.locks.InterProcessLock;\r\nimport org.apache.curator.framework.recipes.locks.InterProcessMutex;\r\nimport org.apache.curator.retry.ExponentialBackoffRetry;\r\n\r\n\/**\r\n * \u7f3a\u70b9\uff1a\u901f\u5ea6\u6709\u70b9\u6162\r\n * \u4e0d\u4f1a\u51fa\u73b0\u5199\u4e09\u53f0\u8ba1\u53ea\u6709\u4e24\u53f0\u673a\u5b58\u6d3b\u4e8c\u62a5\u9519\u7684\u95ee\u9898\r\n *\/\r\npublic class TestDistributedLock {\r\n    \/\/10\u4e2a\u5171\u4eab\u8d44\u6e90\r\n    private static int count = 100;\r\n    public static void main(String&#x5B;] args) {\r\n        \/\/\u7b49\u5f85\u65f6\u957f\uff0c\u91cd\u8bd5\u6b21\u6570\r\n        ExponentialBackoffRetry retry = new ExponentialBackoffRetry(1000, 10);\r\n\r\n        CuratorFramework client = CuratorFrameworkFactory.builder()\r\n                .connectString(&quot;bigdata166:2181,bigdata167:2181,bigdata168:2181&quot;)\r\n                .retryPolicy(retry)\r\n                .build();\r\n        \/\/\u5ba2\u6237\u7aef\u542f\u52a8\r\n        client.start();\r\n        \/\/\u521b\u5efa\u9501 \u8282\u70b9\u81ea\u52a8\u521b\u5efa\r\n        InterProcessMutex lock = new InterProcessMutex(client, &quot;\/mylock&quot;);\r\n\r\n        \/\/\u6d4b\u8bd5\u7ebf\u7a0b\r\n        for (int i = 0; i &lt; 100; i++) {\r\n            new Thread(new Runnable() {\r\n                @Override\r\n                public void run() {\r\n                    try {\r\n                        lock.acquire();\r\n                        printCountNumber();\r\n                    } catch (Exception e) {\r\n                        e.printStackTrace();\r\n                    }finally {\r\n                        try {\r\n                            lock.release();\r\n                        } catch (Exception e) {\r\n                            e.printStackTrace();\r\n                        }\r\n                    }\r\n                }\r\n            }).start();\r\n        }\r\n\r\n    }\r\n\r\n    private static void printCountNumber() throws InterruptedException {\r\n        System.out.println(&quot;===========&quot;+Thread.currentThread().getName()+&quot;==============&quot;);\r\n        System.out.println(&quot;\u5f53\u524d\u5171\u4eab\u8d44\u6e90\u7684\u6570\u91cf\u4e3a\uff1a&quot;+count);\r\n        count--;\r\n\/\/        Thread.sleep(12);\r\n        System.out.println(&quot;===========&quot;+Thread.currentThread().getName()+&quot;==============&quot;);\r\n    }\r\n}\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>package MiaoSha; import org.apache.curator.CuratorConne [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,22,30],"tags":[],"class_list":["post-1242","post","type-post","status-publish","format-standard","hentry","category-other","category-java","category-bigdata"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1242"}],"version-history":[{"count":1,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1242\/revisions"}],"predecessor-version":[{"id":1248,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1242\/revisions\/1248"}],"wp:attachment":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}