I am trying to understand the notions of Listener, Server and Services in the context of gRPC, Protobuf.
Let's use example on https://grpc.io/docs/languages/go/basics/ as a reference. Here we have
- Listener: lis
- gRPC Server: grpcServer := grpc.NewServer()
- Service: RouteGuide service
It appears we can also have more than one service registered to the same server.
That is, besides RouteGuide Service, we can have say SomeOther Service, SomeOtherOther Service.
And we can register all the three and expect the server to be able to serve methods belong to this three services (RouteGuide, SomeOther, SomeOtherOther).
Let's say RouteGuide, SomeOther, SomeOtherOther each have their own proto files specific to them. And all of the protos are in the same namespace (package
value).
grpcServer := grpc.NewServer(opts...)
newRouteGuideService := pb.NewRouteGuideServer()
pb.RegisterRouteGuideServer(grpcServer, newRouteGuideService)
someOtherService := pb.NewSomeOtherServer()
pb.RegisterSomeOtherServer(grpcServer, someOtherService)
someOtherOtherService := pb.NewSomeOtherOtherServer()
pb.RegisterSomeOtherOtherService(grpcServer, someOtherOtherService)
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", 80))
grpcServer.Serve(lis)
It appears the term Server is, so to speak, overloaded here. Not only grpcServer, but also RouteGuide, SomeOther, and SomeOtherOther are also referred to as Servers.
I am trying to figure out how to model or understand the notions.
Can we say, the server gRPCServer
through listener lis
listens on port 80 and can serve the three services RouteGuide, SomeOther, SomeOtherOther which are registered with it (gRPCServer
)?
Is having one server service multiple services error prone?
What are the caveats to keep in mind when using one server with multiple services?
The listener listens for connections on a given port, and when a client connects, it creates a new goroutine to handle that particular session. The listener continues listening on the port for new incoming connections while the goroutine handles that particular client connection.
The session simply listens for the requests from the connected client and dispatches them to one of the registered services. The incoming request includes the name of the service, the function to call, and the arguments to that function. When the service handles the request, the response is sent back to the client.
So, as you say, the server listens on a port and can serve all the registered services through that port. This is not error-prone, and not that different from having one registered service defining all those functions.