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

背景信息

Beego是一个快速开发Go应用的HTTP框架,可以用来快速开发API、Web及后端服务等各种应用,是一个RESTful的框架,主要设计灵感来源于Tornado、Sinatra和Flask这三个框架,但是结合了Go本身的一些特性Interface、Struct嵌入等。

步骤一:准备环境

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

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

步骤二:迁移应用

  1. 执行以下命令安装Beego,详情请参见官方示例
    go get github.com/astaxie/beego                    
  2. 执行以下命令创建hello.go项目。
    package main
    import "github.com/astaxie/beego"
    func main(){
        beego.Run()
    }                    
  3. 执行以下命令运行本地项目。
    go run hello.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 hello.go
    Fun will replace your addr to '0.0.0.0:9000', and also backup your origin file hello.go to hello.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 compile 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'
    Fun will reuse the temporary domain 15014775-XXX.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文件,最后进行自动部署。

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

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