RPC VS HTTP go语言实现

RPC VS HTTP go语言实现

理论分析

RPCHTTP 都是基于 TCP协议的,所以两者在 网络链路层 网络层 传输层 都是一样的,唯一不同的地方就是 应用层的协议不同

两者共同的开销 : 以太网头部(14个字节) + IP头部(20个字节或更多)+ TCP头部(20个字节或更多)

HTTP协议的开销 : 主要是集中在请求头,当然了,出于效率的考虑,你可以减少携带的请求头数量. (50个字节或者更多)

POST /hello HTTP1.1
Content-type:text/html;
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36
....
Body

RPC协议的开销: 函数名,参数.几乎没有其他额外的开销.

type Call struct {
	ServiceMethod string     // The name of the service and method to call.
	Args          any        // The argument to the function (*struct).
	Reply         any        // The reply from the function (*struct).
	Error         error      // After completion, the error status.
	Done          chan *Call // Receives *Call when Go is complete.
}

可以发现 RPC的开销是更小的,但是也不多,我个人认为两者的性能差距是很小的,不过我的测试结果似乎不太相同.

设计实验

简单来说就是分别开启 HTTP服务器 和 RPC服务器,利用go语言自带的测试框架比较其性能就好了.

代码发布在GIthubGitee,欢迎pr.

HTTP服务器

func HttpServer() {
   
	// 创建一个http服务器
	server := http.Server{
   
		Addr: config.HttpServerPort,
	}
	http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
   
		request.ParseForm()
		length := request.Form.Get("length")
		atoi, _ := strconv.Atoi(length)
		writer.Write([]byte(CreateString(atoi)))
	})
	server.ListenAndServe()
}

发送HTTP请求

func HttpRequest() {
   
	url := "http://localhost" + config.HttpServerPort + "/hello?length=10"
	method := "GET"

	client := &http.Client
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值