zoukankan      html  css  js  c++  java
  • go学习笔记 beego 的部署【windows 和docker】

    windows下部署

    在windows下和linux 下是一样的,windows 用bee pack -beGOOS=window 而linux 用 bee pack -beGOOS=linux -be GOARCH=amd64 ,通过bee创建的项目 默认是开发模式, 所以部署前需要修改 为runmode = prod, 这里我增加一个 配置 website="demo" ,在default.go controller里面使用 c.Data["Website"] = beego.AppConfig.String("website") , 目的是部署后我们一般都会修改配置文件,验证是否读取到正确的配置。

    执行命令 bee pack -beGOOS=window 后会生成 对应的exe文件,我们现在创建一个 文件夹 test 来作为发布包, 里面必须包含exe文件, 需要把conf,static和views也拷贝到发布文件夹里面如,

    我们修改app.conf 然后启动exe, 启动后验证正常【读取到了 新的配置文件】

    可以利用nssm工具将exe打包成windows服务,然后启动服务即可。

    如果出现找不到views目录的异常错误。这时需要在main.go入口文件,对配置文件、views目录等静态文件进行指定【我目前没有遇到过】

    package main
    
    import (
        _ "demo/routers"
        "fmt"
        "os"
        "os/exec"
        "path/filepath"
    
        "github.com/astaxie/beego"
    )
    
    func main() {
    
        initConfig()
        beego.Run()
    
    }
    
    func initConfig() {
        rootPath := GetAPPRootPath()
        fmt.Println("应用调用路径:" + rootPath)
        beego.SetViewsPath(rootPath + "/views")
        beego.LoadAppConfig("ini", rootPath+"/conf/app.conf")
        beego.SetStaticPath("static", rootPath+"/static")
    }
    
    func GetAPPRootPath() string {
        file, err := exec.LookPath(os.Args[0])
        if err != nil {
            return ""
        }
    
        p, err := filepath.Abs(file)
        if err != nil {
            return ""
        }
    
        return filepath.Dir(p)
    }

    Docker

    1在项目根目录下创建Dockerfile文件, 如下:

    FROM golang:1.15.6
    
    //正常这2句不需要,上网   逼得
    RUN go env -w GO111MODULE=on
    RUN go env -w GOPROXY=https://goproxy.cn,direct
    # Install beego and the bee dev tool*
    RUN go get github.com/astaxie/beego && go get github.com/beego/bee
    
    # Expose the application on port 8080
    EXPOSE 8080
    
    # Set the entry point of the container to the bee command that runs the
    # application and watches for changes
    CMD ["bee", "run"]

    2.构建镜像:

    docker build -t demo-image .  //-t 是指tag的意思 构建了一个叫做demo-image的镜像

    3..检查镜像并启动

    docker run -it --name demo-instance -p 8080:8080  -v /root/go/demo:/go/src/demo -w /go/src/demo demo-image
    /*
    --name:容器名字
    --network:指定网络
    --rm:容器停止自动删除容器
     
    -i:--interactive,交互式启动
    -t:--tty,分配终端
    -v:--volume,挂在数据卷
    -d:--detach,后台运行
    -e, --env list             设置env
    -u, --user string        指定用户 (格式: <name|uid>[:<group|gid>])
    -w, --workdir string       指定工作目录
    我的gopath 是 /root/go/
    */

    修改 配置文件, 重新启动实例,浏览器访问

     

  • 相关阅读:
    【2021-06-14】何太第一次喝断片
    【2021-06-13】管理的目标也就是把事情做好
    判断字符串的两半是否相等
    生成每种字符都是奇数个的字符串
    叶子相似的树
    面试准备
    北京旅行计划
    M笔试
    连续字符
    字符串中第一个唯一字符
  • 原文地址:https://www.cnblogs.com/majiang/p/14219903.html
Copyright © 2011-2022 走看看