1、golang基于http协议实现的RPC
2、http长连接(此处不考虑http2.0)
二者的性能比较
8.1 Persistent Connections
8.1.1 Purpose
...
Persistent HTTP connections have a number of advantages:
...
- HTTP requests and responses can be pipelined on a connection.
Pipelining allows a client to make multiple requests without
waiting for each response, allowing a single TCP connection to
be used much more efficiently, with much lower elapsed time.
...
虽然Http1.1协议规定客户端可以在长连接中,无需等待服务器响应,复用连接通道连续发送多个请求以提高效率
8.1.2.2 Pipelining
A client that supports persistent connections MAY "pipeline" its
requests (i.e., send multiple requests without waiting for each
response). A server MUST send its responses to those requests in the
same order that the requests were received.
从pipeling的要求来看, 服务端必须按照request的顺序返回response , 这将大大的影响服务器处理请求的效率
猜测golang的net/rpc只是复用了tcp连接通道,而没有完全遵循http1.1协议。按双向发送数据实现,从测试性能上看,比纯粹的http长连接高出一倍左右就合理了。
测试数据如下:
单机基于ab测试100000请求, 发送数据都很小,可以忽略json和protobuf解码效率的差异
类型 | 并发 | QPS |
---|---|---|
http长连接 | 100 | 6474.33 |
http长连接 | 200 | 6624.35 |
http长连接 | 300 | 6737.52 |
rpc基于http | 100 | 10009.45 |
rpc基于http | 200 | 12451.40 |
rpc基于http | 300 | 13362.31 |
rpc基于jsonrpc | 100 | 10077.23 |
rpc基于jsonrpc | 200 | 12271.09 |
rpc基于jsonrpc | 300 | 13182.97 |
从QPS上比较, 基于http和基于json的rpc的性能没有太大差别, 都比http长连接高一倍左右
golang代码实现,参考:https://studygolang.com/articles/14336