# Dynamic Configuration Function

This article describes how to use the dynamic configuration capabilities provided by Sermant in your development.

# Function Introduction

Dynamic Configuration function allows Sermant to perform configuration management and monitoring operations on the configuration issued by the dynamic configuration Center to achieve rich and diverse service governance capabilities.

Note:How to deploy and use the dynamic Configuration Center can be found in Sermant Dynamic Configuration Center User Manual.

# Development Examples

This development example is based on the project created in the Create the First Plugin document to demonstrate the development process with the acquisition of a dynamic configuration:

  1. Add new variable dynamicConfigService in io.sermant.template.TemplateDeclarer class of template\template-plugin, the new variable in the class gets the dynamic configuration service provided by the Sermant framework, which is used to create dynamically configured listeners and obtain configurations:

    DynamicConfigService dynamicConfigService = ServiceManager.getService(DynamicConfigService.class);
    
  2. After obtaining the service instance, we can call the API provided by DynamicConfigService to take action accordingly. For this example, we will use direct access to dynamic config as an example, which can be achieved with the following code:

    @Override
    public ExecuteContext before(ExecuteContext context) throws Exception {
      System.out.println("Good morning!");
      // test_group is user group,test_key is the key to listen to, for zookeeper,configure the obtaining path equivalent to: /test_group/test_key
      System.out.println(dynamicConfigService.getConfig("test_key", "test_group"));
      return context;
    }
    

    Once you're done, follow the build package process you used when creating your first plugin and run mvn package in your project root to generate the build.

  3. Take the Zookeeper deployment as an example of the dynamic configuration center, please refer to the Sermant dynamic configuration Center manual for relevant parameter configuration. Issue the example configuration in Zookeeper's /test_group/test_key node:

    create /test_group/test_key "This is a dynamic config!"
    
  4. Start the Zookeeper server and set the dynamic.config related configuration in agent/config/config.properties, and set the dynamic configuration switch agent.service.dynamic.config.enable to true. The main configuration examples are as follows:

    # Dynamically configure service switches
    agent.service.dynamic.config.enable=true
    # The server address for the configuration center
    dynamic.config.serverAddress=127.0.0.1:2181
    # The type for the configuration center, The range of values is NOP(No implementation)、ZOOKEEPER、KIE、NACOS
    dynamic.config.dynamicConfigType=ZOOKEEPER
    
  5. When the execution is complete, execute cd agent/ in the root directory of the project and run the test Application with Sermant in it, executing the command java-javaagent: sermant-agent.jar-jar application.jar

    $ java -javaagent:sermant-agent.jar -jar Application.jar
    [xxxx-xx-xxTxx:xx:xx.xxx] [INFO] Loading god library into BootstrapClassLoader.
    [xxxx-xx-xxTxx:xx:xx.xxx] [INFO] Building argument map by agent arguments.
    [xxxx-xx-xxTxx:xx:xx.xxx] [INFO] Loading core library into SermantClassLoader.
    [xxxx-xx-xxTxx:xx:xx.xxx] [INFO] Loading sermant agent, artifact is: default
    [xxxx-xx-xxTxx:xx:xx.xxx] [INFO] Load sermant done, artifact is: default
    Good morning!
    This is a dynamic config!
    Good afternoon!
    Good night!
    

As you can see, the configuration issued in the dynamic configuration center can be obtained by the getConfig function and output to the console in step 2.

Examples of other interfaces are not listed here. You can refer to the following API descriptions.

# API&Configuration

# API

The Dynamic Configuration functionality service functionality API is provided by the DynamicConfigService (opens new window) abstract class, which implements three interfaces, as found in the API (opens new window) directory.

# Get the dynamic configuration service

  • Get the dynamic configuration service, which is used to implement the creation of dynamic configuration listeners, configuration acquisition, etc
DynamicConfigService dynamicConfigService = ServiceManager.getService(DynamicConfigService.class);

There are three concepts that need to be clarified for the API we'll cover next:

  • key,Only a dynamically configured key
  • group,A set of dynamically configured groupings, often used to distinguish users
  • content,It refers to the specific content of dynamic configuration

