Mithril gRPC Example
This example demonstrates gRPC support in the Mithril framework.
Prerequisites
Install the Protocol Buffer compiler and Go plugins:
# Install protoc
# macOS
brew install protobuf
# Linux
apt-get install -y protobuf-compiler
# Install Go plugins
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
Generate Code from Proto Files
cd example-grpc
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
proto/hello.proto
Running the Example
# Start the gRPC server
go run example-grpc/main.go
The server will start on localhost:50051.
Testing with grpcurl
Install grpcurl:
# macOS
brew install grpcurl
# Linux
go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
List services:
grpcurl -plaintext localhost:50051 list
Call SayHello:
grpcurl -plaintext -d '{"name": "Mithril"}' localhost:50051 hello.HelloService/SayHello
Call SayHelloStream:
grpcurl -plaintext -d '{"name": "Mithril"}' localhost:50051 hello.HelloService/SayHelloStream
Features Demonstrated
- gRPC server setup with Mithril
- Unary RPC (SayHello)
- Server streaming RPC (SayHelloStream)
- Middleware/Interceptors:
- Logging
- Recovery (panic handling)
- Authentication
- Service reflection for grpcurl
- Configuration from environment
Production Usage
For production deployments:
- TLS/SSL: Enable TLS for secure communication
- Load Balancing: Use gRPC load balancing strategies
- Monitoring: Integrate with Prometheus for metrics
- Tracing: Add OpenTelemetry for distributed tracing
- Health Checks: Implement gRPC health checking protocol
- Authentication: Use JWT or mTLS for authentication
Next Steps
- Add more complex service definitions
- Implement bidirectional streaming
- Add custom metadata handling
- Integrate with database
- Add rate limiting
- Implement circuit breakers