http - Golang RPC http.Serve 与 rpc.ServeConn(HTTP 与原始连接)

标签 http networking tcp go rpc

Go net/rpc 库 documentation允许通过网络公开对象,无论是通过原始网络连接还是通过 HTTP。

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)

原始 TCP 网络连接

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)
    } 
}

要调用第一种类型的服务器,可以使用 rpc.DialHTTP("tcp", "127.0.0.1:1234") 和第二种类型 rpc.Dial("tcp", "127.0.0.1:1234") 将被使用。

我的问题是这两者有什么不同?运行 HTTP 服务器与“原始网络连接”服务器有什么优缺点?可以通过 curl 或浏览器以某种方式使用 HTTP 执行 RPC 吗? HTTP 版本对跨语言 RPC 调用有用吗?

最佳答案

这个问题的答案很好地描述了 HTTP 和原始 TCP 之间的一般差异(不直接涉及 Go)。

TCP Vs. Http Benchmark

它基本上说,由于 HTTP 是 TCP 之上的标准化层,如果您计划让您的代码有一个网页尝试发出请求并处理响应,那么您可能应该使用它,但如果您只关心关于速度,关闭 HTTP 并使用原始 TCP。

关于http - Golang RPC http.Serve 与 rpc.ServeConn(HTTP 与原始连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19640005/

相关文章:

networking - 在内核崩溃转储中确定源代码中的确切行

ios - 如何在 iOS 设备上获取关联接入点的 IP 地址

java - 服务器关闭客户端套接字然后客户端在同一端口上打开新套接字时出现 JVM_Bind 错误

c# - SSL 还是自己的实现?

tcp - 如何通过 tcp 协议(protocol)强制使用 vlc 流式传输 RTP 视频?

python - python 与 MySQL 接口(interface)的最佳方式

networking - 从 "write"系统调用到 I/O 寄存器编程的 tcp 数据的数据路径(旅行)

javascript - 不使用 CORS 访问后端 API

iphone - 通过 xcode 的多部分 HTTP 请求

PHP 和 Node.js 还是只有 Node.js?