10.Curator Temporary node

Source: Internet
Author: User

    Using curator can also simplify the operation of ephemeral node (temporary nodes).temporary nodes are present in the zookeeper and are deleted when the connection and session are broken. For example, through the Zookeeper Publishing Service, the service starts to register its own information as a temporary node, when the service is broken zookeeper the temporary node deleted, so that the client will not get the service information. 1.PersistentEphemeralNode class    The Persistentephemeralnode class represents a temporary node. Its constructor is as follows:
  
 
  1. /**
  2. * @param client client instance
  3. * @param mode creation/protection mode
  4. * @param basePath the base path for the node
  5. * @param data data for the node
  6. */
  7. public PersistentEphemeralNode(CuratorFramework client, Mode mode, String basePath, byte[] initData)
Other parameters good understanding, not understand is Persistentephemeralnode.mode:
    • Ephemeral: Creates a node in zookeeper createmode.ephemeral mode.
    • Ephemeral_sequential: If path already exists, create the node with Createmode.ephemeral, otherwise create the node in createmode.ephemeral_sequential way.
    • Protected_ephemeral: Created in createmode.ephemeral to provide protection.
    • Protected_ephemeral_sequential: Similar to ephemeral_sequential, providing protection.
    protection means a very marginal situation: When the server creates a node, but the nodes are not returned to the client, the server may crash, then the ZK session is still valid, so this temporary node will not be deleted. For the client, it cannot know which node was created by them.
    even if it is not sequential-ephemeral, the server may be created successfully but the client is unaware of the node that was created for some reason.
    Curator provides a protection mechanism for these potentially unattended nodes. When these nodes are created, a GUID is added. The retry mechanism can occur if the node creation fails properly. When retrying, the parent path is searched first, the node is searched based on the GUID, and if such a node is found, the node is considered to be the first attempt to create a successful but missing node and then returned to the caller. Note:The node must call the Start method to start. Call the Close method when not in use.Persistentephemeralnode internally handles the error state itself. 2. Writing the sample program    Our example creates two nodes, one for temporary nodes, and one for persistent nodes. As you can see, the temporary node does not exist after the client is re-connected.
  
 
  1. public class PersistentEphemeralNodeExample
  2. {
  3. private static final String PATH = "/example/ephemeralNode";
  4. /span>private static final string PATH2 = "/ Example/node "
  5. public static void main(String[] args) throws Exception
  6. {
  7. /span>curatorframework client = curatorframeworkfactory newclient ( "127.0.0.1:2181" new exponentialbackoffretry Span class= "pun" > ( 1000 3
  8. client.getConnectionStateListenable().addListener(new ConnectionStateListener()
  9. {
  10. @Override
  11. public void stateChanged(CuratorFramework client, ConnectionState newState)
  12. {
  13. System.out.println("连接状态:" + newState.name());
  14. }
  15. });
  16. cl ient start
  17. /span>persistentephemeralnode node = Span class= "PLN" > new Persistentephemeralnode ( client mode ephemeral PATH "temporary node" getbytes
  18. node.start();
  19. node.waitForInitialCreate(3, TimeUnit.SECONDS);
  20. /span>string Actualpath = node getactualpath
  21. /span>system out println ( "Temporary node path:" + Actualpath + + new string ( Client getdata (). forpath actualpath
  22. cl ient create (). forpath ( path2 span class= "str" > "persistence node" getbytes
  23. /span>system out println ( "persisted node path:" + PATH2 + + new string ( Client getdata (). forpath path2
  24. KillSession.kill(client.getZookeeperClient().getZooKeeper(), "127.0.0.1:2181");
  25. /span>system out println ( "Temporary node path:" + Actualpath + + ( client checkexists (). forpath ( actualpath != null Span class= "pun"));
  26. /span>system out println ( "persisted node path:" + PATH2 + + new string ( Client getdata (). forpath path2
  27. CloseableUtils.closeQuietly(node);
  28. CloseableUtils.closeQuietly(client);
  29. }
  30. }
3. Sample program Run ResultsTo Run the results console:
  
 
  1. 连接状态:CONNECTED
  2. 临时节点路径:/example/ephemeralNode | 值: 临时节点
  3. 持久化节点路径: /example/node | 值: 持久化节点
  4. 连接状态:SUSPENDED
  5. 连接状态:LOST
  6. 连接状态:RECONNECTED
  7. 临时节点路径:/example/ephemeralNode | 是否存在: true
  8. 持久化节点路径: /example/node | 值: 持久化节点
--------------------------------------------------------------------------------------------------------------- ----------------

From for notes (Wiz)

10.Curator Temporary node

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.