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

前提条件

您已完成以下操作:
注意

本文介绍的如何使用Funcraft迁移传统框架的相关内容,后期将不再维护,建议您使用Serverless Devs迁移相关框架到函数计算。关于如何将函数计算的相关资源从Funcraft迁移到Serverless Devs进行管理的详细操作,请参见从Funcraft迁移到Serverless Devs

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

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

背景信息

Gin是一个Golang的微框架,封装优雅,API友好,源码注释明确。具有快速灵活,容错方便等特点。

操作步骤

示例一

  1. 在已安装1.11及以上版本的Golang的环境中安装Gin。详细信息,请参见官方示例
    go get -u github.com/gin-gonic/gin                   
  2. 创建一个example.go项目,并写入以下代码。
    package main
    import "github.com/gin-gonic/gin"
    func main() {
    r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }                      
  3. 执行以下命令运行本地项目。
    go run example.go                    
  4. 执行fun deploy -y命令将项目部署至函数计算。
    fun deploy -y
    返回结果如下。
    current folder is not a fun project.
    Fun detected your application doesn't listen on '0.0.0.0:9000' in example.go
    Fun will replace your addr to '0.0.0.0:9000', and also backup your origin file example.go to example.go.bak
    ? Are your sure? Yes
    Could not find any bin files from current folder.
    Before using 'fun deploy', you must use 'GOARCH=amd64 GOOS=linux go build -ldflags "-s -w"' to comile your project.
    ? Let Fun exec this command for you? Yes
    Executing command 'GOARCH=amd64 GOOS=linux go build -ldflags "-s -w"'...
    Tips: 
    You must use 'GOARCH=amd64 GOOS=linux go build -ldflags "-s -w"' to recompile your project every time before using fun deploy.
    Generating template.yml...
    Generate Fun project successfully!
    ========= Fun will use 'fun deploy' to deploy your application to Function Compute! =========
    ...  ...   ....
                    trigger httpTrigger deploy success
            function express deploy success
    service express deploy success
    Detect 'DomainName:Auto' of custom domain 'Domain'
    Request a new temporary domain ...
    The assigned temporary domain is 15014775-1986***.test.functioncompute.com,expired at 2020-04-03 09:52:55, limited by 1000 per day.
    Waiting for custom domain Domain to be deployed...
    custom domain Domain deploy success                        
    函数计算要求启动服务必须监听0.0.0.0:9000端口,详细信息,请参见简介。您可以在部署日志中看到,Funcraft会尝试去检测应用的启动端口。如果端口不匹配,按下回车后Funcraft会帮您修改,然后自动检测构建生成的可执行程序。如果检测不到可执行程序,则会提示您使用指定命令进行编译,您按下回车后Funcraft会帮您编译,编译完成后,会自动生成Funcraft所需要的bootstrap文件以及template.yml文件,最后进行自动部署。

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

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

示例二

  1. 执行以下命令将示例克隆到本地。详细信息,请参见官方示例
    git clone https://github.com/tanhe123/mdblog.git                        
  2. 修改配置文件。
    1. config目录下,将config.example.toml文件名称修改为config.toml
    2. 打开config.toml文件,修改配置。
      • port = 8091修改为port = 9000,表示应用在9000端口启动。
      • debug = true修改为debug = false,表示使用生产版本。
      • dir = "logs"修改为dir = "/tmp",表示日志写到/tmp目录,函数计算在不挂载NAS的情况下,只有该目录有读写权限。
  3. 执行以下命令编译项目。
    go build                   
  4. 执行以下命令运行本地项目。
    ./mdblog                    
  5. 执行fun deploy -y命令将项目部署至函数计算。
    fun deploy -y

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

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