Migrate a Spring Boot project to a SOFABoot project

更新时间:
复制 MD 格式

SOFABoot is built on the Spring Boot framework, so you can easily migrate from Spring Boot to SOFABoot. After reading the Precautions in this article, you can follow the Migration steps to change an existing Spring Boot project to a project that uses the SOFABoot framework.

Precautions

  • The SOFABoot framework is built based on Spring Boot 2.1.0.RELEASE. You may change the Spring Boot version during the migration. If incompatibilities occur during the upgrade process, you can contact the technical team.

  • Tracer is integrated into the starter of each middleware. You do not need to add dependencies or configure the tracker.

Migration steps

You can perform the following steps to complete project migration.

  1. In the application.properties configuration file of the project, add basic configurations. Among them, you need to specify the following two system configuration items:

    • spring.application.name: required. This is the application name and maintains the extension and consistency of Spring Boot.

    • logging.path: required. Configure the output path of the log.

      Note

      See properties-system for a detailed description of these two configuration items.

  2. In the application.properties configuration file of the project, modify the middleware configuration items. For more information, see Adding SOFA middleware> properties configuration items.

  3. Open the pom.xml file in the project root directory and configure the following version dependencies in the parent section:

    <parent>
         <groupId>com.alipay.sofa</groupId>
         <artifactId>sofaboot-enterprise-dependencies</artifactId>
         <version>3.1.1</version>
    </parent>
  4. Introduce the starter of the corresponding middleware into the pom.xml. For more information, see Adding SOFA middleware> Adding the starter for the relevant middleware service. In this example, SOFAREST is added. You only need to configure the following dependencies in the pom.xml:

    <dependency>
         <groupId>com.alipay.sofa</groupId>
         <artifactId>rest-enterprise-sofa-boot-starter</artifactId>
         <version>3.0.0</version>
    </dependency>
  5. Declare rest facade interface. Example:

    @Path("/webapi/test")
    @Consumes("application/json;charset=UTF-8")
    @Produces("application/json;charset=UTF-8")
    public interface TestFacade{
    
        @GET
        public String test();
    
    }
  6. Declares the facade implementation class. Example:

    public class TestServiceImpl implements TestFacade{
         @Override
         public String test(){
             return "test";
         }
    }
  7. Declare the implementation class as a bean. Example:

    <bean id="testServiceImpl" class="com.alipay.test.endpoint.impl.TestServiceImpl"/>
  8. Start the application.

    • Access the http://localhost:8341/webapi/test in a browser and you can see the data returned by the rest interface.

    • In the ${logging.path}/logs/tracelog/rest-server-digest.log file, you can view the link information (tracer) log of the access.

      Note

      {logging.path} to the log output path you set in the configuration file.

For more information about other SOFA middleware integration methods, see the integration documentation of the corresponding middleware starter.