Welcome to ZK etcd config

https://github.com/houyijun/zk-etcd-config Welcome to ZK etcd config ZK etcd config is a lightweight dynamic attribute c...
Sample code
maven pom dependency package

https://github.com/houyijun/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:

param1=value1 param2=value2

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); config.start(); 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) { LOG.error("###changed,old={},new={}",oldValue,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:

config.close();

maven pom dependency package

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

<dependency> <groupId>houyijun.dynamic</groupId> <artifactId>zk-etcd-config</artifactId> <version>0.1.0</version> </dependency> <!-- etcd V3 --> <dependency> <groupId>io.etcd</groupId> <artifactId>jetcd-core</artifactId> <version>0.4.1</version> </dependency> <!-- zookeeper client --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency>

4 May 2020, 17:14 | Views: 7362

Add new comment

For adding a comment, please log in
or create account

0 comments