英文:
How to run multiple Go lang http Servers at the same time and test them using command line?
问题
编辑:我的目标是同时运行多个Go HTTP服务器。在使用Nginx反向代理时,我在访问运行在多个端口上的Go HTTP服务器时遇到了一些问题。
最后,这是我用来运行多个服务器的代码。
package main
import (
"net/http"
"fmt"
"log"
)
func main() {
// 在控制台上显示应用程序已启动
log.Println("Server started on: http://localhost:9000")
main_server := http.NewServeMux()
// 创建子域名
server1 := http.NewServeMux()
server1.HandleFunc("/", server1func)
server2 := http.NewServeMux()
server2.HandleFunc("/", server2func)
// 运行第一个服务器
go func() {
log.Println("Server started on: http://localhost:9001")
http.ListenAndServe("localhost:9001", server1)
}()
// 运行第二个服务器
go func() {
log.Println("Server started on: http://localhost:9002")
http.ListenAndServe("localhost:9002", server2)
}()
// 运行主服务器
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用于主机而不是网址。请改用 wget http://localhost:9000。感谢其他人纠正了这个错误。
- 在运行应用程序的服务器上结束SSH会话 -- 一旦关闭会话,应用程序也会关闭。
- 使用Ctrl + Z -- 如果你使用单个终端窗口并使用Ctrl + Z,它会暂停程序,你将在访问服务器时遇到问题。
我希望这对像我一样的Go语言新手程序员有所帮助。
英文:
EDIT: My aim was to run multiple Go HTTP Servers at the same time. I was facing some issues while accessing the Go HTTP server running on multiple ports while using Nginx reverse proxy.
Finally, this is the code that I used to run multiple servers.
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")
}
Few newbie mistakes I was making:
- ping http://localhost:9000 -- As mentioned, ping is used for host not a web address. Use wget http://localhost:9000 instead. Thanks for others for correcting it.
- Ending SSH session while running the application on server -- Once you close your session, it will also shut down the application.
- Use of Ctrl + Z -- If you are using single terminal window and you will use Ctrl + Z, it will pause the program and you will face issues while accessing the servers
I hope it will help newbie Go lang programmers like me.
答案1
得分: 5
经典的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 netcat
或sudo apt-get install netcat
来安装它(对于基于RPM和DEB的发行版分别适用)。
英文:
The classic ping does not work for testing TCP ports, just hosts (see https://serverfault.com/questions/309357/ping-a-specific-port). I've seen many frameworks provide a "ping" option to test if the server is alive, may be this is the source of the mistake.
I like to use 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!
You may have to install it with sudo yum install netcat
or sudo apt-get install netcat
(respectively for RPM and DEB based distros).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论