golang 熱更新技巧

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

序言

Golang標準庫的http部分提供了強大的web應用支援,再加上negroni等中介軟體架構的支援,可以開發高效能的web應用(如提供Restful的api服務等)。通常這些web應用部署在多台Linux作業系統的應用伺服器上,並用Nginx等做為反向 Proxy,實現高可用的叢集服務。當應用版本升級時,如何?比較優雅的多態伺服器的版本更新呢?

問題分析

Web應用的更新,我覺得可能需要考慮幾個方面的問題:

  1. 編譯好的應用二進位檔案、設定檔上傳到伺服器上;
  2. 應用伺服器能感知到有新的版本上傳;
  3. 在沒有停止服務的情況下,熱更新版本;
  4. 最好所有的更新過程,可以指令碼化,減少手動操作的錯誤。

方案

其實,go社區有一些開源項目,可以自動檢測web應用的改變,並實現自動的更新,但這些應用都是檢測源碼、資源檔的更新,啟動build過程,實現自動的編譯和重啟,例如 gin和 fresh,這些應用適合應用於開發與測試階段,可能並不適合應用的部署和更新,但提供了良好的思路。

部署環境的目錄及版本的上傳我將發布的應用二進位檔案和設定檔,存放在某個目錄下,如 ~/app/release,每個版本都保留在這個目錄中,例如 app.1.0、app.1.1、app.2.0,一旦發現有問題,可以及時的復原。同時,在~/app目錄下,利用軟連結檔案,指向到最新版本,如

ln -s ~/app/release/app.2.0 ~/app/app.bin

此外,利用一個儲存在 ~/app/release 下的文字檔,來指明當前應用的版本,如current.conf:

{    "bin.file": "~/app/release/app.2.0",    "cfg.file": "~/app/release/cfg.2.0"}

當需要補救伺服器的版本時,可以通過指令碼調用scp,將新版本上傳到release目錄下,然後更新current.conf檔案。監控current.conf檔案,獲知版本更新current.conf檔案中是當前的版本,一旦這個檔案發生變化,即表示有版本需要更新(或者復原),我們只需要監控這個檔案的變化,一旦發生變化,則做相應的處理。檔案的監控,可以通過 fsnotify來實現。

func watch() {    watcher, err := fsnotify.NewWatcher()    if err != nil {        logger.Fatal(err)    }    defer watcher.Close()    go func() {        for {                select {                case event := <-watcher.Events:                    logger.Println("event:", event)                    if event.Op&fsnotify.Write == fsnotify.Write {                        logger.Println("notify runner to do the ln -s and restart server.")                        restartChan <- true                    }                case err := <-watcher.Errors:                    logger.Println("error:", err)            }        }    }()    err = watcher.Add("/path/to/current.conf")    if err != nil {        logger.Fatal(err)    }    <- make(chan bool)}

重啟服務監控到current.conf檔案的變化後,接下來就是重啟服務。為了讓服務不中斷,優雅的進行重啟,可以利用 endless 來替換標準庫net/http的ListenAndServe:

 n := negroni.New()    n.Use(middleware.NewRecovery())    n.Use(middleware.NewMaintainMiddleware())    n.Use(middleware.NewLogMiddleware())    n.Use(middleware.NewStatic(http.Dir("static")))    n.UseHandler(router.NewRouter())    log.Fatal(endless.ListenAndServe(":3000", n))

在current.conf變更後,首先將~/app下的軟連結檔案指向最新版本,然後利用

kill -HUP

通知應用重啟。

func run() {    for {        <- restartChan        c, err := ioutil.ReadFile("/path/to/current.conf")        if err != nil {            logger.Println("current.conf read error:", err)            return        }        var j interface{}        err = json.Unmarshal(c, &j)        if err != nil {            logger.Println("current.conf parse error:", err)            return        }        parsed, ok := j.(map[string]interface{})        if !ok {            logger.Println("current.conf parse error: mapping errors")            return        }        exec.Command("rm", "app.bin").Run()        exec.Command("ln", "-s", parsed["bin.file"].(string), "app.bin").Run()        exec.Command("rm", "app.conf").Run()        exec.Command("ln", "-s", parsed["cfg.file"].(string), "app.cfg").Run()        if !started {            cmd := exec.Command("./app.bin", "-c", "app.cfg")            started = true        } else {            processes, _ := ps.Processes()            for _, v := range processes {                if strings.Contains(v.Executable(), parsed["bin.file"]) {                    process, _ := os.FindProcess(v.Pid())                    process.Signal(syscall.SIGHUP)                }            }        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.