本文介绍如何将Spring Boot应用部署到函数计算。与传统的部署方法相比,您可将传统的Spring Boot应用一键部署至远端直接用于生产。跳过购买机器等步骤的同时,还拥有了弹性伸缩、按量付费和免运维等特性。

前提条件

您已完成以下操作:

背景信息

Spring Boot是由Pivotal团队在2013年开始研发,于2014年04月发布第一个版本的全新开源轻量级框架。它基于Spring 4.0设计,不仅继承了Spring框架原有的优秀特性,还通过简化配置进一步简化Spring应用的整个搭建和开发过程。此外Spring Boot通过集成大量的框架,使得依赖包的版本冲突、引用的不稳定性等问题得到了解决。

操作步骤

  1. 在目标目录中执行以下命令,初始化项目。
    s init devsapp/start-springboot -d start-springboot
    -d用于指定项目名称,本示例以项目名称start-springboot为例,介绍如何将Spring Boot迁移到函数计算。
    说明 若已存在该项目则跳过此步骤。
    输出示例:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     devsapp!start-springboot@0.0.5.zip file decompression completed
     please select credential alias default
    
         _____            _            ______             _
        /  ___|          (_)           | ___ \           | |
        \ `--. _ __  _ __ _ _ __   __ _| |_/ / ___   ___ | |_
         `--. \ '_ \| '__| | '_ \ / _` | ___ \/ _ \ / _ \| __|
        /\__/ / |_) | |  | | | | | (_| | |_/ / (_) | (_) | |_
        \____/| .__/|_|  |_|_| |_|\__, \____/ \___/ \___/ \__|
              | |                  __/ |
              |_|                 |___/
    
    
        Welcome to the start-springboot application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
             ACR: https://cr.console.aliyun.com/
         This application can help you quickly deploy the SpringBoot project:
             Full yaml configuration    : https://github.com/devsapp/sprintboot#%E5%AE%8C%E6%95%B4yaml
             SpringBoot development docs: https://spring.io/projects/spring-boot/
         This application homepage: https://github.com/devsapp/start-springboot
    
    
    ‍ Thanks for using Serverless-Devs
     You could [cd /test/start-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-springboot
  3. 执行以下命令,部署项目:
    s deploy
    输出示例:
    [2021-09-01T14:15:08.995] [INFO ] [S-CLI] - Start ...
     Using web framework type: nas, If you want to deploy with container, you can [s cli fc-default set web-framework container] to switch.
    [2021-09-01T14:15:10.352] [INFO ] [WEB-FRAMEWORK] - The configuration of the domain name is not detected, and a temporary domain name is generated.
    (node:45614) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
      End of request
      Deployed.
      End of request
    [2021-09-01T14:15:14.711] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-01T14:15:14.712] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-01T14:15:14.712] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-01T14:15:14.712] [INFO ] [FC-DEPLOY] - Using accessKeySecret: KdcUEJLoGto****
      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-01T14:15:14.944] [INFO ] [FC-DEPLOY] - Checking Service web-springboot exists
    [2021-09-01T14:15:15.417] [INFO ] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
    [2021-09-01T14:15:15.480] [INFO ] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-09-01T14:15:15.763] [INFO ] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-09-01T14:15:15.940] [INFO ] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-09-01T14:15:16.426] [INFO ] [FC-DEPLOY] - Using logConfig: auto: fc will try to generate default sls project
    [2021-09-01T14:15:16.636] [INFO ] [SLS] - Checking Project 188077086902****-cn-hangzhou-logproject exists
    [2021-09-01T14:15:16.954] [INFO ] [SLS] - Checking Logstore 188077086902****-cn-hangzhou-logproject/fc-service-web-springboot-logstore exists
    [2021-09-01T14:15:18.988] [INFO ] [SLS] - Checking Logstore index 188077086902****-cn-hangzhou-logproject/fc-service-web-springboot-logstore exists
      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-01T14:15:19.024] [INFO ] [FC-DEPLOY] - Generated logConfig:
    enableInstanceMetrics: true
    enableRequestMetrics: true
    logstore: fc-service-web-springboot-logstore
    project: 188077086902****-cn-hangzhou-logproject
    
    [2021-09-01T14:15:19.025] [INFO ] [FC-DEPLOY] - Using vpcConfig: auto: fc will try to generate related vpc resources automatically
    [2021-09-01T14:15:19.504] [INFO ] [VPC] - Getting vpc: fc-deploy-component-generated-vpc-cn-hangzhou
    [2021-09-01T14:15:19.605] [INFO ] [VPC] - Getting vswitch: fc-deploy-component-generated-vswitch-cn-hangzhou
    [2021-09-01T14:15:19.696] [INFO ] [VPC] - Getting securityGroup: fc-deploy-component-generated-securityGroup-cn-hangzhou
    [2021-09-01T14:15:19.770] [INFO ] [FC-DEPLOY] - Generated vpcConfig:
    securityGroupId: sg-bp117ue6hnozayz6****
    vSwitchId: vsw-bp167nedaxg0avapv****
    vpcId: vpc-bp1j70bzw9k4snkgq****
    
    [2021-09-01T14:15:19.771] [INFO ] [FC-DEPLOY] - Using nasConfig: auto: fc will try to generate related nas file system automatically
    [2021-09-01T14:15:30.414] [INFO ] [FC-DEPLOY] - Generated nasConfig:
    groupId: 10003
    mountPoints:
      - fcDir: /mnt/auto
        nasDir: /web-springboot
        serverAddr: example.com
    userId: 10003
    
    [2021-09-01T14:15:30.418] [INFO ] [FC-DEPLOY] - Checking Function springboot exists
    [2021-09-01T14:15:30.462] [WARN ] [FC-DEPLOY] - Image registry.cn-hangzhou.aliyuncs.com/web-framework/java11:0.0.1 dose not exist locally.
    Maybe you need to run 's build' first if it dose not exist remotely.
    [2021-09-01T14:15:30.466] [INFO ] [FC-DEPLOY] - Checking Trigger web-springboot exists
    [2021-09-01T14:15:30.506] [INFO ] [FC-DEPLOY] - Checking Trigger web-springboot exists
    [2021-09-01T14:15:30.571] [INFO ] [FC-DEPLOY] - Creating service: web-springboot
    [2021-09-01T14:15:30.571] [INFO ] [FC-DEPLOY] - Creating function: springboot
    [2021-09-01T14:15:30.571] [INFO ] [FC-DEPLOY] - Creating triggers: ["web-springboot"]
      Make service web-springboot success.
      Make function web-springboot/springboot success.
      Make trigger web-springboot/springboot/web-springboot success.
    [2021-09-01T14:15:32.366] [INFO ] [FC-DEPLOY] - Checking Service web-springboot exists
    [2021-09-01T14:15:32.408] [INFO ] [FC-DEPLOY] - Checking Function springboot exists
    [2021-09-01T14:15:32.463] [INFO ] [FC-DEPLOY] - Checking Trigger web-springboot exists
    [2021-09-01T14:15:32.571] [INFO ] [FC-DEPLOY] - Creating custom domain: springboot.web-springboot.188077086902****.cn-hangzhou.fc.devsapp.net
      devsapp_fc-domain.zip file decompression completed
    [2021-09-01T14:15:33.223] [INFO ] [FC-DOMAIN] - Creating custom domain: springboot.web-springboot.188077086902****.cn-hangzhou.fc.devsapp.net
    
    There is auto config in the service: web-springboot
    [2021-09-01T14:15:33.943] [INFO ] [WEB-FRAMEWORK] - nas component get src is: ./code/target/demo-0.0.1-SNAPSHOT.jar
     Start uploading file: /mnt/0af104930d-ljj61-cn-hangzhou/web-springboot/springboot
      File uploaded successfully: /mnt/0af104930d-ljj61-cn-hangzhou/web-springboot/springboot
      upload done
      Try container acceleration
    springboot:
      region: cn-hangzhou
      serviceName: web-springboot
      functionName: springboot
      customDomains:
        - >-
          http://springboot.web-springboot.188077086902****.cn-hangzhou.fc.devsapp.net

执行结果

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

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