在beego的app.go文件中, 找到
// run normal mode if BConfig.Listen.EnableHTTPS { go func() { time.Sleep(1000 * time.Microsecond) //這里是我修改后的代碼, 原本只是睡眠了 20 * Microsecond,我這里改到了1000, 可以繼續往大改 if BConfig.Listen.HTTPSPort != 0 { app.Server.Addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort) } else if BConfig.Listen.EnableHTTP { BeeLogger.Info("Start https server error, confict with http.Please reset https port") return } logs.Info("https server Running on https://%s", app.Server.Addr) if err := app.Server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil { logs.Critical("ListenAndServeTLS: ", err) time.Sleep(100 * time.Microsecond) endRunning <- true } }() }
問題原因分析: 初步判斷為http監聽地址和https監聽地址有一定幾率互相覆蓋導致, 所以加一個睡眠時間, 讓兩個協程執行時間錯開.