duanjiao3686 2013-10-28 16:23
浏览 590

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

The Go net/rpc library documentation enables exposing an object across a network, either via raw network connections or via HTTP.

HTTP Example

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)

Raw TCP Network Connection

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

To call the first type of server, one would use rpc.DialHTTP("tcp", "127.0.0.1:1234") and for the second type rpc.Dial("tcp", "127.0.0.1:1234") would be used.

My question is how are these two really different? What pros/cons are there to running an HTTP server vs. a "raw network connection" server? Can one perform RPCs via curl or the browser in some way with HTTP? Is the HTTP version useful for cross-language RPC invocation?

  • 写回答

1条回答 默认 最新

  • duanqiongdu9916 2013-10-28 16:43
    关注

    This question has an answer with a good description about the differences between HTTP and raw TCP in general (not directly concerning Go).

    TCP Vs. Http Benchmark

    It basically says that since HTTP is a layer on top of TCP for standardization, it is something you should probably use if you plan on having your code having a webpage trying to make requests and processing the response, but if all you care about is speed, leave the HTTP off and go with raw TCP.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