golang中的net/rpc包使用概述(小结)
作者:andyidea 时间:2024-05-29 22:05:54
RPC,即 Remote Procedure Call(远程过程调用),说得通俗一点就是:调用远程计算机上的服务,就像调用本地服务一样。
我的项目是采用基于Restful的微服务架构,随着微服务之间的沟通越来越频繁,消耗的系统资源越来越多,于是乎就希望可以改成用rpc来做内部的通讯,对外依然用Restful。于是就想到了golang标准库的rpc包和google的grpc。
这篇文章重点了解一下golang的rpc包。
介绍
golang的rpc支持三个级别的RPC:TCP、HTTP、JSONRPC。但Go的RPC包是独一无二的RPC,它和传统的RPC系统不同,它只支持Go开发的服务器与客户端之间的交互,因为在内部,它们采用了Gob来编码。
Go RPC的函数只有符合下面的条件才能被远程访问,不然会被忽略,详细的要求如下:
函数必须是导出的(首字母大写)
必须有两个导出类型的参数,
第一个参数是接收的参数,第二个参数是返回给客- 户端的参数,第二个参数必须是指针类型的
函数还要有一个返回值error
举个例子,正确的RPC函数格式如下:
func (t *T) MethodName(argType T1, replyType *T2) error
T、T1和T2类型必须能被encoding/gob包编解码。
示例
举一个http的例子。
下面是http服务器端的代码:
package main
import (
"errors"
"net"
"net/rpc"
"log"
"net/http"
)
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
func main() {
arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":1234")
if e != nil {
log.Fatal("listen error:", e)
}
http.Serve(l, nil)
}
简单分析一下上面的例子,先实例化了一个Arith对象arith,然后给arith注册了rpc服务,然后把rpc挂载到http服务上面,当http服务打开的时候我们就可以通过rpc客户端来调用arith中符合rpc标准的的方法了。
请看客户端的代码:
package main
import (
"net/rpc"
"log"
"fmt"
)
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
func main() {
client, err := rpc.DialHTTP("tcp", "127.0.0.1:1234")
if err != nil {
log.Fatal("dialing:", err)
}
// Synchronous call
args := &Args{7,8}
var reply int
err = client.Call("Arith.Multiply", args, &reply)
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)
// Asynchronous call
quotient := new(Quotient)
divCall := client.Go("Arith.Divide", args, quotient, nil)
replyCall := <-divCall.Done // will be equal to divCall
if replyCall.Error != nil {
log.Fatal("arith error:", replyCall.Error)
}
fmt.Printf("Arith: %d/%d=%d...%d", args.A, args.B, quotient.Quo, quotient.Rem)
// check errors, print, etc.
}
简单说明下,先用rpc的DialHTTP方法连接服务器端,调用服务器端的函数就要使用Call方法了,Call方法的参数和返回值已经很清晰的表述出rpc整体的调用逻辑了。
我们把服务器端跑起来,再把客户端跑起来,这时候客户端会输出:
Arith: 7*8=56
Arith: 7/8=0...7
到此,整个rpc的调用逻辑就完成了。
来源:https://segmentfault.com/a/1190000011891865
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用sklearn进行对数据标准化、归一化以及将数据还原的方法
![](https://img.aspxhome.com/file/2023/3/112933_0s.jpg)
django使用haystack调用Elasticsearch实现索引搜索
![](https://img.aspxhome.com/file/2023/2/113152_0s.png)
vue实现引入本地json的方法分析
从多个tfrecord文件中无限读取文件的例子
Javascript中的函数声明与函数表达式(奇技淫巧)
使用pandas把某一列的字符值转换为数字的实例
使用Python中的reduce()函数求积的实例
![](https://img.aspxhome.com/file/2023/3/121003_0s.jpg)
Node.js API详解之 Error模块用法实例分析
Python中list列表添加元素的3种方法总结
sqlserver数据库导入数据操作详解(图)
![](https://img.aspxhome.com/file/2023/8/113588_0s.gif)
解决django接口无法通过ip进行访问的问题
![](https://img.aspxhome.com/file/2023/6/62266_0s.jpg)
python中numpy包使用教程之数组和相关操作详解
不能使用“;文件已在使用中 Microsoft JET Database Engine
Django中外键ForeignKey介绍使用
jquery和css3中的选择器nth-child使用方法和用途示例
在ASP中按指定参数格式化显示时间的函数。
使用python实现UDP通信方式
![](https://img.aspxhome.com/file/2023/7/125157_0s.jpg)
Python如何调用JS文件中的函数
![](https://img.aspxhome.com/file/2023/0/67850_0s.png)
解决django后台样式丢失,css资源加载失败的问题
![](https://img.aspxhome.com/file/2023/3/118723_0s.jpg)