Note:In the following API example parameters, key is uniformly configured as test_key, group as test_group, and content astest_content.

  • Gets the configuration value of a key in the dynamic configuration center

    dynamicConfigService.getConfig("test_key")
    
  • Set the configuration value of a key in the dynamic configuration center

    dynamicConfigService.publishConfig("test_key", "test_content")
    
  • Remove the configuration value of a key in the dynamic configuration center

    dynamicConfigService.removeConfig("test_key")
    
  • Obtain all configuration values in the dynamic configuration center

    dynamicConfigService.listKeys();
    
  • Add a listener for a key in the dynamic configuration center. The listener process function performs custom actions after listening for an event

    dynamicConfigService.addConfigListener("test_key", new DynamicConfigListener() {
      @Override
      public void process(DynamicConfigEvent dynamicConfigEvent) {
        // do something
      }
    });
    
  • Add a listener for a key in the dynamic configuration center. According to the third parameter ifNotify, determine whether to trigger the initialization event after adding the listener. The listener process function can perform custom operations on the initialization event and other subsequent events after listening

    dynamicConfigService.addConfigListener("test_key", new DynamicConfigListener() {
      @Override
      public void process(DynamicConfigEvent dynamicConfigEvent) {
        // do something
      }
    }, true);
    
  • Removes a listener for a key in the dynamic configuration center

    dynamicConfigService.removeConfigListener("test_key");
    

# This section describes how to perform operations on all configuration keys in a group

  • Gets all keys in a group in the dynamic configuration center

    dynamicConfigService.listKeysFromGroup("test_group")
    
  • Add a listener for all keys under a group in the dynamic configuration Center. The listener process function performs custom actions after listening for an event

    dynamicConfigService.addGroupListener("test_group", new DynamicConfigListener() {
      @Override
      public void process(DynamicConfigEvent dynamicConfigEvent) {
        // do something
      }
    });
    
  • Add listeners for all keys in a group in the dynamic configuration center. According to the third parameter ifNotify, determine whether to trigger the initialization event after adding the listener. The listener process function can perform custom operations on the initialization event and other subsequent events after listening

    dynamicConfigService.addGroupListener("test_group", new DynamicConfigListener() {
      @Override
      public void process(DynamicConfigEvent dynamicConfigEvent) {
        // do something
      }
    }, true);
    
  • Removes listeners for all keys in a group in the dynamic configuration center

    dynamicConfigService.removeConfigListener("test_group");
    
  • Gets the configuration value of a key in a group in the dynamic configuration center

    dynamicConfigService.getConfig("test_key", "test_group")
    
  • Set the configuration value of a key in a group in the dynamic configuration center

    dynamicConfigService.publishConfig("test_key", "test_group", "test_content")
    
  • Remove the configuration value of a key in a group in the dynamic configuration center

    dynamicConfigService.removeConfig("test_key", "test_group")
    
  • Add a listener for a key under a group in the dynamic configuration Center, and the listener process function can perform a custom action after listening for an event

    dynamicConfigService.addConfigListener("test_key", "test_group", new DynamicConfigListener() {
      @Override
      public void process(DynamicConfigEvent dynamicConfigEvent) {
        // do something
      }
    });
    
  • Add a listener for a key under a group in the dynamic configuration center. According to the third parameter ifNotify, determine whether to trigger the initialization event after adding the listener. The process function of the listener can perform custom operations on the initialization event and other subsequent events after listening

    dynamicConfigService.addConfigListener("test_key", "test_group", new DynamicConfigListener() {
      @Override
      public void process(DynamicConfigEvent dynamicConfigEvent) {
        // do something
      }
    }, true);
    
  • Remove a listener for a key in a group in the dynamic configuration center

    dynamicConfigService.removeConfigListener("test_key", "test_group");
    

# Illustrate

The above API is mainly divided into the operation of adding, selecting, changing and deleting data, and the operation of adding and deleting Listener DynamicConfigListener (opens new window). The event callback of the latter is a crucial part of the function of the dynamic configuration service, and is also the main function of the plugin using dynamic configuration service.

Also, it's important to note that all APIs for certain configuration keys areAPIs without Group. They all use the default Group in DynamicConfigService (opens new window). The default Group can be changed by sermant-agent configuration file , which is dynamic.config.default of/config/config.properties . Parameters can be described in Sermant-agent User manual.

Finally, in addition to the above service interfaces, there are a few other interfaces, configurations, or entities that developers need to focus on:

# Configuration

The configuration of dynamic configuration function can be refered to the Sermant Dynamic Configuration Center User Manual related chapters about parameter configuration.

Last Updated: 12/13/2024, 2:38:15 AM