如何做到Zero Downtime重啟Go服務?

來源:互聯網
上載者:User

標籤:函數   string   str   int   style   root   pst   curl   head   

graceful的實踐

 

使用endless庫來實現,比如接入gin:

r := gin.Default()r.GET("/", index)endless.ListenAndServe(":3000", r)

我們編寫一個帶指定時間逾時的處理函數:

func index(c *gin.Context) {    duration := c.Query("duration")    durationInt, _ := strconv.Atoi(duration)    time.Sleep(time.Duration(durationInt) * time.Second)    c.String(http.StatusOK, duration)}
curl http://localhost:3000/?duration=10

測試時使用CTRL+C時候,會處理完所有請求才會退出;如果是後台運行,當我們擷取進程的pid後,如果使用kill -9 $PID, endless無法catch信這個訊號,需要使用kill -s TERM $PID

利用nginx upstream負載

nignx的簡單配置:

upstream graceful{    server 127.0.0.1:8888;    server 127.0.0.1:8889;}server {    listen 8001;    server_name localhost;    location ^~ /{        proxy_set_header X-Real-IP  $remote_addr;        proxy_set_header Host $host;        proxy_set_header http_referer $http_referer;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        proxy_pass http://graceful/;        proxy_redirect default;    }    error_page   500 502 503 504  /50x.html;    location = /50x.html {        root   html;    }   location ~ /\..+ {        deny all;   }}

作一個較為簡單的負載平衡,在更新程式的時候順序更新,當其實一個服務不再提供時,負載導到另外一台,順序更新後,可滿足服務熱更新的需求,當然最好使用專門的部署指令碼來實現。

 

如何做到Zero Downtime重啟Go服務?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.