我可以使用Go设置一个Web应用程序的多个端口吗?

huangapple go评论75阅读模式
英文:

Can I setup multi port from one web app with Go?

问题

据我所知,我可以使用Golang的http包来运行简单的Web服务器,例如:

http.ListenAndServe(PORT, nil)

其中,PORT是要监听的TCP地址。

我可以将PORT用作PORTS,例如从一个应用程序中使用http.ListenAndServe(":80, :8080", nil)吗?

可能我的问题很愚蠢,但是“不问则不知!”

英文:

As I know, I can run simple web server with Golang just use http package, like

http.ListenAndServe(PORT, nil)

where PORT is TCP address to listen.

Can I use PORT as PORTS, for example http.ListenAndServe(":80, :8080", nil) from one application?

Possible my question is stupid, but "Who don't ask, He will not get answer!"

答案1

得分: 23

不,你不能。

但是你可以在不同的端口上启动多个监听器。

go http.ListenAndServe(PORT, handlerA)
http.ListenAndServe(PORT, handlerB)
英文:

No, you cannot.

You can however start multiple listeners on different ports

go http.ListenAndServe(PORT, handlerA)
http.ListenAndServe(PORT, handlerB)

答案2

得分: 16

这是一个简单的工作示例:

package main

import (
	"fmt"
	"net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "你好")
}

func world(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "世界")
}

func main() {
	serverMuxA := http.NewServeMux()
	serverMuxA.HandleFunc("/hello", hello)

	serverMuxB := http.NewServeMux()
	serverMuxB.HandleFunc("/world", world)

	go func() {
		http.ListenAndServe("localhost:8081", serverMuxA)
	}()

	http.ListenAndServe("localhost:8082", serverMuxB)
}

希望对你有帮助!

英文:

Here is a simple working Example:

package main

import (
	"fmt"
	"net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello")
}

func world(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "world")
}

func main() {
	serverMuxA := http.NewServeMux()
	serverMuxA.HandleFunc("/hello", hello)

	serverMuxB := http.NewServeMux()
	serverMuxB.HandleFunc("/world", world)

	go func() {
		http.ListenAndServe("localhost:8081", serverMuxA)
	}()

	http.ListenAndServe("localhost:8082", serverMuxB)
}

答案3

得分: 1

你将需要不同的*http.Server实例,每个实例都单独配置(根据你的选择,可以是相同的或不同的)处理程序(或路由器)来提供它们的端点。

我们有一种更好的方法来组织并发的http.Server实例,同时仍然支持在SIGINT(从shell发送到操作系统,然后从操作系统发送到程序,当终端用户按下ctrl+c时)之后进行优雅的关闭。

func main() {
    var serverA = &http.Server{}
    var serverB = &http.Server{}

    // ... 根据需要进行配置:添加端点

    go serverA.ListenAndServe()
    go serverB.ListenAndServe()

    // 创建一个通道来订阅ctrl+c/SIGINT事件
    sigInterruptChannel := make(chan os.Signal, 1)
    signal.Notify(sigInterruptChannel, os.Interrupt)
    // 阻塞执行,直到收到SIGINT信号
    <-sigInterruptChannel
    
    // 创建一个在4秒宽限期后过期的上下文
    ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
    defer cancel()
    
    // 请求关闭两个服务器
    go serverA.Shutdown(ctx)
    go serverB.Shutdown(ctx)

    // 等待直到ctx结束(将在4秒后发生)
    <-ctx.Done()
}

另请参阅:

英文:

You'll need different instances of *http.Server, each configured separately (for same or different as your choice) handlers (or routers) to serve their endpoints.

We have a better method to organize concurrent http.Server instances while still supporting graceful shutdown after SIGINT which is the signal that sent from shell to O.S. and O.S. to program when terminal user presses ctrl+c.

func main() {
    var serverA = &amp;http.Server{}
    var serverB = &amp;http.Server{}

    // ... configure them as you wish: add endpoints

    go serverA.ListenAndServe()
    go serverB.ListenAndServe()

    // create a channel to subscribe ctrl+c/SIGINT event
    sigInterruptChannel := make(chan os.Signal, 1)
    signal.Notify(sigInterruptChannel, os.Interrupt)
    // block execution from continuing further until SIGINT comes
    &lt;-sigInterruptChannel
    
    // create a context which will expire after 4 seconds of grace period
    ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
    defer cancel()
    
    // ask to shutdown for both servers
    go serverA.Shutdown(ctx)
    go serverB.Shutdown(ctx)

    // wait until ctx ends (which will happen after 4 seconds)
    &lt;-ctx.Done()
}

See also:

huangapple
  • 本文由 发表于 2015年2月27日 02:01:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/28749579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定