The Dynamic Configuration (DRM) feature is built on SOFABoot projects. This topic describes how to implement dynamic configuration.
Prerequisites
You have set up the environment. For more information, see Set up the environment.
You have downloaded the sample project and changed the SOFABoot version number in the project's main
pom.xmlfile to the latest version. For more information, see Version Guide.
Procedure
Develop the application locally.
In your SOFABoot project, add the following dependency to the module that contains the dynamic configuration class:
<dependency> <groupId>com.alipay.sofa</groupId> <artifactId>ddcs-enterprise-sofa-boot-starter</artifactId> </dependency>You do not need to specify the version for this dependency.
Configure security settings.
To ensure the security of the middleware, all calls must authenticate the caller. For more information, see Import SOFA middleware.
Create a dynamic configuration class.
The following code provides an example of a configuration class:
@DObject(region ="AntCloud", appName ="dynamic-configuration-tutorial", id ="com.antcloud.tutorial.configuration.DynamicConfig") public class DynamicConfig{ @DAttribute private String name; @DAttribute private int age; @DAttribute private boolean man; public void init(){ DRMClient.getInstance().register(this); } public String getName(){ return name; } public void setName(String name){ this.name = name; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } public boolean isMan(){ return man; } public void setMan(boolean man){ this.man = man; } }Description:
This configuration class is a standard Java class that must follow the JavaBean specification. It contains several private properties with corresponding
getandsetmethods. For example,name,age, andmanin the code are resource properties. Resource properties can only beStringor primitive data types.Add the
@DObjectannotation to the configuration class. The package name for this annotation iscom.alipay.drm.client.api.annotation. The@DObjectannotation requires theid,region, andappNameproperties.id: A globally unique string. Typically, the fully qualified class name is used to ensure uniqueness. If you do not set this property, the fully qualified class name is used by default.region: Used to distinguish domains for different organizations. For example, you can set an independent domain for each subsidiary.appName: The name of the application.
Add the
@DAttributeannotation to the resource properties. The package name is alsocom.alipay.drm.client.api.annotation.The dynamic configuration framework uses reflection to call
getandsetmethods to read and write resource properties. In specific scenarios, you can change the content of thegetandsetmethods. However, do not change the method signature, which includes the method name, parameters, and return value. When the system starts, the framework checks whether the property complies with the JavaBean specification. If it does not, the framework skips the registration of that property.The
@BeforeUpdateand@AfterUpdateannotations are optional. To perform a common operation, such as log printing, before or after each property update, create a method that accepts(String, Object)parameters and has no return value. Then, add the corresponding annotation to the method.NoteWhen these two methods are called, the parameters passed are the property name and the input parameter of the current set method, not the values of the private property before and after the update. These methods are only suitable for secondary tasks such as log printing. Place the actual business logic in the set method.
After you call the
registermethod, data updates from the server-side are pushed to the client within seconds.
Configure the XML file.
Here is an example:
<bean id="dynamicConfig" class="com.antcloud.tutorial.configuration.tutorial.config.DynamicConfig" init-method="init"/>
Start the application.
You can start the application in two ways:
Local startup: Start the SOFABoot project that you developed locally.
Cloud startup: Package the local project and publish it to the cloud. For more information, see Publish to the cloud.
Configure dynamic configuration in the cloud.
You can go to the microservice console to create, manage, and push dynamic configuration items. This lets you dynamically update configurations without restarting the application. For more information, see Add a dynamic configuration and Push a dynamic configuration.