热升级

What is a hot upgrade? If you know about nginx, you know that it supports hot upgrades, where you can use old processes to serve previously linked links and new processes to serve new links, i.e., you can upgrade the system and modify the operating parameters without stopping the service.

The main idea of Beego comes from: http://grisha.org/blog/2014/06/03/graceful-restart-in-golang/

  1. import(
  2. "log"
  3. "net/http"
  4. "os"
  5. "strconv"
  6. "github.com/beego/beego/v2/server/web/grace"
  7. )
  8. func handler(w http.ResponseWriter, r *http.Request) {
  9. w.Write([]byte("WORLD!"))
  10. w.Write([]byte("ospid:" + strconv.Itoa(os.Getpid())))
  11. }
  12. func main() {
  13. mux := http.NewServeMux()
  14. mux.HandleFunc("/hello", handler)
  15. err := grace.ListenAndServe("localhost:8080", mux)
  16. if err != nil {
  17. log.Println(err)
  18. }
  19. log.Println("Server on 8080 stopped")
  20. os.Exit(0)
  21. }

Open two terminals:

One input: ps -ef|grep 应用名

One input: curl "http://127.0.0.1:8080/hello"

And then

kill -HUP ${procces ID}

Open other terminal, input: curl "http://127.0.0.1:8080/hello?sleep=0"