我有一个结构性问题:我不知道如何使用我现在构建它的方式(我可以找到可以看到它与其他模型一起工作的解决方案)。
我正在使用标准的 Net/Http,并使用以下代码启动我的服务器:
gv := GlobalVars{
jobs: make(chan QueueElement),
appConfig: appConfig,
}
go worker(&gv)
server := http.Server{
Handler: &gv,
Addr: ":" + appConfig.Port,
}
log.Fatal(server.ListenAndServe())
然后我有一个处理程序来检查一个案例中的所有路由:
func (gv *GlobalVars) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
应用程序正在启动一些将作业传递到队列的 api。
我的问题是,我需要启动其中的 3 个(不同的队列,不同的配置)但相同的 globalvar 结构。
但是这个只有一个我可以设置的处理程序——我如何添加多个处理程序(我不想要多个服务器,必须在同一个端口上运行)可以理解它寻址不同的 globalvars 变量?
server := http.Server{
Handler: &gv,
Addr: ":" + appConfig.Port,
}
最佳答案
看看 net.http.ServeMux输入:
ServeMux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL.
ServeMux 本身就是一个http.Handler
并通过请求路由多路复用到不同的子处理程序。
根据我对您问题的理解,您希望在同一台服务器上有不同的处理程序,并且每个处理程序使用不同的配置处理不同的队列。
使用 ServeMux 可以轻松实现:
gv1 := GlobalVars{
jobs: make(chan QueueElement),
appConfig: appConfig1,
}
gv2 := GlobalVars{
jobs: make(chan QueueElement),
appConfig: appConfig2,
}
gv3 := GlobalVars{
jobs: make(chan QueueElement),
appConfig: appConfig3,
}
sm := http.NewServeMux()
// Let gv{1,2,3} handle routes{1,2,3} respectively
sm.Handle("/route1", &gv1)
sm.Handle("/route2", &gv2)
sm.Handle("/route3", &gv3)
// Register the ServeMux as the sole Handler. It will delegate to the subhandlers.
server := http.Server{
Handler: sm,
Addr: ":" + globalAppConfig.Port,
}
请注意,您不必自己构建http.Server
。如果只需要一台服务器,可以使用http包级函数http.ListenAndServe和 http.Handle负责为您创建服务器和默认 ServeMux。
// same GlobalVars as above
// ...
// Instead of creating a ServeMux we can use the global DefaultServeMux
http.Handle("/route1", &gv1)
http.Handle("/route2", &gv2)
http.Handle("/route3", &gv3)
// Calling the package level ListenAndServe uses the single global server.
// Passing nil as the Handler uses the DefaultServeMux as Handler on which we registered the Handlers above.
log.Fatal(http.ListenAndServe(":" + globalAppConfig.Port, nil)
更新
标准 ServeMux
的一个小例子,有两个 Handler
服务于 3 个路由
// Keeping this type simple for the example
type GlobalVars struct {
appConfig string
}
// This method makes every GlobalVars a net.http.Handler
func (gv *GlobalVars) ServeHTTP(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "%s here. Receiving request for %s\n", gv.appConfig, req.URL.Path)
}
func main() {
gv1 := &GlobalVars{
appConfig: "gv1",
}
gv2 := &GlobalVars{
appConfig: "gv2",
}
// Handle requires a route and a Handler, our gvs are Handlers.
// gv1 handles two routes, while gv2 handles only one.
http.Handle("/route1", gv1)
http.Handle("/route2", gv1)
http.Handle("/route3", gv2)
log.Fatal(http.ListenAndServe(":8000", nil))
}
如果我依次调用所有三个路由,我将收到以下响应:
$ curl localhost:8000/route1
gv1 here. Receiving request for /route1
$ curl localhost:8000/route2
gv1 here. Receiving request for /route2
$ curl localhost:8000/route3
gv2 here. Receiving request for /route3
这展示了如何使用有状态变量作为处理程序(例如 GlobalVars 类型的变量)。
注意:处理程序方法ServeHTTP
有一个GlobalVars
的指针接收器。这意味着该方法可能会更改 GlobalVars
变量。 HTTP 处理程序是并发执行的(想象一下在很短的时间内对同一个处理程序发出多个请求,您会希望尽可能保持响应)。这个例子只读取了 appConfig 的值,所以没问题。然而,一旦对变量/字段的写入进入混合状态,您将需要适当的同步。
关于go - Http.Server {} - 多个处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56513174/