本文以官方提供的入门的demo helloworld(https://github.com/grpc/grpc-go/tree/master/examples/helloworld/helloworld)为例,通过源码讲解实际的grpc调用执行的流程(UnaryRPC),为了方便理解只列出核心代码。该篇主要关注Server端源代码流程。
RegisterGreeterServer实际将helloworld.proto 中定义的Greeter_Service进行注册,所谓的服务注册。
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
var Greeter_ServiceDesc = grpc.ServiceDesc{
ServiceName: "helloworld.Greeter",
HandlerType: (*GreeterServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "SayHello",
Handler: _Greeter_SayHello_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "examples/helloworld/helloworld/helloworld.proto",
}
func RegisterGreeterServer(s grpc.ServiceRegistrar, srv GreeterServer) {
s.RegisterService(&Greeter_ServiceDesc, srv)
}
func (s *Server) register(sd *ServiceDesc, ss interface{}) {
info := &serviceInfo{
serviceImpl: ss,
methods: make(map[string]*MethodDesc),
streams: make(map[string]*StreamDesc),
mdata: sd.Metadata,
}
for i := range sd.Methods {
d := &sd.Methods[i]
info.methods[d.MethodName] = d
}
s.services[sd.ServiceName] = info
}
Serve方法通过lis net.Listener监听连接,每次起一新的goroutine来处理 新的Conn,每一个新的连接都是在独立的线程中进行处理并发执行。
func (s *Server) Serve(lis net.Listener) error {
for {
rawConn, err := lis.Accept()
go func() {
s.handleRawConn(lis.Addr().String(), rawConn)
}()
}
}
建立一个HTTP2的Transport,用其进行Transport层的rpc调用执行
func (s *Server) handleRawConn(lisAddr string, rawConn net.Conn) {
st := s.newHTTP2Transport(conn, authInfo)
go func() {
s.serveStreams(st)
}()
}
func (s *Server) serveStreams(st transport.ServerTransport) {
s.handleStream(st, stream, s.traceInfo(st, stream))
}
通过stream.Method()得到需要调用FullMethod(/helloworld.Greeter/SayHello)从注册的services列表中找到对应serviceInfo对象,进而找到MethodDesc,然后执行processUnaryRPC
func (s *Server) handleStream(t transport.ServerTransport, stream *transport.Stream, trInfo *traceInfo) {
srv, knownService := s.services[service]
if knownService {
if md, ok := srv.methods[method]; ok {
s.processUnaryRPC(t, stream, srv, md, trInfo)
return
}
}
}
func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.Stream, info *serviceInfo, md *MethodDesc, trInfo *traceInfo) (err error) {
// 实际执行定义的rpc方法google.golang.org/grpc/examples/helloworld/helloworld._Greeter_SayHello_Handler(如下)
reply, appErr := md.Handler(info.serviceImpl, ctx, df, s.opts.unaryInt)
//将rpc方法的值返回给客户端
s.sendResponse(t, stream, reply, cp, opts, comp)
}
google.golang.org/grpc/examples/helloworld/helloworld._Greeter_SayHello_Handler
func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(HelloRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GreeterServer).SayHello(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/helloworld.Greeter/SayHello",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GreeterServer).SayHello(ctx, req.(*HelloRequest))
}
return interceptor(ctx, in, info, handler)
}
GRPC源码实例解析(二)
https://www.jianshu.com/p/944e3eb59dd8