This topic provides an example of how to implement business logic and troubleshoot logs for dynamic configuration.
Preparations
This dynamic configuration example is based on SOFABoot. Before you start, ensure that you are familiar with SOFABoot.
Set up the basic SOFABoot environment. For more information, see Set up the environment.
To compile and test locally in an offline environment for joint debugging, you must learn how to compile and run a SOFABoot project. For more information, see Run locally.
To deploy the sample code on a server, see Publish to the cloud.
Download the sample project for dynamic configuration.
Step 1: Manage in the cloud
In the sample code, configure the cloud-managed dynamic configuration class. For more information, see Quick Start for Dynamic Configuration.
Step 2: Publish the application
For instructions, see Publish to the cloud.
After the application is published, you can view the ECS instances that are running the code and their in-memory property values on the Resource Query page in the Dynamic Configuration console.
Step 3: Push resources
Enter a new value on the property details page for dynamic configuration and then push the configuration. The configuration is immediately pushed to subscribed clients, which updates the corresponding dynamic configuration property values on those clients. For instructions, see Push dynamic configurations.
After you push the configuration, you can view the subscribed clients and the in-memory value of the property on those clients in real time.
View results
Log on to the application's ECS instance using a Secure Shell (SSH) tool. Then, view the client and business logs:
Dynamic configuration client startup log
Log path: /home/admin/drm/drm-boot.log.
If no errors are logged, the dynamic configuration client started correctly.
Search by resource ID. For example, the resource ID in the sample is
com.antcloud.tutorial.configuration.DynamicConfig. Rungrep com.antcloud.tutorial.configuration.DynamicConfig drm-boot.logto view the registration logs for this resource.
Dynamic configuration push log
If the client starts correctly, push the resource. Then, check the dynamic configuration push log at /home/admin/drm/drm-monitor.log for the following information:
Receive update command from zdrmdata server: This message indicates that the client received an update command from the server.Query data from zdrmdata: This message indicates that the client queried the server and updated the property to the latest pushed value.
Business log
Based on the path configured in log4j, a Logger also prints business logs in set and before/update to /home/admin/logs/service/default.log.
Code analysis
DynamicConfig is a dynamic configuration class with the following characteristics:
It is a standard Java class that follows the JavaBean specification.
All its properties must have get and
setmethods. If these methods are missing, resource registration fails. For example, thestrproperty in the code is a resource property. Resource properties can only be of theStringor primitive data types.The resource class must have the
@DObjectannotation from thecom.alipay.drm.client.api.annotationpackage. Its properties must have the@DAttributeannotation.The
@BeforeUpdateand@AfterUpdateannotations are used to execute a common operation, such as log printing, before or after each property is updated. These methods are optional and should be used only for logic outside the main business flow.Place the business logic in the property's
setmethod.Note: If the business logic takes a long time to execute, you must process it asynchronously. This prevents the dynamic client from reporting an
interrupterror due to a timeout.