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:
/**
* @param client client instance
* @param mode creation/protection mode
* @param basePath the base path for the node
* @param data data for the node
*/
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.
public class PersistentEphemeralNodeExample
{
private static final String PATH = "/example/ephemeralNode";
/span>private static final string PATH2 = "/ Example/node "
public static void main(String[] args) throws Exception
{
/span>curatorframework client = curatorframeworkfactory newclient ( "127.0.0.1:2181" new exponentialbackoffretry Span class= "pun" > ( 1000 3
client.getConnectionStateListenable().addListener(new ConnectionStateListener()
{
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
System.out.println("连接状态:" + newState.name());
}
});
cl ient start
/span>persistentephemeralnode node = Span class= "PLN" > new Persistentephemeralnode ( client mode ephemeral PATH "temporary node" getbytes
node.start();
node.waitForInitialCreate(3, TimeUnit.SECONDS);
/span>string Actualpath = node getactualpath
/span>system out println ( "Temporary node path:" + Actualpath + + new string ( Client getdata (). forpath actualpath
cl ient create (). forpath ( path2 span class= "str" > "persistence node" getbytes
/span>system out println ( "persisted node path:" + PATH2 + + new string ( Client getdata (). forpath path2
KillSession.kill(client.getZookeeperClient().getZooKeeper(), "127.0.0.1:2181");
/span>system out println ( "Temporary node path:" + Actualpath + + ( client checkexists (). forpath ( actualpath != null Span class= "pun"));
/span>system out println ( "persisted node path:" + PATH2 + + new string ( Client getdata (). forpath path2
CloseableUtils.closeQuietly(node);
CloseableUtils.closeQuietly(client);
}
}
3. Sample program Run ResultsTo Run the results console:
连接状态:CONNECTED
临时节点路径:/example/ephemeralNode | 值: 临时节点
持久化节点路径: /example/node | 值: 持久化节点
连接状态:SUSPENDED
连接状态:LOST
连接状态:RECONNECTED
临时节点路径:/example/ephemeralNode | 是否存在: true
持久化节点路径: /example/node | 值: 持久化节点
--------------------------------------------------------------------------------------------------------------- ----------------
From for notes (Wiz)
10.Curator Temporary node