2019-go-10

beego 的模块化设计-grace

grace 模块是 beego 新增的一个独立支持热重启的模块。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

import(
"log"
"net/http"
"os"
"strconv"

"github.com/astaxie/beego/grace"
)

func handler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("WORLD!"))
w.Write([]byte("ospid:" + strconv.Itoa(os.Getpid())))
}

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/hello", handler)

err := grace.ListenAndServe("localhost:8080", mux)
if err != nil {
log.Println(err)
}
log.Println("Server on 8080 stopped")
os.Exit(0)
}

打开两个终端

一个终端输入:ps -ef|grep 应用名

一个终端输入请求:curl “http://127.0.0.1:8080/hello

热升级

kill -HUP 进程 ID

打开一个终端输入请求:curl “http://127.0.0.1:8080/hello?sleep=0