This topic describes how to migrate application configurations from a self-managed Apollo configuration center to a Microservices Engine (MSE) Nacos configuration center.
Prerequisites
An MSE Nacos Professional Edition instance is created. For more information about how to create an MSE Nacos instance, see Create a Nacos engine.
Migration description
The following table describes the parameters of the Apollo model and the MSE Nacos model.
Apollo model | MSE Nacos model |
|
|
In this topic, all configurations in a single Apollo environment are migrated as files to a Nacos namespace based on the following mapping rules:
Configurations in different Apollo environments can be mapped to different Nacos instances or different namespaces of a Nacos instance. You can use one of the mapping methods based on your business requirements.
The
clusterparameter of Apollo is mapped to thegroupparameter of Nacos. The{appId}.{namespace}.{format}configuration of Apollo is mapped to thedataIdparameter of Nacos.The
appIdparameter of Apollo is mapped to theappNameparameter of Nacos.
Step 1: Export Apollo configurations
Export Apollo configurations as a .zip package by using a tool
Log on to the self-managed Apollo console.
In this example, you can click the link to go to the self-managed Apollo console.
On the My Applications page, click the name of the desired application.
In the upper-right corner of the application details page, choose Administrator Tools > Configure Export and Import. On the Configure Export and Import page, select Select Export Environment and click Export.
You must separately export configurations in different environments as different .zip files.
Export Apollo configurations as a file by executing an SQL statement
Earlier versions of Apollo do not support the file export feature. In this case, you can directly export configurations from databases. You can execute the following SQL statement to export configurations as a JSON or EXCEL file. The file name extension is .json or .xlsx.
select distinct a.NamespaceId,b.NamespaceName,c.`Comment` AS 'NamespaceDesc' ,b.AppId,b.ClusterName,a.Key,a.Type ,a.Value,a.Comment,a.LineNum from ITEM a left join Namespace b on a.NamespaceId=b.id left join AppNamespace c on c.Name=b.NamespaceName where a.IsDeleted=0 order by a.NamespaceId,a.LineNum ;Step 2: Convert the file format
After you export the Apollo configurations, you can obtain a .zip, .json, or .xlsx file. You must convert the file format to a format that can be directly imported to Nacos in the MSE console.
Run the following command to download the file format conversion tool: curl -O https://msesync.oss-cn-hangzhou.aliyuncs.com/ApolloConfigTransfer.jar.
Run the following command to execute the conversion program: java -DsourceFilePath={Full path of the exported Apollo file} -jar ApolloConfigTransfer.jar.
After the program is executed, a file named nacos_config_import_{Exported Apollo file name}_{Timestamp}.zip is available in the current directory.
Step 3: Import the file to Nacos
Log on to the MSE console, and select a region in the top navigation bar.
In the left-side navigation pane, choose Microservices Registry > Instances. Click the name of the instance.
In the left-side navigation pane, choose Configuration Management > Configurations.
On the page that appears, select a namespace from the Namespace drop-down list and click Import Configuration. In the Import Configuration dialog box, upload the file obtained in Step 2.
NoteAfter you perform the preceding operations, the configurations are migrated from Apollo to Nacos.
Step 4: Change the dependency
Change the dependency of Apollo to the dependency of Spring Cloud Alibaba in the application project.
Before the change:
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>{apollo.version}</version>
</dependency>After the change:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.10</version>
</dependency>If you use Spring Cloud Alibaba 2023.x, upgrade the version to 2023.0.3.2.
If you use Spring Cloud Alibaba 2022.x, upgrade the version to 2022.0.0.2.
If you use Spring Cloud Alibaba 2021.x, upgrade the version to 2021.0.6.2.
If you use Spring Cloud Alibaba 2.2.x, upgrade the version to 2.2.11.
Step 5: Modify the code
Applications support the following Apollo dynamic configuration methods. You can modify the related code based on the descriptions in this section.
@Value annotation
The @Value annotation is used to reference a property value in a Spring bean. The Spring Cloud Alibaba framework supports the annotation without code modification.
@RestController
@RefreshScope
public class DemoController {
@Value("testKey")
String testKey = "value";
@RequestMapping("/valuekey")
public String getNacosTestKey() {
return testKey;
}
}@ApolloConfig annotation
The @ApolloConfig annotation is used to register a Spring bean and automatically inject the property value that is related to a field of the application into the Spring bean. You can replace the property value by using the standard Spring annotation @ConfigurationProperties.
@Configuration
@ConfigurationProperties
public class CNStackInfoConfig {
private String name;
private int customerCount;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCustomerCount() {
return customerCount;
}
public void setCustomerCount(int customerCount) {
this.customerCount = customerCount;
}
}Apollo Config API
An Apollo Config API is called to obtain a single property value in a specific namespace.
ConfigService.getAppConfig().getProperty("testkey", "defaulyv1");
ConfigService.getAppConfig("namespace").getProperty("testkey", "defaulyv1");You can re-encapsulate the Apollo Config API in Nacos. Sample code:
@NacosConfig(dataId="appId1.application.properties",group="group")
Properties nacosConfigProperties;
private void method() {
String value=nacosConfigProperties.getProperty("key","defaultValue");
}ApolloConfigChangeListener callback
If a key of a specific prefix in Apollo changes, the ApolloConfigChangeListener method is called back.
Sample code in Apollo:
/**
* notify listener
* @param configChangeEvent
*/
@ApolloConfigChangeListener(interestedKeyPrefixes = {"data."})
public void apolloNotify(ConfigChangeEvent configChangeEvent) {
System.out.println(configChangeEvent.changedKeys());
}
Sample code in Nacos:
@NacosConfigKeysListener(dataId="appId1.application.properties",group="group",interestedKeyPrefixes = {"data."})
public void apolloNotify(ConfigChangeEvent configChangeEvent) {
System.out.println(configChangeEvent.changedKeys());
}For more information about the usage of annotations in Spring Cloud Alibaba Nacos configuration centers, see Annotations of Nacos configuration centers for Spring Cloud applications.
Step 6: Change the endpoint
Add Nacos configurations to the project code.
## Original Apollo configurations # Environment-Dappllo.env={env} apollo.meta=http://127.0.0.1:8070 apollo.bootstrap.enabled=true # Specify the namespace to which you want to add the configurations. apollo.bootstrap.namespaces=application,namespace1 # Specify the cluster. apollo.bootstrap.cluster=default # Specify the ID of the application. app.id=app1Change the endpoint to the Nacos endpoint.
# Specify dataId and group of the configurations that you added. spring.config.import[0]=nacos:app1.application.properties?group=default&refreshEnabled=true spring.config.import[1]=nacos:app1.namespace1.properties?group=default&refreshEnabled=true # Specify the MSE Nacos endpoint. spring.cloud.nacos.config.server-addr=mse-xxx-p.nacos-ans.mse.aliyuncs.com # Specify the MSE Nacos namespace. spring.cloud.nacos.config.namespace={Namespace ID that corresponds to the specified environment of the MSE Nacos configuration center, such as 5babe1ee-1352-xxxx-bd7b-7e7ce892e2ab}Restart the service.