Recently, some of my developer friends want to migrate their Java Spring application configuration to Alibaba Cloud Application Configuration Management (ACM). During the migration process, they asked some interesting questions. This article uses a simple example to explain their concerns during the migration process and gives the corresponding solutions. I hope you will find them helpful.
This is the first question for all users who want to migrate their configuration to ACM. Let's analyze this question in two dimensions—timeliness and security.
Static configuration items refer to those that basically do not need to be modified after the application is released. For example:
Dynamic configuration refers to some configuration items which may change when the program is running. Such changes usually affect the running behavior of the program, for example:
In terms of timeliness, we recommend that users save their own copies of static configuration items, which should be as simple as possible. Dynamic configuration items need to be saved to the ACM to increase the flexibility and the timeliness of dynamic changes.
Non-sensitive configuration items generally refer to technology-oriented configuration items. Exposing them does not cause security risks. For example:
Sensitive configuration items are often associated with business data and can cause security risks if they are disclosed to unauthorized persons, for example:
In terms security, we recommend that users save their own copies of the non-sensitive configuration items, which should be as simple as possible. Sensitive configuration items need to be saved to the ACM. For sensitive configuration items, encryption and authentication are required, and these items must be secured from unauthorized persons.
The summary for the timeliness and security analysis
Java developers who use the Spring framework usually use the @value function to automatically insert configuration.
For example, this configuration contains two configuration items, the software version number and the database connection string:
We can automatically insert the configuration items by using the @PropertySource and @value annotations.
@Configuration
@ComponentScan("com.alibaba")
@PropertySource("classpath:myApp.properties")
public class AppConfig {
@Value(value="${url}")
private String URL;
@Value(value="${dbuser}")
private String USER;
@Value(value="${driver}")
private String DRIVER;
@Value(value="${dbpassword}")
private String PASSWORD;
@Value(value="${appVersion}")
private String version;
}
Operations such as the connection and initialization of the related database are omitted in the above code.
For the sake of security compliance or configuration timeliness, we need to migrate our configuration to the ACM. After the analysis, we found that we'd better migrate some database configurations to ACM. These configuration items are marked in red. The red part will be migrated to the ACM.
Next, we need to make the following three modifications.
First, directly create a configuration item on ACM with the name of myapp.dbconfig.properties, and edit the configuration content in the corresponding edit box. For detailed instructions, see the ACM Quick Start document. The operation screenshots are as follows:
Second, add dependencies into maven's pom.xml file:
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>0.2.1- RC1</version>
</dependency>
Third, add API annotations to the corresponding AppConfig.java code, to enable ACM to retrieve dynamic configurations. Add the red part to the code.
@Configuration @ComponentScan("com.journaldev")
@PropertySource("classpath:myApp.properties")
@EnableNacosConfig(globalProperties =
@NacosProperties(endpoint = "acm.aliyun.com", namespace = "xxx", accessKey = "xxx", secretKey = "xxx"))
@NacosPropertySource(dataId = "myApp.dbconfig.properties", autoRefreshed = true) public class AppConfig {
@Value(value="${url}") private String URL;
@Value(value="${dbuser}") private String USER;
@Value(value="${driver}") private String DRIVER;
@Value(value="${dbpassword}") private String PASSWORD;
@Value(value="${appVersion}")
private String version; public String getVersion() {
return version;
}
}
Now, the modification is finished. Because the ACM SDK supports Spring's @value annotation function, we barely need to modify the code.
Notes:
In the above code example, note that:
If you are interested in Nacos, the open source version of Alibaba Cloud ACM, visit our official website at https://developer.alibabacloud.com/opensource/project/nacos
Alibaba Cloud Hybrid Backup Recovery: Asia's First SAP-Certified Cloud Backup Product
2,599 posts | 764 followers
FollowAlibaba Cloud Serverless - December 20, 2022
Alibaba Clouder - November 2, 2018
Alibaba Clouder - November 27, 2020
Alibaba Clouder - November 27, 2020
Alibaba Clouder - July 5, 2019
Alibaba Cloud Community - March 22, 2022
2,599 posts | 764 followers
FollowLearn More
API Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.
Learn MoreBuild business monitoring capabilities with real time response based on frontend monitoring, application monitoring, and custom business monitoring capabilities
Learn MoreMore Posts by Alibaba Clouder