dgh*_*dgh 14 networking rpc tcp http go
Go net/rpc库文档允许通过原始网络连接或HTTP通过网络公开对象.
arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":1234")
if e != nil {
log.Fatal("listen error:", e)
}
go http.Serve(l, nil)
Run Code Online (Sandbox Code Playgroud)
arith := new(Arith)
rpc.Register(arith)
l, e := net.Listen("tcp", ":1234")
if e != nil {
log.Fatal("listen error:", e)
}
go func() {
for {
conn, err := l.Accept()
go rpc.ServeConn(conn)
}
}
Run Code Online (Sandbox Code Playgroud)
要调用第一种类型的服务器,可以使用rpc.DialHTTP("tcp","127.0.0.1:1234"),对于第二种类型,rpc.Dial("tcp","127.0.0.1:1234")将使用用过的.
我的问题是这两者有何不同?运行HTTP服务器与"原始网络连接"服务器有什么优缺点?可以通过curl或浏览器以某种方式使用HTTP执行RPC吗?HTTP版本对跨语言RPC调用有用吗?
Ver*_*ran 10
这个问题的答案很好地描述了HTTP和原始TCP之间的差异(不直接与Go有关).
它基本上说,因为HTTP是用于标准化的TCP之上的一层,所以如果您打算让您的代码有一个网页尝试发出请求并处理响应,那么您可能应该使用它,但如果您关心的只是速度,关闭HTTP并使用原始TCP.
归档时间: |
|
查看次数: |
4862 次 |
最近记录: |