標籤:函數 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服務?