解决beego中同时开启http和https时,https端口占用问题


在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监听地址有一定几率互相覆盖导致, 所以加一个睡眠时间, 让两个协程执行时间错开.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM