Welcome to ZK etcd config


Welcome to ZK etcd config

ZK etcd config is a lightweight dynamic attribute configuration interface, which supports both etcd v3 and zookeeper middleware

github address

With ZK etcd config, you can flexibly switch between etcd or zookeeper as the dynamic attribute configuration middleware, and use a simple interface to monitor the dynamic changes of attribute values.

Sample code

(1) Usage: suppose our system has two parameters that need to be configured dynamically. When the microservice cluster of the system is started, we need to dynamically capture the changes of these two parameter values. If we use zookeeper as middleware storage, we will assemble all parameters into a file similar to properties.application. The format is as follows:


Then we can write the above configuration content to the data of the / config node of zookeeper. When the value of param1 or param2 in / config changes, change notifications are received in real time through the IWatcher interface. Sample code

String kind="zookeeper";
String hosts="localhost:2181,localhost:2183";
String path="/config";

IConfiger config = ConfigerFactory.createFactory(kind, hosts, path);
String value = config.get("param1");
LOG.error("###key={},value={}", "param1", value);
IWatcher w = new TestWatcher();
config.addWatcher("param1", w);

TestWatcher is a custom class that implements the IWatcher interface:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import houyijun.dynamic.config.IWatcher;
public class TestWatcher implements IWatcher{
	private static final Logger LOG = LoggerFactory.getLogger(TestWatcher.class);	

	public void onChanged(String key, String oldValue, String newValue) {

kind indicates which middleware to use, and the currently supported value is: "etcd-v3" indicates the V3 version interface of etcd. "Zookeeper" represents the interface using zookeeper. hosts represents the address of etcd or zookeeper, separated by "," in ip:port format. Path represents the path of the node where the dynamic attribute is stored on etcd or zookeeper. IWatcher is a listening event interface defined as:

public interface IWatcher {
	 * watcher event happened
	 * @param key
	 * node path
	 * @param oldValue
	 * old value
	 * @param newValue
	 * new value
	void onChanged(String key,String oldValue,String newValue);

(2) Close config on last exit:


maven pom dependency package

In addition to ZK etcd config, the dependency package of etcd and zookeeper must be added.

	<!-- etcd V3  -->
	<!-- zookeeper client -->

Tags: Zookeeper Attribute github Apache

Posted on Mon, 04 May 2020 17:14:42 -0400 by Popple3