A basic CRUD service, to explore and explain gRPC.
ideas.proto contains the definition of service
service CoolIdeas {
rpc SubmitIdea(Idea) returns (IdeaResponse) {}
rpc GetIdeas(User) returns (Ideas) {}
}
Service struct in service.go satisifies the above interface and can be registerd and run as gRPC Service.
ideas.RegisterCoolIdeasServer(server, &ideas.Service{}) // file: server/main.go
Client makes request to the server, by creating a client and calling GetIdeas or SubmitIdea
ideas.NewCoolIdeasClient(conn).GetIdeas(ctx, user) // file: client/client.go
Instead of using JSON to send payload to request, gRPC uses protocol buffers (protobuf), its smaller and performant.
message Idea {
User user_id = 1;
string title = 2;
string description = 3;
}
- You should 've go installed, check with
go version protoc --versionto verify protoc installation, along withprotoc-gen-goor install it by
brew install protobuf
go get -u github.com/golang/protobuf/protoc-gen-go
cmd/serverdir has server code,cmd/clienthas the client code.- Generate the ideas.pb.proto which contains the service definition and protobuf, by running
protoc -I . ./ideas.proto --go_out=plugins=grpc:$GOSRC
go build && ./serverin server dir to run servergo build && ./clientin client dir to run client
you can optionally pass the port flag while running server/client to listen/dial on that port