You can migrate web projects in Spring Boot by using the custom runtime of Function Compute and HTTP triggers to the cloud with a few clicks. You can use a browser or an HTTP client tool such as cURL to access a function. You can modify the Spring Boot sample code to perform secondary development.

Prerequisites

The following operations are complete:

Procedure

  1. Run the following command in the target directory to initialize the project:
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. Run the following command to go to the code directory:
    cd start-fc-custom-samples/java8/http-demo/code
  3. Run the following command to install dependencies:
    mvn package
    Sample output:
    [INFO] Scanning for projects...
    ......
    [INFO]
    [INFO] --------------------------< com.example:demo >--------------------------
    [INFO] Building demo 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    ......
    [INFO]
    [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
    ......
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 1 resource
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo ---
    ......
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 2 source files to /test/start-fc-custom-samples/java8/http-demo/code/target/classes
    [INFO]
    [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory /test/start-fc-custom-samples/java8/http-demo/code/src/test/resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to /test/start-fc-custom-samples/java8/http-demo/code/target/test-classes
    [INFO]
    [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo ---
    ......
    [INFO]
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] Running com.example.demo.DemoApplicationTests
    ......
    
      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.1.8.RELEASE)
    
    2021-09-26 10:30:16.078  INFO 16623 --- [           main] com.example.demo.DemoApplicationTests    : Starting DemoApplicationTests on iZbp180k0vbjx91bee86q6Z with PID 16623 (started by root in /test/start-fc-custom-samples/java8/http-demo/code)
    2021-09-26 10:30:16.083  INFO 16623 --- [           main] com.example.demo.DemoApplicationTests    : No active profile set, falling back to default profiles: default
    2021-09-26 10:30:18.070  INFO 16623 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2021-09-26 10:30:18.368  INFO 16623 --- [           main] com.example.demo.DemoApplicationTests    : Started DemoApplicationTests in 2.668 seconds (JVM running for 3.619)
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.523 s - in com.example.demo.DemoApplicationTests
    2021-09-26 10:30:18.664  INFO 16623 --- [       Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
    [INFO]
    [INFO] Results:
    [INFO]
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO]
    [INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ demo ---
    ......
    [INFO] Building jar: /test/start-fc-custom-samples/java8/http-demo/code/target/demo.jar
    [INFO]
    [INFO] --- spring-boot-maven-plugin:2.1.8.RELEASE:repackage (repackage) @ demo ---
    ......
    [INFO] Replacing main artifact with repackaged archive
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 04:32 min
    [INFO] Finished at: 2021-09-26T10:31:41+08:00
    [INFO] ------------------------------------------------------------------------
  4. Deploy the project.
    1. Run the following command to go to the project directory:
      cd ../
    2. Run the following command to deploy the project:
      s deploy -y
      Sample output:
      [2021-09-26T10:38:17.293] [INFO ] [S-CLI] - Start ...
      [2021-09-26T10:38:18.866] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
      [2021-09-26T10:38:18.867] [INFO ] [FC-DEPLOY] - Using access alias: default
      [2021-09-26T10:38:18.867] [INFO ] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
      [2021-09-26T10:38:18.867] [INFO ] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
        Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
      [2021-09-26T10:38:19.789] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
      [2021-09-26T10:38:20.226] [INFO ] [FC-DEPLOY] - Checking Function springbootHttpFunc exists
      [2021-09-26T10:38:22.881] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists
      [2021-09-26T10:38:22.921] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists
        Make service custom-demo success.
        Make function custom-demo/springbootHttpFunc success.
        Make trigger custom-demo/springbootHttpFunc/httpTrigger success.
      [2021-09-26T10:40:51.357] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
      [2021-09-26T10:40:51.578] [INFO ] [FC-DEPLOY] - Checking Function springbootHttpFunc exists
      [2021-09-26T10:40:51.688] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists
      [2021-09-26T10:40:51.888] [INFO ] [FC-DEPLOY] - Using customDomain: auto: fc will try to generate related custom domain resources automatically
        End of request
        Deployed.
        End of request
      [2021-09-26T10:40:59.885] [INFO ] [FC-DEPLOY] - Generated auto custom domain: springboothttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
      [2021-09-26T10:40:59.886] [INFO ] [FC-DEPLOY] - Creating custom domain: springboothttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
      [2021-09-26T10:41:00.068] [INFO ] [FC-DOMAIN] - Creating custom domain: springboothttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
      
      Tips for next step
      ======================
      * Display information of the deployed resource: s info
      * Display metrics: s metrics
      * Display logs: s logs
      * Invoke remote function: s invoke
      * Remove Service: s remove service
      * Remove Function: s remove function
      * Remove Trigger: s remove trigger
      * Remove CustomDomain: s remove domain
      
      
      
      fc-springboot-helloworld:
        region:   cn-hangzhou
        service:
          name: custom-demo
        function:
          name:       springbootHttpFunc
          runtime:    custom
          handler:    com.example.demo.DemoApplication::main
          memorySize: 1024
          timeout:    15
        url:
          system_url:    https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/custom-demo/springbootHttpFunc/
          custom_domain:
            -
              domain: http://springboothttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
        triggers:
          -
            type: http
            name: httpTrigger

Result

After the project is deployed, you can view the temporary domain name generated by Function Compute in the execution output. The temporary domain name can be used to access the newly deployed application. For example, run the following command to access the application:

curl http://springboothttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
Note The temporary domain name is used only for demonstration and development purposes. It is valid for a limited period of time. You must use a domain name that has obtained an Internet Content Provider (ICP) filing from Alibaba Cloud for production. For more information, see Bind a custom domain name.