Linux 如何同时运行多个Go-lang http服务器并使用命令行测试它们?

Linux 如何同时运行多个Go-lang http服务器并使用命令行测试它们?,linux,ubuntu,go,Linux,Ubuntu,Go,编辑:我的目标是同时运行多个Go HTTP服务器。在使用Nginx反向代理访问运行在多个端口上的Go-HTTP服务器时,我遇到了一些问题 最后,这是我用来运行多个服务器的代码 package main import ( "net/http" "fmt" "log" ) func main() { // Show on console the application stated log.Println("Server started on: http

编辑:我的目标是同时运行多个Go HTTP服务器。在使用Nginx反向代理访问运行在多个端口上的Go-HTTP服务器时,我遇到了一些问题

最后,这是我用来运行多个服务器的代码

package main

import (
    "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——如前所述,ping用于主机而不是web地址。改为使用wget。谢谢其他人纠正它
  • 在服务器上运行应用程序时结束SSH会话——一旦关闭会话,它也将关闭应用程序
  • 使用Ctrl+Z——如果您使用的是单终端窗口,并且您将使用Ctrl+Z,它将暂停程序,您将在访问服务器时遇到问题

  • 我希望它能帮助新手像我一样成为程序员

    经典的ping不适用于测试TCP端口,只适用于主机(请参阅)。我看到许多框架提供了一个“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!
    
    $nc localhost 8090-vvv
    nc:connectx到本地主机端口8090(tcp)失败:连接被拒绝
    $nc本地主机8888-vvv
    找到0个关联
    找到1个连接:
    1:flags=82
    outif lo0
    src::1端口64550
    dst::1端口8888
    排名信息不可用
    TCP辅助信息可用
    连接到本地主机端口8888[tcp/ddi-tcp-1]成功!
    

    您可能必须使用
    sudo-yum-install-netcat
    sudo-apt-get-install-netcat
    (分别适用于基于RPM和DEB的发行版)安装它。

    它不是主机名
    http://127.0.0.1:8090
    。这是URL。您可以执行pinglocalhost
    ping操作,在这种情况下这是非常无用的。最好试试像
    wget这样的方法http://127.0.0.1:8090
    如果可能的话,请发布文本而不是图片。不确定人们为什么投你反对票,但这对我很有帮助。正是我想要的。谢谢你,谢谢。我要试试netcat。