Synchronizing a cluster configuration with ZooKeeper

Source: Internet
Author: User

Synchronize the cluster configuration with ZooKeeper, when you need to modify all the node configuration, update the configuration to a node of ZooKeeper, causing the node data to change,

Local watcher on all other nodes that need to be configured synchronously immediately discovers the change in node state and updates the latest data to a local

Change the official demo, in response to watcher with Conffileutil update the local configuration can be

[Java]View Plaincopyprint?
  1. Public class Executor implements Watcher, Runnable,
  2. Datamonitor.datamonitorlistener {
  3. String Znode;
  4. DataMonitor DM;
  5. ZooKeeper ZK;
  6. String filename;
  7. Public Executor (string hostport, String znode, string filename)
  8. throws Keeperexception, IOException {
  9. this.filename = filename;
  10. ZK = New ZooKeeper (Hostport, N, this );
  11. DM = New DataMonitor (ZK, Znode, this );
  12. }
  13. public static void Main (string[] args) {
  14. args = new string[3];
  15. args[0] = Constant.zkpeer;
  16. args[1] = Constant.znode;
  17. args[2] = "";
  18. String hostport = args[0];
  19. String Znode = args[1];
  20. String filename = args[2];
  21. try {
  22. New Executor (Hostport, Znode, filename). Run ();
  23. } catch (Exception e) {
  24. E.printstacktrace ();
  25. }
  26. }
  27. public void Process (Watchedevent event) {
  28. Dm.process (event);
  29. }
  30. public Void Run () {
  31. try {
  32. synchronized (this) {
  33. While (!dm.dead) {
  34. Wait ();
  35. }
  36. }
  37. } catch (Interruptedexception e) {
  38. }
  39. }
  40. public void closing (int rc) {
  41. synchronized (this) {
  42. Notifyall ();
  43. }
  44. }
  45. /** 
  46. * Processing Data
  47. */
  48. public void exists (byte[] data) {
  49. System.out.println ("...  Invoked:Executor.exists (byte[] data) ... ");
  50. Conffileutil.sync (data);
  51. }
  52. }

The Executor above is used to establish and maintain connections with Znode.

DataMonitor is still responsible for receiving event processing results, and this class is basically not changed.

DataMonitor delegate Executor to update local configuration when watcher is triggered

Full code See GITHUB

One thing to be aware of when updating nodes

[Java]View Plaincopyprint?
    1. Zk.setdata (Constant.znode, Bytes,-1, null, null);


This byte[] is limited in size, and if it exceeds the limit, ZooKeeper will not tell you that the official document is 1M, which I did not test.

When the update data is unsuccessful and there are no exceptions, you need to check if the array is too long.

Synchronizing a cluster configuration with ZooKeeper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.