通过函数计算的Custom Container,您可以使用Java在函数计算平台编辑函数。本文介绍如何快速部署和调用Java函数。您也可以按需修改Spring Boot的示例代码实现二次开发。

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-container-http-springboot -d start-cc-http-springboot
    -d用于指定项目名称,本示例项目名称为start-cc-http-springboot。
    说明 若已存在该项目则跳过此步骤。
    输出示例:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     file decompression completed
     please select credential alias default
    
     Thanks for using Serverless-Devs
     You could [cd /test/start-cc-http-springboot] and enjoy your serverless journey!
     If you need help for this example, you can use [s -h] after you enter folder.
     Document Star:https://github.com/Serverless-Devs/Serverless-Devs
  2. 执行以下命令,进入项目目录:
    cd start-cc-http-springboot
  3. 执行以下命令,安装依赖:
    s build --use-docker --dockerfile ./code/Dockerfile
    输出示例:
    [2021-10-13T17:01:34.063] [INFO ] [S-CLI] - Start ...
    [2021-10-13T17:01:34.670] [INFO ] [FC-BUILD] - Build artifact start...
    [2021-10-13T17:01:34.690] [INFO ] [FC-BUILD] - Use docker for building.
    [2021-10-13T17:01:34.691] [INFO ] [FC-BUILD] - Building image...
    Sending build context to Docker daemon  27.65kB
    Step 1/12 : FROM maven:3.8.2-openjdk-8
    ......
     ---> Running in f86fa02fd322
    Removing intermediate container f86fa02fd322
     ---> 1abe1ad95d22
    Successfully built 1abe1ad95d22
    Successfully tagged registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-laravel.laravel:LATEST-1630478186515
    Build image(registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-laravel.laravel:LATEST-1630478186515) successfully
    [2021-10-13T17:03:54.055] [INFO ] [FC-BUILD] - Build artifact successfully.
    
    Tips for next step
    ======================
    * Invoke Event Function: s local invoke
    * Invoke Http Function: s local start
    * Deploy Resources: s deploy
    End of method: build
  4. 执行以下命令,部署项目:
    s deploy --push-registry acr-internet -y
    输出示例:
    [2021-10-13T17:21:58.539] [INFO ] [S-CLI] - Start ...
    [2021-10-13T17:21:59.529] [INFO ] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-10-13T17:21:59.530] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-10-13T17:21:59.531] [INFO ] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-10-13T17:21:59.531] [INFO ] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN**** 
     ......
    
    There is auto config in the service: container-demo
    
    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
    
    
    
    customContainer-demo:
      region:   cn-shenzhen
      service:
        name: container-demo
      function:
        name:       springboot-http-function
        runtime:    custom-container
        handler:    not-used
        memorySize: 256
        timeout:    60
      url:
        system_url:    https://188077086902****.cn-shenzhen.fc.aliyuncs.com/2016-08-15/proxy/container-demo/springboot-http-function/
        custom_domain:
          -
            domain: http://springboot-http-function.container-demo.188077086902****.cn-shenzhen.fc.devsapp.net
      triggers:
        -
          type: http
          name: httpTrigger

执行结果

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

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