curator listening zk temporary node to achieve "Information Center"

We establish a connection using a curator, curator session has maintained a retry mechanism, better support for recursively create nodes and delete nodes:

RetryPolicy = RetryPolicy new new ExponentialBackoffRetry (1000,. 3 ); 
CuratorFramework Client = 
                CuratorFrameworkFactory.builder () 
                        .connectString ( "10.10.210.123:2181" ) 
                        .sessionTimeoutMs ( 50000000 ) 
                        .connectionTimeoutMs ( 50000000 ) 
                        .retryPolicy (retryPolicy) 
                        .build (); 
client.start (); // after the start you can operate using Curator zk, do not forget to call the close method used up to release later.

General codes can first determine whether there is after creating the root node to communicate a message:

 Stat s = client.checkExists().forPath("/msg_node_list");
            if (s == null) {
                String o = client.create().withMode(CreateMode.PERSISTENT).forPath("/msg_node_list");
            }

Netty for each of our services, temporary node to msg_node_list registered according to their service id:

client.create().withMode(CreateMode.EPHEMERAL).forPath("/msg_node_list/b");

Our monitoring center, responsible for client distribution specific netty linked server, you need to monitor every polling temporary child node status, and by listening watch abnormal offline node, update the list of services to redis, so that the client It provides messaging services:

. List <String> = client.getChildren list () forPath ( "/ msg_node_list" );
 // call the following code through each provisional child node in the list, the parameter list for the node name in the temporary withdrawn: 
client.getChildren ( ) .usingWatcher (w) .forPath ( " / msg_node_list / b");
 Watcher w = new Watcher() {
        @Override
        public void process(WatchedEvent watchedEvent) {
            Event.EventType type = watchedEvent.getType();
            if (type.equals(Event.EventType.NodeDeleted)) {
                System.out.println(watchedEvent.getPath());
            }
        }
    };

If netty service client connections abnormal offline, then the visits monitoring server for available netty address.

redis in maintains a list of each netty server corresponding client id, stored in a set of redis .

Monitoring server internally maintains a collection holding netty server address, and the number of connected clients from less to more aligned, each held preferentially allocated to the client's minimum service netty .

If the netty service is not available, and the list is not promptly removed.

Guess you like

Origin www.cnblogs.com/zzq-include/p/12124898.html