Go Gorilla mux 使用 内置 pprof 监控

Go About 708 words

现象

如果使用了gorilla/mux处理HTTP请求,则访问Go内置的pprof端口将显示404

解决方法

方法一

r := mux.NewRouter()
r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/{cmd}", pprof.Index)
http.Handle("/", r)

方法二

再启动一个协程,监听另外一个端口。

pprofSrv := &http.Server{
    Addr:         "localhost:6060",
    WriteTimeout: time.Second * 15,
    ReadTimeout:  time.Second * 15,
    IdleTimeout:  time.Second * 60,
}

go func() {
    log.Println(pprofSrv.ListenAndServe())
}()

//my serve

srv := &http.Server{
    Handler:      r,
    Addr:         "0.0.0.0:8000",
    WriteTimeout: time.Second * 15,
    ReadTimeout:  time.Second * 15,
    IdleTimeout:  time.Second * 60,
}

go func() {
    log.Println(srv.ListenAndServe())
}()
Views: 732 · Posted: 2023-10-12

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh