Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use zk-etcd-config

Shulou Source: shulou.com Published: 2022-05-31 15:45:21 09月21日 Update

This article mainly explains "how to use zk-etcd-config". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use zk-etcd-config.

Zk-etcd-config is a lightweight dynamic property configuration interface that supports both etcd v3 and zookeeper middleware.

Github address

With zk-etcd-config, you can flexibly switch and use etcd or zookeeper as dynamic attribute configuration middleware, and use a simple interface to listen for the dynamic change of attribute values.

Sample code

(1) use: suppose our system has two parameters that need to be configured dynamically, and when the micro-service cluster of the system starts, we need to dynamically capture the changes of these two parameter values. If we use zookeeper as the middleware to store, all the parameters are assembled into a file similar to properties.application in the following format:

Param1=value1param2=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, the change notification is 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 what kind of middleware to use. Currently, the supported values are: "etcd-v3" indicates the interface with the V3 version of etcd. "zookeeper" indicates the interface that uses zookeeper. Hosts represents the address of etcd or zookeeper, separated by "," and in ip:port format. Path represents the node path where dynamic attributes are stored on etcd or zookeeper. IWatcher is a listening event interface, which is 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 when you exit:

Config.close (); maven pom dependency package

Dependency packages in addition to zk-etcd-config, you must add dependency packages for etcd and zookeeper.

Houyijun.dynamic zk-etcd-config 0.2.0 io.etcd jetcd-core 0.4.1 org.apache.curator curator-recipes 4.0.1 Org.apache.zookeeper zookeeper 3.4.8 to this I believe you have a deeper understanding of "how to use zk-etcd-config". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Interface dynamic middleware content properties configuration parameters changes two code address format example system node storage learning support monitoring utility Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Huawei MariaDB Redmi macOS