The development of dynamic perception program for distributed application system server_

I. function

1. Dynamic changes in the number of servers

2. The client can dynamically monitor the online server

3. Specific ideas:

(1) as soon as the server is started, go to zookeeper to register the information (create). Pay attention to using temporary nodes when registering instead of permanent nodes (because when the server is hung up, the temporary nodes will also disappear)

(2) the client, getchildren(), obtains the node information and registers for listening. When a server is offline, it listens for the information, then acquires the server list again and registers for listening.

2, Specific code

1. Client code

package cn.itcast.bigdata.zkDis;

import java.util.ArrayList;
import java.util.List;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

public class DistributeClient {
	private ZooKeeper zkClient = null;
	private static final String connectString="10.177.21.1:2181,10.177.21.2:2181,10.177.21.4:2181";
	private static final int sessionTimeout = 50000;
	private static final String parentNode="/servers";
	//volatile: ensure data consistency in multithreading
	private volatile List<String> serverList;
	/**
	 * 
	 * Get zk server cluster connection
	 * @throws Exception
	 */
	public void getConnect() throws Exception{
		zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			@Override
			//The client can respond to zookeeper's notification
			public void process(WatchedEvent event) {
				
				//Update the server list again and register to listen
				try {
					getServerList();
				} catch (Exception e) {
					
				}
			}
		});
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
	}
	
	/**
	 * Get server information list
	 */
	
	public void getServerList() throws KeeperException, InterruptedException{
		//Get the information of the server's child node and listen to the parent node
		List<String> children = zkClient.getChildren(parentNode, true);
		List<String> servers = new ArrayList<String>();
		for(String child:children){
			//Child is just a child node name
			byte[] data = zkClient.getData(parentNode+"/"+child, false, null);
			servers.add(new String(data));
		}
		serverList = servers;
		//Print the server list
		System.out.println(serverList);
	}
	
	/**
	 * Business functions
	 * @throws InterruptedException 
	 */
	public void handleBusiness() throws InterruptedException{
		System.out.println("client is working...");
		Thread.sleep(Long.MAX_VALUE);
	}
	
	
	public static void main(String[] args) throws Exception {
		
		//Get links
		DistributeClient client =new DistributeClient();
		client.getConnect();
		
		//Get the sub node information of the server (and listen), and get the server information list from it
		client.getServerList();
		//Business thread start
		client.handleBusiness();
		
		
	}
	
}

2. server side

package cn.itcast.bigdata.zkDis;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

/**
 * 
 * Development of dynamic awareness program on and off the distributed application system server
 * 
 * @author Administrator
 *
 */



public class DistributedServer {
	
	private ZooKeeper zkClient = null;
	private static final String connectString="10.177.21.1:2181,10.177.21.2:2181,10.177.21.4:2181";
	private static final int sessionTimeout = 50000;
	private static final String parentNode="/servers";
	/**
	 * 
	 * Get zk server cluster connection
	 * @throws Exception
	 */
	public void getConnect() throws Exception{
		zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			@Override
			//The client can respond to zookeeper's notification
			public void process(WatchedEvent event) {
				//Callback function after event notification
				System.out.println(event.getType()+ "----"+event.getPath());
				//Because the listener listens only once, a new listener is called here
				try {
					zkClient.getChildren("/", true);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
			}
		});
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
	}
	
	/**
	 * Register information with zk server
	 * @param hostname
	 */
	
	public void registerServer(String hostname) throws KeeperException, InterruptedException{
		String create = zkClient.create(parentNode+"/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
		System.out.println(hostname+"is online.."+create);
	}
	
	/**
	 * Business functions
	 * @throws InterruptedException 
	 */
	public void handleBusiness(String hostname) throws InterruptedException{
		System.out.println(hostname+"is working...");
		Thread.sleep(Long.MAX_VALUE);
	}
	
	
	
	public static void main(String[] args) throws Exception {
		
		//Get zk connection
		DistributedServer server = new DistributedServer();
		server.getConnect();
		//Using zk connection to register server information
		server.registerServer(args[0]);
		//Start business function
		server.handleBusiness(args[0]);
	}
	
	
}

 

Published 7 original articles, won praise 0, visited 4
Private letter follow

Keywords: Zookeeper Apache Java

Added by Galahad on Sun, 26 Jan 2020 17:26:51 +0200