Implementation of unified configuration management based on zookeeper __zookeeper

Source: Internet
Author: User
Tags zookeeper

Why to use a unified configuration.

We use the configuration of the project, such as database configuration, etc... We are all written dead in the project, if you need to change, then also modify the configuration file and then put into production, then the problem, if the cluster, there are 100 machines, this time to make changes that is too unrealistic; then you need to use the unified configuration management.

Solving Ideas

1. Extract the public configuration

2. Maintenance of public configuration

3. The application does not need to be redeployed after modifying the public configuration

Adoption of the programme

1. Public configuration extraction stored in the Zookeeper and landing database

2. Publish to zookeeper and landing database after modification of public configuration

3. For application open configuration real-time monitoring, zookeeper configuration file once modified, the application can be real-time monitoring and acquisition


The following is a rough implementation of a unified configuration management based on zookeeper

The jar that needs to be used is zkclient

Configuration file Config

Package com.cwh.zk.util;

Import java.io.Serializable;

public class Config implements serializable{

	/**
	 * */
	private static final long Serialversionuid = 1l;< C6/>private String usernm;
	Private String USERPW;
	
	Public config () {
	} public
	Config (string usernm, String userpw) {
		this.usernm = usernm;
		THIS.USERPW = USERPW;
	}
	Public String getusernm () {return
		usernm;
	}
	public void setusernm (String usernm) {
		this.usernm = usernm;
	}
	Public String GETUSERPW () {return
		userpw;
	}
	public void Setuserpw (String userpw) {
		THIS.USERPW = USERPW;
	}
	@Override public
	String toString () {return
		"Config [usernm=" + usernm + ", userpw=" + USERPW + "]";
	}
	
}
Configuration Management Center Zkconfigmag
Package com.cwh.zk.util;

import org. I0Itec.zkclient.ZkClient;

public class Zkconfigmag {

	private config config;
	/**
	 * Load configuration from database
	/public config Downloadconfigfromdb () {
		//getdb
		config = new config ("nm", "PW");
		return config;
	}
	
	/**
	 * configuration file Upload to database
	/public void Uploadconfigtodb (string nm, string pw) {
		if (config==null) config = New Config ();
		CONFIG.SETUSERNM (nm);
		CONFIG.SETUSERPW (PW);
		UpdateDB
	}
	
	/**
	 * configuration file sync to zookeeper
	/public void Syncconfigtozk () {
		zkclient ZK = new Zkclient ("localhost:2181");
		if (!zk.exists ("/zkconfig")) {
			zk.createpersistent ("/zkconfig", True);
		}
		Zk.writedata ("/zkconfig", config);
		Zk.close ();
	}

Application of monitoring to implement Zkgetconfigclient
Package com.cwh.zk.util; import org.
I0Itec.zkclient.IZkDataListener; import org.

I0Itec.zkclient.ZkClient;

	public class Zkgetconfigclient {private config config;
		Public Config GetConfig () {zkclient ZK = new Zkclient ("localhost:2181");
		Config = (config) zk.readdata ("/zkconfig");
		
		SYSTEM.OUT.PRINTLN ("Load to Configuration:" +config.tostring ()); Listener configuration file Modification zk.subscribedatachanges ("/zkconfig", new Izkdatalistener () {@Override public void Handledatachange (stri
				Ng arg0, Object arg1) throws Exception {config = (config) arg1;
			System.out.println ("Supervisor hears configuration file is modified:" +config.tostring ());
				@Override public void handledatadeleted (String arg0) throws Exception {config = null;
			System.out.println ("Supervisor hears configuration file is deleted");
		}
			
		});
	return config;
		public static void Main (string[] args) {zkgetconfigclient client = new Zkgetconfigclient ();
		Client.getconfig ();
		System.out.println (Client.config.toString ()); for (int i = 0;i<10;i++) {System.out.println (client.coNfig.tostring ());
			try {thread.sleep (1000);
			catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
 }
		}
		
	}

	
}
Test, start Configuration Management Center

Package com.cwh.zkConfig.test;

Import Com.cwh.zk.util.Config;
Import Com.cwh.zk.util.ZkConfigMag;

public class Zkconfigtest {public

	static void Main (string[] args) {
		Zkconfigmag mag = new Zkconfigmag ();
		Config config = mag.downloadconfigfromdb ();
		System.out.println (".....) Load Database configuration ... "+config.tostring ());
		Mag.syncconfigtozk ();
		System.out.println (".....) Sync configuration file to zookeeper ... ");
		
		Take a rest, see more clearly
		try {
			thread.sleep (10000);
		} catch (Interruptedexception e) {
			//TODO auto-generated Catch block
			e.printstacktrace ();
		}
		
		Mag.uploadconfigtodb ("CWHCC", "PASSWORDCC");
		System.out.println (".....) Modify configuration file ... "+config.tostring ());
		Mag.syncconfigtozk ();
		System.out.println (".....) Sync configuration file to zookeeper ... ");
		
		
	}
	

Test results:

Configuration Management Center Print:

Application monitoring:



ok! a rough realization, as in the case of deficiencies.




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.