迁移Beego到函数计算

更新时间: 2024-02-05 15:31:07

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

前提条件

您已完成以下操作:

重要

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

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

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

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

背景信息

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

操作步骤

  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文件,最后进行自动部署。

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

    说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见配置自定义域名
阿里云首页 函数计算 相关技术圈