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

前提条件

您已完成以下操作:
注意

本文介绍的如何使用Funcraft迁移传统框架的相关内容,后期将不再维护,建议您使用Serverless Devs迁移相关框架到函数计算

关于如何将函数计算的相关资源从Funcraft迁移到Serverless Devs进行管理的详细操作,请参见从Funcraft迁移到Serverless Devs

关于如何使用Serverless Devs迁移Web框架的详细操作,请参见使用Serverless Devs迁移Web框架

由此带来的不便,敬请谅解!

背景信息

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

操作步骤

  1. 创建一个Spring Boot项目。详细信息,请参见Spring Quickstart Guide
  2. 执行以下命令进入刚创建的示例项目或您已有的项目。
    cd <project-name>
  3. 执行以下命令运行本地项目。
    • macOS、Linux平台运行项目。
      ./mvnw spring-boot:run
    • Windows平台运行项目。
      mvnw spring-boot:run
  4. 在项目的根目录下执行mvn package命令打包。
    编译输出结果与以下示例类似。
    mvn package
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] ----------------------< com.example:Spring-Boot >-----------------------
    [INFO] Building Spring-Boot 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ Spring-Boot ---
    ... ... ...
    [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) @ Spring-Boot ---
    [INFO] Building jar: /Users/txd123/Desktop/Spring-Boot/target/Spring-Boot-0.0.1-SNAPSHOT.jar
    [INFO] 
    [INFO] --- spring-boot-maven-plugin:2.2.6.RELEASE:repackage (repackage) @ Spring-Boot ---
    [INFO] Replacing main artifact with repackaged archive
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  38.850 s
    [INFO] Finished at: 2020-03-31T15:09:34+08:00
    [INFO] ------------------------------------------------------------------------
  5. 执行fun deploy -y命令将项目部署至函数计算。
    fun deploy -y
    current folder is not a fun project.
    Generating template.yml...
    Generate Fun project successfully!
    ========= Fun will use 'fun deploy' to deploy your application to Function Compute! =========
    using region: cn-qingdao
    using accountId: ***********3743
    using accessKeyId: ***********Ptgk
    using timeout: 60
    
    Collecting your services information, in order to caculate devlopment changes...
    
    Resources Changes(Beta version! Only FC resources changes will be displayed):
    
                   trigger httpTrigger deploy success
           function Spring-Boot deploy success
    service Spring-Boot deploy success
    
    Detect 'DomainName:Auto' of custom domain 'Domain'
    Request a new temporary domain ...
    The assigned temporary domain is 15639196-XXX.test.functioncompute.com,expired at 2020-04-10 15:19:56, limited by 1000 per day.
    Waiting for custom domain Domain to be deployed...
    custom domain Domain deploy success

    部署成功后,您可以在日志中看到函数计算为您生成的临时域名,通过这个临时域名您可直接访问刚部署的应用。

    说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名