This is a sample gRPC server implementation in Go, demonstrating basic gRPC service methods.
The simple-grpc application starts a gRPC server on port 50051. It provides a DemoService with several RPC methods to showcase different interaction patterns.
The server implements the following methods:
- Health: Returns a simple boolean status.
- Hello: Responds with a greeting message.
- Echo: Returns the same message it received.
- Add: Takes two integers and returns their sum.
- GetTime: Returns the current server time in UTC.
- GetResource: Simulates fetching a resource by ID.
- Go 1.23 or higher
- Protobuf compiler (
protoc) installed (if you want to regenerate files)
-
Clone the repository.
-
Navigate to the
simple-grpcdirectory:cd simple-grpc -
Download dependencies:
go mod tidy
Start the server using go run:
go run main.goYou should see:
2024/02/17 14:18:41 gRPC server listening on [::]:50051
You can use grpcurl to interact with the server. Ensure grpcurl is installed.
grpcurl -plaintext -proto pb/demo.proto localhost:50051 demo.DemoService/Healthgrpcurl -plaintext -proto pb/demo.proto localhost:50051 demo.DemoService/Hellogrpcurl -plaintext -proto pb/demo.proto -d '{"msg": "Greetings"}' localhost:50051 demo.DemoService/Echogrpcurl -plaintext -proto pb/demo.proto -d '{}' localhost:50051 demo.DemoService/Echogrpcurl -plaintext -proto pb/demo.proto -d '{"a": 50, "b": 25}' localhost:50051 demo.DemoService/Addgrpcurl -plaintext -proto pb/demo.proto -d '{"id": "user-123"}' localhost:50051 demo.DemoService/GetResourcemain.go: The main entry point and server implementation.pb/: Contains the generated gRPC code and.protodefinition.go.mod/go.sum: Go module dependency files.