通过函数计算的Custom Runtime结合HTTP触发器的方式,您可以将Spring Boot的Web项目一键迁移至云上,并可直接使用浏览器或者cURL等HTTP客户端工具访问函数。您可以按需修改Spring Boot的示例代码实现二次开发。

前提条件

您已成功完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. 执行以下命令,进入code项目目录内。
    cd start-fc-custom-samples/java8/http-demo/code
  3. 执行以下命令,安装依赖。
    mvn package
    输出示例:
    [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. 部署项目。
    1. 执行以下命令,进入项目目录。
      cd ../
    2. 执行以下命令,部署项目。
      s deploy -y
      输出示例:
      [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

执行结果

成功部署该项目后,您可以在执行输出中查看到函数计算生成的临时域名,通过该临时域名可以访问刚部署的应用,例如执行以下命令访问应用:

curl http://springboothttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名