Tutorial example: Use dynamic configuration

更新时间:
复制 MD 格式

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.

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. Run grep com.antcloud.tutorial.configuration.DynamicConfig drm-boot.log to 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 set methods. If these methods are missing, resource registration fails. For example, the str property in the code is a resource property. Resource properties can only be of the String or primitive data types.

  • The resource class must have the @DObject annotation from the com.alipay.drm.client.api.annotation package. Its properties must have the @DAttribute annotation.

  • The @BeforeUpdate and @AfterUpdate annotations 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 set method.

  • Note: If the business logic takes a long time to execute, you must process it asynchronously. This prevents the dynamic client from reporting an interrupt error due to a timeout.