小编Adi*_*Adi的帖子

如何同时运行多个Go lang 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 …
Run Code Online (Sandbox Code Playgroud)

linux ubuntu go

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

go ×1

linux ×1

ubuntu ×1