{"id":11310,"date":"2013-06-23T21:20:00","date_gmt":"2013-06-23T20:20:00","guid":{"rendered":"http:\/\/zygm0nt.github.com\/blog\/2013\/06\/23\/zookeeper-curator"},"modified":"2023-03-20T15:19:01","modified_gmt":"2023-03-20T14:19:01","slug":"zookeeper-curator-distributed-sync","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2013\/06\/23\/zookeeper-curator-distributed-sync\/","title":{"rendered":"Zookeeper + Curator = Distributed sync"},"content":{"rendered":"<p class=\"c8\">An application developed for one of my recent projects at TouK involved multiple servers. There was a requirement to ensure failover for the system\u2019s components. Since I had already a few separate components I didn\u2019t want to add more of that, and since there already was a Zookeeper ensemble running &#8211; required by one of the services, I\u2019ve decided to go that way with my solution.<\/p>\n<p><!--more--><\/p>\n<h2 id=\"what-is-zookeeper\" class=\"c8\">What is Zookeeper?<\/h2>\n<p class=\"c8\">Just a crude distributed synchronization framework. However, it implements Paxos-style algorithms (<span class=\"c5\"><a class=\"c0\" href=\"http:\/\/en.wikipedia.org\/wiki\/Paxos_(computer_science)\">http:\/\/en.wikipedia.org\/wiki\/Paxos_(computer_science)<\/a><\/span>) to ensure no split-brain scenarios would occur. This is quite an important feature, since I don\u2019t have to care about that kind of problems while using this app. You just need to create an ensemble of a couple of its instances &#8211; to ensure high availability. It is basically a virtual filesystem, with files, directories and stuff. One could ask why another filesystem? Well this one is a rather special one, especially for distributed systems. The reason why creating all the locking algorithms on top of Zookeeper is easy is its Ephemeral Nodes &#8211; which are just files that exist as long as connection for them exists. After it disconnects &#8211; such file disappears.<\/p>\n<p class=\"c8\">With such paradigms in place it\u2019s fairly easy to create some high level algorithms for synchronization.<\/p>\n<p class=\"c8\">Having that in place, it\u00a0can\u00a0safely integrate multiple services ensuring loose coupling in a distributed way.<\/p>\n<h2 id=\"zookeeper-from-developers-pov\" class=\"c8\">Zookeeper from developer\u2019s POV<\/h2>\n<p class=\"c8\">With all the base services for Zookeeper started, it seems there is nothing else, than just connect to it and start implementing necessary algorithms. Unfortunately, the API is quite basic and offers files and directories abstractions with the addition of different node type (file types) &#8211; ephemeral and sequence. It is also possible to watch a node for changes.<\/p>\n<h2 id=\"using-bare-zookeeper-is-hard\" class=\"c8\">Using bare Zookeeper is hard!<\/h2>\n<p class=\"c8\">Creating connections is tedious &#8211; and there is lots of things to take care of. Handling an established connection is hard &#8211; when establishing connection to ensemble, it\u2019s necessary to negotiate a session also. During the whole process a number of exceptions can occur &#8211; these are \u201crecoverable\u201d<br \/>\nexceptions, that can be gracefully handled and not break the connection.<\/p>\n<p>So, Zookeeper API is hard. Even if one is proficient with that API, then there come recipes. The reason for using Zookeeper is to be able to implement some more sophisticated algorithms on top of it. Unfortunately those aren&#8217;t trivial and it is again quite hard to implement them without bugs.<\/p>\n<p class=\"c8\">And since distributed systems are hard, why would anyone want another difficult to handle tool?<\/p>\n<h2 id=\"enter-curator\" class=\"c8\">Enter Curator<\/h2>\n<p>Happily, guys from Netflix implemented a nice abstraction for dealing with Zookeeper internals. They called it Curator and use it extensively in the company&#8217;s environment. Curator offers consistent API for Zookeeper&#8217;s functionality. It even implements a couple of recipes for distributed systems.<\/p>\n<h2 id=\"file-read-write\" class=\"c8\">File read\/write<\/h2>\n<p>The basic use of Zookeeper is as a distributed configuration repository. For this scenario I only need read\/write capabilities, to be able to write and read files from the Zookeeper filesystem. This code snippet writes a sample json to a file on ZK filesystem.<a href=\"http:\/\/zygm0nt.github.com\/blog\/2013\/06\/23\/zookeeper-curator\/#\" name=\"fb1072a0c295459e23a47b12e8c09952a5ab3768\"><\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">EnsurePath ensurePath = new EnsurePath(markerPath);\r\nensurePath.ensure(client.getZookeeperClient());\r\nString json = \u201c...\u201d;\r\nif (client.checkExists().forPath(statusFile(core)) != null)\r\n     client.setData().forPath(statusFile(core), json.getBytes());\r\nelse\r\n     client.create().forPath(statusFile(core), json.getBytes());<\/pre>\n<h2 id=\"distributed-locking\" class=\"c8\">Distributed locking<\/h2>\n<p class=\"c8\">Having multiple systems there may be a need of using an exclusive lock for some resource, or perhaps some big system requires it\u2019s components to synchronize based on locks. This \u201crecipe\u201d<br \/>\nis an ideal match for those situations.<a href=\"http:\/\/zygm0nt.github.com\/blog\/2013\/06\/23\/zookeeper-curator\/#\" name=\"1\"><\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">lock = new InterProcessSemaphoreMutex(client, lockPath);\r\nlock.acquire(5, TimeUnit.MINUTES);\r\n\u2026 do sth \u2026\r\nlock.release();<\/pre>\n<p class=\"c8\">\u00a0(from <span class=\"c5\"><a class=\"c0\" href=\"https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/LockingRemotely.java\">https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/LockingRemotely.java<\/a><\/span>)<\/p>\n<h2 id=\"sevice-advertisement\" class=\"c8\">Sevice Advertisement<\/h2>\n<p>This is quite an interesting use case. With many small services on different servers it is not wise to exchange ip addresses and ports between them. When some of those services may go down, while other will try to replace them &#8211; the task gets even harder.<\/p>\n<p class=\"c8\">That\u2019s why, with Zookeeper in place, it can be utilised as a registry of existing services.<\/p>\n<p class=\"c8\">If a service starts, it registers into the ServiceRegistry, offering basic information, like it\u2019s purpose, role, address, and port.<\/p>\n<p class=\"c8\">Services that want to use a specific kind of service request an access to some instance. This way of configuring easily decouples services from their configuration.<\/p>\n<p class=\"c8\">Basically this scenario needs ? steps:<\/p>\n<p>1. Service starts and registers its presence (<a>https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/WorkerAdvertiser.java#L44<\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ServiceDiscovery discovery = getDiscovery();\r\n            discovery.start();\r\n            ServiceInstance si = getInstance();\r\n            log.info(si);\r\n            discovery.registerService(si);\r\n<\/pre>\n<p><a href=\"http:\/\/zygm0nt.github.com\/blog\/2013\/06\/23\/zookeeper-curator\/#\" name=\"1c67229d451cd2d82dddaf1fb12af1aed4a8ecb0\"><\/a><\/p>\n<p class=\"c8\">2. Another service &#8211; on another host or in another JVM on the same machine tries to discover who is implementing the service (<span class=\"c5\"><a class=\"c0\" href=\"https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/WorkerFinder.java#L50\">https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/WorkerFinder.java#L50<\/a><\/span>):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">instances = discovery.queryForInstances(serviceName);\r\n<\/pre>\n<p><a href=\"http:\/\/zygm0nt.github.com\/blog\/2013\/06\/23\/zookeeper-curator\/#\" name=\"0e430a074ab36c8eea3f84fae3d0cffb400dd281\"><\/a><\/p>\n<p class=\"c8\">The whole concept here is ridiculously simple &#8211; the service advertising its presence just stores a file with its whereabouts. The service that is looking for service providers just look into specific directory and read stored definitions.<\/p>\n<p class=\"c8\">In my example, the structure advertised by services looks like this (+ some getters and constructor &#8211; the rest is here: <span class=\"c5\"><a class=\"c0\" href=\"https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/model\/WorkerMetadata.java\">https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/model\/WorkerMetadata.java<\/a><\/span>):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public final class WorkerMetadata {\r\n    private final UUID workerId;\r\n    private final String listenAddress;\r\n    private final int listenPort;\r\n}\r\n<\/pre>\n<p><a href=\"http:\/\/zygm0nt.github.com\/blog\/2013\/06\/23\/zookeeper-curator\/#\" name=\"c0131478bfab43e60b5e077f3170a172a909d199\"><\/a><\/p>\n<h2 id=\"source-code\" class=\"c8\">Source code<\/h2>\n<p>The above recipes are available in Curator library (<a href=\"http:\/\/curator.incubator.apache.org\/\">http:\/\/curator.incubator.apache.org\/<\/a><br \/>\nusage examples are in my github repo at <a href=\"https:\/\/github.com\/zygm0nt\/curator-playground\">https:\/\/github.com\/zygm0nt\/curator-playground<\/a><\/p>\n<h2 id=\"conclusion\" class=\"c8\">Conclusion<\/h2>\n<p>If you&#8217;re in need of a reliable platform for exchanging data and managing synchronization, and you need to do it in a distributed fashion &#8211; just choose Zookeeper. Then add Curator for the ease of using it. Enjoy!<\/p>\n<hr \/>\n<ol class=\"c13\" start=\"1\">\n<li class=\"c7\">image comes from: <span class=\"c5\"><a class=\"c0\" href=\"http:\/\/www.flickr.com\/photos\/jfgallery\/2993361148\">http:\/\/www.flickr.com\/photos\/jfgallery\/2993361148<\/a><\/span><\/li>\n<li class=\"c7\">all source code fragments taken from this repo: <span class=\"c5\"><a class=\"c0\" href=\"https:\/\/github.com\/zygm0nt\/curator-playground\">https:\/\/github.com\/zygm0nt\/curator-playground<\/a><\/span><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\n\n\n\n    An application developed for one of my recent projects at TouK involved multiple servers. There was a requirement to ensure failover for the system&rsquo;s components. Since I had already a few separate components I didn&rsquo;t want to add more of that, and since there already was a Zookeeper ensemble running &#8211; required by one of the services, I&rsquo;ve decided to go that way with my solution.\n\nWhat is Zookeeper?\nJust a crude distributed synchronization framework. However, it implements Paxos-style algorithms (http:\/\/en.wikipedia.org\/wiki\/Paxos_(computer_science)) to ensure no split-brain scenarios would occur. This is quite an important feature, since I don&rsquo;t have to care about that kind of problems while using this app. You just need to create an ensemble of a couple of its instances &#8211; to ensure high availability. It is basically a virtual filesystem, with files, directories and stuff. One could ask why another filesystem? Well this one is a rather special one, especially for distributed systems. The reason why creating all the locking algorithms on top of Zookeeper is easy is its Ephemeral Nodes &#8211; which are just files that exist as long as connection for them exists. After it disconnects &#8211; such file disappears. \n\nWith such paradigms in place it&rsquo;s fairly easy to create some high level algorithms for synchronization.\n\n\nHaving that in place, it&nbsp;can&nbsp;safely integrate multiple services ensuring loose coupling in a distributed way.\n\n\nZookeeper from developer&rsquo;s POV\n\nWith all the base services for Zookeeper started, it seems there is nothing else, than just connect to it and start implementing necessary algorithms. Unfortunately, the API is quite basic and offers files and directories abstractions with the addition of different node type (file types) &#8211; ephemeral and sequence. It is also possible to watch a node for changes.\n\nUsing bare Zookeeper is hard!\nCreating connections is tedious &#8211; and there is lots of things to take care of. Handling an established connection is hard &#8211; when establishing connection to ensemble, it&rsquo;s necessary to negotiate a session also. During the whole process a number of exceptions can occur &#8211; these are &ldquo;recoverable&rdquo;\n    exceptions, that can be gracefully handled and not break the connection.\n\n    class=\"c8\"&gt;&lt;span&gt;So, Zookeeper API is hard.&lt;\/span&gt;&lt;\/p&gt;&lt;p class=\"c1\"&gt;&lt;span&gt;&lt;\/span&gt;&lt;\/p&gt;&lt;p class=\"c8\"&gt;&lt;span&gt;Even if one is proficient with that API, then there come recipes. The reason for using Zookeeper is to be able to implement some more sophisticated algorithms on top of it. Unfortunately those aren&amp;rsquo;t trivial and it is again quite hard to implement them without bugs.&lt;\/span&gt;\r\n\n\n\nAnd since distributed systems are hard, why would anyone want another difficult to handle tool? \n\n\nEnter Curator\n&lt;p\n    class=\"c8\"&gt;&lt;span&gt;Happily, guys from Netflix implemented a nice abstraction for dealing with Zookeeper internals. They called it Curator and use it extensively in the company&amp;rsquo;s environment. Curator offers consistent API for Zookeeper&amp;rsquo;s functionality. It even implements a couple of recipes for distributed systems.&lt;\/span&gt;\r\n\n\n\nFile read\/write\n&lt;p\n    class=\"c8\"&gt;&lt;span&gt;The basic use of Zookeeper is as a distributed configuration repository. For this scenario I only need read\/write capabilities, to be able to write and read files from the Zookeeper filesystem. This code snippet writes a sample json to a file on ZK filesystem.&lt;\/span&gt;\r\n\n\n\n&lt;a href=&#8221;#&#8221;\n                                                                                                  name=\"0\"&gt;&lt;\/a&gt;\r\n\n\r\n\r\nEnsurePath ensurePath = new EnsurePath(markerPath);\r\nensurePath.ensure(client.getZookeeperClient());\r\nString json = &ldquo;...&rdquo;;\r\nif (client.checkExists().forPath(statusFile(core)) != null)\r\n     client.setData().forPath(statusFile(core), json.getBytes());\r\nelse\r\n     client.create().forPath(statusFile(core), json.getBytes());\r\n\r\n\r\n\n\nDistributed locking\nHaving multiple systems there may be a need of using an exclusive lock for some resource, or perhaps some big system requires it&rsquo;s components to synchronize based on locks. This &ldquo;recipe&rdquo;\n    is an ideal match for those situations.\n\nref=\"#\"\r\n                                                                                    name=\"b0329bbbf14b79ffaba1139881914aea887ef6a3\"&gt;&lt;\/a&gt;\r\n\n\n\r\n\r\n\r\nlock = new InterProcessSemaphoreMutex(client, lockPath);\r\nlock.acquire(5, TimeUnit.MINUTES);\r\n&hellip; do sth &hellip;\r\nlock.release();\r\n\r\n\r\n\n&nbsp;(from https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/LockingRemotely.java)\n\n\nSevice Advertisement\n&lt;p\n    class=\"c8\"&gt;&lt;span&gt;This is quite an interesting use case. With many small services on different servers it is not wise to exchange ip addresses and ports between them. When some of those services may go down, while other will try to replace them - the task gets even harder. &lt;\/span&gt;\r\n\n\nThat&rsquo;s why, with Zookeeper in place, it can be utilised as a registry of existing services.\n\n\nIf a service starts, it registers into the ServiceRegistry, offering basic information, like it&rsquo;s purpose, role, address, and port. \n\nServices that want to use a specific kind of service request an access to some instance. This way of configuring easily decouples services from their configuration.\n\n\nBasically this scenario needs ? steps:\n\n&lt;span&gt;1. Service starts and registers its presence (&lt;\/span&gt;&lt;span class=\"c5\"&gt;&lt;a class=\"c0\"\r\n                                                                               href=\"https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/WorkerAdvertiser.java#L44\"&gt;https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/WorkerAdvertiser.java#L44&lt;\/a&gt;&lt;\/span&gt;&lt;span&gt;)&lt;\/span&gt;&lt;span&gt;:&lt;\/span&gt;\r\n\n\n\n\n\r\n\r\n\r\nServiceDiscovery discovery = getDiscovery();\r\n            discovery.start();\r\n            ServiceInstance si = getInstance();\r\n            log.info(si);\r\n            discovery.registerService(si);\r\n\r\n\r\n\r\n\n\n2. Another service &#8211; on another host or in another JVM on the same machine tries to discover who is implementing the service (https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/WorkerFinder.java#L50):\n\n\n&lt;a href=&#8221;#&#8221;\n                                                                                                  name=\"3\"&gt;&lt;\/a&gt;\r\n\n\r\n\r\ninstances = discovery.queryForInstances(serviceName);\r\n\r\n\nThe whole concept here is ridiculously simple &#8211; the service advertising its presence just stores a file with its whereabouts. The service that is looking for service providers just look into specific directory and read stored definitions.\n\n\nIn my example, the structure advertised by services looks like this (+ some getters and constructor &#8211; the rest is here: https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/model\/WorkerMetadata.java):\n\n\n\n\r\n\r\n\r\npublic final class WorkerMetadata {\r\n    private final UUID workerId;\r\n    private final String listenAddress;\r\n    private final int listenPort;\r\n}\r\n\r\n\r\n\n\nSource code\n&lt;p\n    class=\"c8\"&gt;&lt;span&gt;The above recipes are available in Curator library (&lt;\/span&gt;&lt;span class=\"c5\"&gt;&lt;a class=\"c0\"\r\n                                                                                                    href=\"http:\/\/curator.incubator.apache.org\/\"&gt;http:\/\/curator.incubator.apache.org\/&lt;\/a&gt;&lt;\/span&gt;&lt;span&gt;). Recipes&amp;rsquo;\r\nusage examples are in my github repo at &lt;\/span&gt;&lt;span class=\"c5\"&gt;&lt;a class=\"c0\"\r\n                                                                   href=\"https:\/\/github.com\/zygm0nt\/curator-playground\"&gt;https:\/\/github.com\/zygm0nt\/curator-playground&lt;\/a&gt;&lt;\/span&gt;\r\n\n\n\nConclusion\n&lt;p\n    class=\"c8\"&gt;&lt;span&gt;If you&amp;rsquo;re in need of a reliable platform for exchanging data and managing synchronization, and you need to do it in a distributed fashion - just choose Zookeeper. Then add Curator for the ease of using it. Enjoy!&lt;\/span&gt;\r\n\n\n\n\n\n\n\nimage comes from: http:\/\/www.flickr.com\/photos\/jfgallery\/2993361148\n    \nall source code fragments taken from this repo: https:\/\/github.com\/zygm0nt\/curator-playground\n    \n\n\n\n\n\n    An application developed for one of my recent projects at TouK involved multiple servers. There was a requirement to ensure failover for the system&rsquo;s components. Since I had already a few separate components I didn&rsquo;t want to add more of that, and since there already was a Zookeeper ensemble running &#8211; required by one of the services, I&rsquo;ve decided to go that way with my solution.\n\nWhat is Zookeeper?\nJust a crude distributed synchronization framework. However, it implements Paxos-style algorithms (http:\/\/en.wikipedia.org\/wiki\/Paxos_(computer_science)) to ensure no split-brain scenarios would occur. This is quite an important feature, since I don&rsquo;t have to care about that kind of problems while using this app. You just need to create an ensemble of a couple of its instances &#8211; to ensure high availability. It is basically a virtual filesystem, with files, directories and stuff. One could ask why another filesystem? Well this one is a rather special one, especially for distributed systems. The reason why creating all the locking algorithms on top of Zookeeper is easy is its Ephemeral Nodes &#8211; which are just files that exist as long as connection for them exists. After it disconnects &#8211; such file disappears. \n\nWith such paradigms in place it&rsquo;s fairly easy to create some high level algorithms for synchronization.\n\n\nHaving that in place, it&nbsp;can&nbsp;safely integrate multiple services ensuring loose coupling in a distributed way.\n\n\nZookeeper from developer&rsquo;s POV\n\nWith all the base services for Zookeeper started, it seems there is nothing else, than just connect to it and start implementing necessary algorithms. Unfortunately, the API is quite basic and offers files and directories abstractions with the addition of different node type (file types) &#8211; ephemeral and sequence. It is also possible to watch a node for changes.\n\nUsing bare Zookeeper is hard!\nCreating connections is tedious &#8211; and there is lots of things to take care of. Handling an established connection is hard &#8211; when establishing connection to ensemble, it&rsquo;s necessary to negotiate a session also. During the whole process a number of exceptions can occur &#8211; these are &ldquo;recoverable&rdquo;\n    exceptions, that can be gracefully handled and not break the connection.\n\n    class=\"c8\"&gt;&lt;span&gt;So, Zookeeper API is hard.&lt;\/span&gt;&lt;\/p&gt;&lt;p class=\"c1\"&gt;&lt;span&gt;&lt;\/span&gt;&lt;\/p&gt;&lt;p class=\"c8\"&gt;&lt;span&gt;Even if one is proficient with that API, then there come recipes. The reason for using Zookeeper is to be able to implement some more sophisticated algorithms on top of it. Unfortunately those aren&amp;rsquo;t trivial and it is again quite hard to implement them without bugs.&lt;\/span&gt;\r\n\n\n\nAnd since distributed systems are hard, why would anyone want another difficult to handle tool? \n\n\nEnter Curator\n&lt;p\n    class=\"c8\"&gt;&lt;span&gt;Happily, guys from Netflix implemented a nice abstraction for dealing with Zookeeper internals. They called it Curator and use it extensively in the company&amp;rsquo;s environment. Curator offers consistent API for Zookeeper&amp;rsquo;s functionality. It even implements a couple of recipes for distributed systems.&lt;\/span&gt;\r\n\n\n\nFile read\/write\n&lt;p\n    class=\"c8\"&gt;&lt;span&gt;The basic use of Zookeeper is as a distributed configuration repository. For this scenario I only need read\/write capabilities, to be able to write and read files from the Zookeeper filesystem. This code snippet writes a sample json to a file on ZK filesystem.&lt;\/span&gt;\r\n\n\n\n&lt;a href=&#8221;#&#8221;\n                                                                                                  name=\"0\"&gt;&lt;\/a&gt;\r\n\n\r\n\r\nEnsurePath ensurePath = new EnsurePath(markerPath);\r\nensurePath.ensure(client.getZookeeperClient());\r\nString json = &ldquo;...&rdquo;;\r\nif (client.checkExists().forPath(statusFile(core)) != null)\r\n     client.setData().forPath(statusFile(core), json.getBytes());\r\nelse\r\n     client.create().forPath(statusFile(core), json.getBytes());\r\n\r\n\r\n\n\nDistributed locking\nHaving multiple systems there may be a need of using an exclusive lock for some resource, or perhaps some big system requires it&rsquo;s components to synchronize based on locks. This &ldquo;recipe&rdquo;\n    is an ideal match for those situations.\n\nref=\"#\"\r\n                                                                                    name=\"b0329bbbf14b79ffaba1139881914aea887ef6a3\"&gt;&lt;\/a&gt;\r\n\n\n\r\n\r\n\r\nlock = new InterProcessSemaphoreMutex(client, lockPath);\r\nlock.acquire(5, TimeUnit.MINUTES);\r\n&hellip; do sth &hellip;\r\nlock.release();\r\n\r\n\r\n\n&nbsp;(from https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/LockingRemotely.java)\n\n\nSevice Advertisement\n&lt;p\n    class=\"c8\"&gt;&lt;span&gt;This is quite an interesting use case. With many small services on different servers it is not wise to exchange ip addresses and ports between them. When some of those services may go down, while other will try to replace them - the task gets even harder. &lt;\/span&gt;\r\n\n\nThat&rsquo;s why, with Zookeeper in place, it can be utilised as a registry of existing services.\n\n\nIf a service starts, it registers into the ServiceRegistry, offering basic information, like it&rsquo;s purpose, role, address, and port. \n\nServices that want to use a specific kind of service request an access to some instance. This way of configuring easily decouples services from their configuration.\n\n\nBasically this scenario needs ? steps:\n\n&lt;span&gt;1. Service starts and registers its presence (&lt;\/span&gt;&lt;span class=\"c5\"&gt;&lt;a class=\"c0\"\r\n                                                                               href=\"https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/WorkerAdvertiser.java#L44\"&gt;https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/WorkerAdvertiser.java#L44&lt;\/a&gt;&lt;\/span&gt;&lt;span&gt;)&lt;\/span&gt;&lt;span&gt;:&lt;\/span&gt;\r\n\n\n\n\n\r\n\r\n\r\nServiceDiscovery discovery = getDiscovery();\r\n            discovery.start();\r\n            ServiceInstance si = getInstance();\r\n            log.info(si);\r\n            discovery.registerService(si);\r\n\r\n\r\n\r\n\n\n2. Another service &#8211; on another host or in another JVM on the same machine tries to discover who is implementing the service (https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/curator\/WorkerFinder.java#L50):\n\n\n&lt;a href=&#8221;#&#8221;\n                                                                                                  name=\"3\"&gt;&lt;\/a&gt;\r\n\n\r\n\r\ninstances = discovery.queryForInstances(serviceName);\r\n\r\n\nThe whole concept here is ridiculously simple &#8211; the service advertising its presence just stores a file with its whereabouts. The service that is looking for service providers just look into specific directory and read stored definitions.\n\n\nIn my example, the structure advertised by services looks like this (+ some getters and constructor &#8211; the rest is here: https:\/\/github.com\/zygm0nt\/curator-playground\/blob\/master\/src\/main\/java\/pl\/touk\/model\/WorkerMetadata.java):\n\n\n\n\r\n\r\n\r\npublic final class WorkerMetadata {\r\n    private final UUID workerId;\r\n    private final String listenAddress;\r\n    private final int listenPort;\r\n}\r\n\r\n\r\n\n\nSource code\n&lt;p\n    class=\"c8\"&gt;&lt;span&gt;The above recipes are available in Curator library (&lt;\/span&gt;&lt;span class=\"c5\"&gt;&lt;a class=\"c0\"\r\n                                                                                                    href=\"http:\/\/curator.incubator.apache.org\/\"&gt;http:\/\/curator.incubator.apache.org\/&lt;\/a&gt;&lt;\/span&gt;&lt;span&gt;). Recipes&amp;rsquo;\r\nusage examples are in my github repo at &lt;\/span&gt;&lt;span class=\"c5\"&gt;&lt;a class=\"c0\"\r\n                                                                   href=\"https:\/\/github.com\/zygm0nt\/curator-playground\"&gt;https:\/\/github.com\/zygm0nt\/curator-playground&lt;\/a&gt;&lt;\/span&gt;\r\n\n\n\nConclusion\n&lt;p\n    class=\"c8\"&gt;&lt;span&gt;If you&amp;rsquo;re in need of a reliable platform for exchanging data and managing synchronization, and you need to do it in a distributed fashion - just choose Zookeeper. Then add Curator for the ease of using it. Enjoy!&lt;\/span&gt;\r\n\n\n\n\n\n\n\nimage comes from: http:\/\/www.flickr.com\/photos\/jfgallery\/2993361148\n    \nall source code fragments taken from this repo: https:\/\/github.com\/zygm0nt\/curator-playground\n    \n\n\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":{"0":"post-11310","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=11310"}],"version-history":[{"count":13,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11310\/revisions"}],"predecessor-version":[{"id":15440,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11310\/revisions\/15440"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=11310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=11310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=11310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}