文档

从Apollo迁移到MSE Nacos

更新时间:

本文介绍自建Apollo配置中心如何迁移到MSE Nacos配置中心。

前提条件

已创建MSE Nacos集群,且Nacos版本为专业版。具体操作,请参见创建Nacos引擎

使用限制

Apollo版本为Apollo2.0及以上。

步骤一:发布配置

  1. 登录自建Apollo控制台。

    本文以官方Demo地址为例。

  2. 我的应用页面,单击目标应用名称。

  3. 在目标应用详情页的右上方,选择管理员工具 > 配置导出导入。在配置导出导入页面,勾选选择导出的环境,然后单击导出

    导出需要迁移的配置至本地后。按照Apollo和Nacos数据结构的对应关系,布局本地配置的存储结构。

    Apollo和Nacos的数据结构的推荐对应关系如下。

    Apollo数据结构

    Nacos数据结构

    环境

    Namespace

    集群

    group

    Namespace

    dataId

    下列图示为本地配置的存储结构布局图。存储结构部署

  4. 将导出的配置文件放入迁移脚本同目录下,迁移脚本会读取配置文件中的配置内容。使用迁移脚本将配置文件重新发布至MSE Nacos。

步骤二:更改依赖

在应用项目中将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配置项。

    1. 在您的业务工程中找到@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;
          }
      }
    2. 将所有@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中增加相关配置项。

    1. 在您的业务工程中找到@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;
          }
      }
    2. 将所有@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;
          }
      }
    3. bootstrap.properties中增加如下配置项。

      name=jack
      customerCount=200

步骤四:修改Endpoint

  1. 修改Apollo的Endpoint。

    如下所示,将自建Apollo的URL,替换为MSE的公网地址。

    修改前:

     -Dapollo.configService=http://{Apollo Server网络地址}:{Apollo Server port}

    修改后:

     -Dspring.cloud.nacos.config.server-addr={MSE Nacos网络地址}:8848
  2. 重启服务。

  • 本页导读 (1)
文档反馈