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

背景信息

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

步骤一:准备环境

您无需安装Docker,仅安装Funcraft即可,最简单的方式即直接下载可执行的二进制文件。

  1. 安装Funcraft
  2. 执行fun --version检查安装是否成功。
  3. 配置Funcraft

步骤二:迁移应用

示例一

  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

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

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