Register Your Server

Distributed Services with Go — by Travis Jeffery (35 / 84)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Implement a gRPC Server | TOC | Test a gRPC Server and Client 👉

We implemented the server writing nothing gRPC-specific yet. There are just three steps left to get our service working with gRPC, and happily we only need to perform two of them: creating a gRPC server and registering our service with it. The final step is giving the server a listener to accept incoming connections from, but we’ll require our users to pass their own listener implementation, as they might like to when testing. Once these three steps are complete, the gRPC server will listen on the network, handle requests, call our server, and respond to the client with the result.

Above your grpcServer struct in server.go, add the following NewGRPCServer function to provide your users a way to instantiate your service, create a gRPC server, and register your service to that server (this will give the user a server that just needs a listener for it to accept incoming connections):

ServeRequestsWithgRPC/internal/server/server.go

​ ​func​ NewGRPCServer(config *Config) (*grpc.Server, ​error​) {
​ gsrv := grpc.NewServer()
​ srv, err := newgrpcServer(config)
​ ​if​ err != nil {
​ ​return​ nil, err
​ }
​ api.RegisterLogServer(gsrv, srv)
​ ​return​ gsrv, nil
​ }

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.