# Dynamic Configuration
This document describes how the Sermant plugin uses the dynamic configuration function.
Dynamic configuration function is one of the core services of sermant-core module (opens new window). This feature allows Sermant to pull configurations from a dynamic configuration center for rich and diverse service governance capabilities.
Refer to Sermant Dynamic Configuration Center User Manual (opens new window) on how to use the dynamic configuration center. The following section describes how to develop dynamic-configuration-related capabilities in plugins.
# Dynamic Configuration API
The functionality API
of Dynamic Configuration Service is provided by the abstract class DynamicConfigService (opens new window) , which implements three interfaces, as seen in API (opens new window) directory, The concrete interface is as follows::
Interface | Method | Explanation |
---|---|---|
KeyService (opens new window) | String getConfig(String) | Get the configured value for a key (default group). |
KeyService (opens new window) | boolean publishConfig(String, String) | Set value for a key (default group) . |
KeyService (opens new window) | boolean removeConfig(String) | Remove a configured value for a key (default group). |
KeyService (opens new window) | List<String> listKeys() | Get all keys (default group). |
KeyService (opens new window) | boolean addConfigListener(String, DynamicConfigListener (opens new window)) | Add a listener for a key (default group). |
KeyService (opens new window) | boolean removeConfigListener(String) | Remove a listener for a key (default group). |
KeyGroupService (opens new window) | String getConfig(String, String) | Get the configured value for a key in the group. |
KeyGroupService (opens new window) | boolean publishConfig(String, String, String) | Set value for a key in the group. |
KeyGroupService (opens new window) | boolean removeConfig(String, String) | Remove the configured value for a key in the group. |
KeyGroupService (opens new window) | boolean addConfigListener(String, String, DynamicConfigListener (opens new window)) | Add a listener for a key in the group. |
KeyGroupService (opens new window) | boolean removeConfigListener(String, String) | Remove a listener for a key in the group. |
GroupService (opens new window) | List<String> listKeysFromGroup(String) | Get all keys in the group. |
GroupService (opens new window) | boolean addGroupListener(String, DynamicConfigListener) | Add listeners for all keys in the group. |
GroupService (opens new window) | boolean removeGroupListener(String) | Remove listeners for all keys in the group. |
DynamicConfigService (opens new window) | boolean addConfigListener(String, DynamicConfigListener, boolean) | Add a listener for a key(default group). Whether to trigger the initialization event depends on the input parameters |
DynamicConfigService (opens new window) | boolean addConfigListener(String, String, DynamicConfigListener (opens new window), boolean) | Add a listener for a key in the group. Whether to trigger the initialization event depends on the input parameters. |
DynamicConfigService (opens new window) | boolean addGroupListener(String, DynamicConfigListener (opens new window), boolean) | Add listeners for all keys in the group. Whether to trigger the initialization event depends on the input parameters. |
Above all, two concepts need to be clear:
Key
, a single reference to a dynamical configuration keyGroup
, a dynamical configuration set of groups, often used to distinguish between users
As you can see, the above API
is mainly divided into data adding, deleting, querying and modifying operations, and add/remove operations of the listener's DynamicConfigListener (opens new window). The latter event callback is a crucial part of the functionality of the Dynamic Configuration Service, which is the main feature of the plugin using Dynamic Configuration Service.
Also, in the KeyService (opens new window) interface, all the API
defined are API
without Group
. They will actually use the default Group
and be fixed to API
of KeyGroupService (opens new window) in DynamicConfigService (opens new window). The default Group
can be modified via dynamic.config.default_group
in the unified configuration file config.properties
.
Finally, besides the above service interfaces, there are a few other interfaces, configurations, or entities that developers need to pay attention to:
Static configuration DynamicConfig (opens new window) for Dynamic Configuration Service, whose parameters configuration refers to Sermant Dynamic Configuration Center User Manual (opens new window) .
Dynamic configuration service implementation typeDynamicConfigServiceType (opens new window), contains:
Enum Explanation ZOOKEEPER ZooKeeper KIE ServiceComb Kie NOP No implementation Dynamic configuration listener DynamicConfigListener (opens new window), which contains the following interface methods:
Method Explanation void process(DynamicConfigEvent (opens new window)) Callback interface for handling change events of configuration Change events of dynamic configuration DynamicConfigEvent (opens new window), whose member properties are as follows:
Type Property Explanation String key Key of configuration String group Group of configuration String content Content of configuration DynamicConfigEventType (opens new window) changeType Type of configuration change event Type of change events of dynamic configuration DynamicConfigEventType (opens new window), which contains following four kinds:
Enum Explanation INIT Initial response when adding a listener CREATE Event of adding new configuration MODIFY Event of modifying configuration DELETE Event of deleting configuration
# Dynamic Configuration Development Example
The DynamicConfigService
instance of the dynamic configuration function can be obtained with the following code:
DynamicConfigService service = ServiceManager.getService(DynamicConfigService.class);
Once the service instance is obtained, you can invoke the API listed in the table above to take the appropriate action. For example, to register a listener, you can do this by:
// ${group} is user group,${key} is the key listened. For zookeeper, the path is: / + ${group} + ${key}
// if ${group} do not exist,it will set the value by dynamicconfig.default_group in unified configuration
service.addConfigListener("${key}", "${group}", new DynamicConfigListener() {
@Override
public void process(ConfigChangedEvent event) {
// do something
}
});
Once the listener is registered, the process
method will be triggered when the server creates, deletes, modifies, or adds child nodes.
Examples of other interfaces are not listed here, you can refer to the API description above.