亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何同時運行多個 Go lang http 服務器并使用命令行對其進行測試?

如何同時運行多個 Go lang http 服務器并使用命令行對其進行測試?

Go
喵喵時光機 2022-04-20 17:46:26
我的目標是同時運行多個 Go HTTP 服務器。我在使用 Nginx 反向代理時訪問在多個端口上運行的 Go HTTP 服務器時遇到了一些問題。最后,這是我用來運行多個服務器的代碼。package mainimport (    "net/http"    "fmt"    "log")func main() {    // Show on console the application stated    log.Println("Server started on: http://localhost:9000")    main_server := http.NewServeMux()    //Creating sub-domain    server1 := http.NewServeMux()    server1.HandleFunc("/", server1func)    server2 := http.NewServeMux()    server2.HandleFunc("/", server2func)    //Running First Server    go func() {        log.Println("Server started on: http://localhost:9001")        http.ListenAndServe("localhost:9001", server1)    }()    //Running Second Server    go func() {        log.Println("Server started on: http://localhost:9002")        http.ListenAndServe("localhost:9002", server2)    }()    //Running Main Server    http.ListenAndServe("localhost:9000", main_server)}func server1func(w http.ResponseWriter, r *http.Request) {    fmt.Fprintf(w, "Running First Server")}func server2func(w http.ResponseWriter, r *http.Request) {    fmt.Fprintf(w, "Running Second Server")}我犯的幾個新手錯誤:ping http://localhost:9000 -- 如前所述,ping 用于主機而不是 Web 地址。請改用wget http://localhost:9000。感謝其他人的糾正。在服務器上運行應用程序時結束 SSH 會話——關閉會話后,它也會關閉應用程序。使用 Ctrl + Z - 如果您使用單個終端窗口并且您將使用 Ctrl + Z,它將暫停程序并且您在訪問服務器時會遇到問題我希望它能幫助像我這樣的新手 Go lang 程序員。
查看完整描述

1 回答

?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

經典 ping 不適用于測試 TCP 端口,僅適用于主機(請參閱https://serverfault.com/questions/309357/ping-a-specific-port)。我見過許多框架提供了一個“ping”選項來測試服務器是否還活著,這可能是錯誤的根源。


我喜歡使用 netcat:


$ nc localhost 8090 -vvv

nc: connectx to localhost port 8090 (tcp) failed: Connection refused


$ nc localhost 8888 -vvv

found 0 associations

found 1 connections:

     1: flags=82<CONNECTED,PREFERRED>

     outif lo0

     src ::1 port 64550

     dst ::1 port 8888

     rank info not available

     TCP aux info available


Connection to localhost port 8888 [tcp/ddi-tcp-1] succeeded!

您可能必須使用sudo yum install netcator安裝它sudo apt-get install netcat(分別對于基于 RPM 和 DEB 的發行版)。


查看完整回答
反對 回復 2022-04-20
  • 1 回答
  • 0 關注
  • 417 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號