分享一个简单的 rpc 服务框架
一、服务端实现
package mainimport ("log""net""net/rpc"
)const HelloServiceName = "main.HelloService"type HelloServiceInterface interface {Hello(request string, replay *string) error
}func RegisterHelloService(svc HelloServiceInterface) error {//todo 其中 rpc.Register 函数调用会将对象类型中所有满足 RPC 规则的对象方法注册为 RPC 函数,所有注册的方法会放在 “HelloService” 服务空间之下。return rpc.RegisterName(HelloServiceName, svc)
}type HelloService struct{}//todo RPC方法需要满足的规则:只能有两个可序列化的参数,第二个参数表reply是指针类型,函数返回error类型,函数需要大写进行公开func (p *HelloService) Hello(request string, reply *string) error {*reply = "hello:" + requestreturn nil
}func main() {//rpc.RegisterName("HelloService", new(HelloService))RegisterHelloService(new(HelloService))//todo 然后我们建立一个唯一的 TCP 连接,并且通过 rpc.ServeConn 函数在该 TCP 连接上为对方提供 RPC 服务。listener, err := net.Listen("tcp", ":1234")if err != nil {log.Fatal("ListenTCP error:", err)}for {conn, err := listener.Accept()if err != nil {log.Fatal("Accept error:", err)}//todo 启动协程去处理每个tcp请求go rpc.ServeConn(conn)}
}
二、客户端实现
package mainimport ("fmt""log""net/rpc"
)const HelloServiceName = "main.HelloService"type HelloServiceInterface interface {Hello(request string, replay *string) error
}type HelloServiceClient struct {*rpc.Client
}// todo 在编译阶段检查 HelloServiceClient 类型是否实现了 HelloServiceInterface 接口。
// TODO 这里将 nil 转换为 *HelloServiceClient 类型并赋值给 _(匿名变量),编译器会自动检查 HelloServiceClient 是否实现了接口 HelloServiceInterface 中声明的所有方法。
// TODO 如果 HelloServiceClient 没有实现接口中的所有方法,这段代码会导致编译失败,从而在编码阶段就能发现潜在的问题。
var _ HelloServiceInterface = (*HelloServiceClient)(nil)func DialHelloService(network, address string) (*HelloServiceClient, error) {c, err := rpc.Dial(network, address)if err != nil {return nil, err}return &HelloServiceClient{Client: c}, nil
}func (p *HelloServiceClient) Hello(request string, reply *string) error {return p.Client.Call(HelloServiceName+".Hello", request, reply)
}func main() {client, err := DialHelloService("tcp", "localhost:1234")if err != nil {log.Fatal("dialing:", err)}var reply stringerr = client.Hello("world", &reply)if err != nil {log.Fatal(err)}fmt.Println(reply)
}