本文介绍自建Apollo配置中心如何迁移到MSE Nacos配置中心。
前提条件
使用限制
Apollo版本为Apollo2.0及以上。
步骤一:发布配置
步骤二:更改依赖
在应用项目中将Apollo的依赖更改为Spring Cloud Alibaba的依赖。
修改前:
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>2.0.0</version>
</dependency>
说明 Apollo版本要求为Apollo2.0及以上。
修改后:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
步骤三:改造代码
- 如果您的工程中使用了
@EnableApolloConfig
注解,请将所有@EnableApolloConfig
注解的类替换为@ConfigurationProperties
注解。说明 对于Apollo中在@Value
注解上写入的默认值,可对应Spring Cloud Alibaba体系中的bootstrap.properties
配置项。- 在您的业务工程中找到
@EnableApolloConfig
注解,如下所示。@Configuration @EnableApolloConfig public class CNStackInfoConfig { @Value("${apollo.name:jack}") private String name; @Value("${apollo.customerCount:200}") 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; } }
- 将所有
@EnableApolloConfig
注解的类替换为@ConfigurationProperties
注解。@Configuration @ConfigurationProperties(prefix = "apollo") 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; } }
- 在您的业务工程中找到
- 如果您的工程中使用了
@ApolloConfig
注解,将其修改为@ConfigurationProperties(prefix = "")
注解,并且在bootstrap.properties
中增加相关配置项。- 在您的业务工程中找到
@ApolloConfig
注解,如下所示。@Configuration @ApolloConfig 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; } }
- 将所有
@EnableApolloConfig
注解的类替换为@ConfigurationProperties(prefix = "")
注解。@Configuration @ConfigurationProperties(prefix = "") 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; } }
- 在
bootstrap.properties
中增加如下配置项。name=jack customerCount=200
- 在您的业务工程中找到