This project demonstrates how to build a gRPC service using Go-Kit in Golang. The example consists of a simple Greeter service that responds with a greeting message when a request is made.
- Implements gRPC with Go-Kit
- Follows Clean Architecture (Service, Endpoint, Transport layers)
- Supports gRPC client-server communication
- Easily extendable for additional functionalities
- Golang (Go 1.18+)
- gRPC
- Go-Kit
- Protocol Buffers (proto3)
.
├── client
│ ├── main.go # gRPC client implementation
├── server
│ ├── main.go # gRPC server entry point
├── service
│ ├── service.go # Business logic for Greeter service
├── endpoint
│ ├── endpoint.go # Defines Go-Kit endpoints
├── transport
│ ├── transport.go # gRPC transport layer for Go-Kit
├── proto
│ ├── helloworld.proto # Protocol Buffer file
└── README.md
Ensure you have the following installed:
- Go 1.18+
- Protocol Buffers Compiler (protoc)
- gRPC and Go-Kit dependencies:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest go get github.com/go-kit/kit
Run the following command to generate Go code from the .proto
file:
protoc --go_out=. --go-grpc_out=. proto/helloworld.proto
go run server/main.go
go run client/main.go
- Server Output:
gRPC server is listening on port 50051
- Client Output:
Greeting: Hello, Go-Kit
The code follows the Go-Kit architecture with three main layers:
-
Service Layer (
service/service.go
)- Implements the actual business logic.
- Defines
GreeterService
interface and its implementation.
-
Endpoint Layer (
endpoint/endpoint.go
)- Converts incoming requests into structured endpoint requests.
- Uses
Go-Kit
endpoints to abstract the business logic from the transport layer.
-
Transport Layer (
transport/transport.go
)- Implements the gRPC server.
- Converts gRPC requests into endpoint requests and vice versa.
- Uses Go-Kit transport handlers to decouple transport logic from business logic.
-
Server Initialization (
server/main.go
)- Creates an instance of the service.
- Registers the gRPC transport layer.
- Starts listening on port
50051
.
-
Client (
client/main.go
)- Connects to the gRPC server.
- Sends a request to
SayHello
method. - Receives and prints the response from the server.
- Add middleware (logging, authentication, rate limiting)
- Integrate HTTP transport using Go-Kit
- Implement service discovery with Consul or etcd
This project is open-source and available under the MIT License.
client.SayHello(ctx, &pb.HelloRequest{Name: "Go-Kit"})
sends a gRPC request to the server with the name "Go-Kit".
- The server receives the request in the
SayHello
method intransport.go
.
ServeGRPC
callsdecodeGRPCSayHelloRequest
to decode the gRPC request into a Go-Kit struct.
- The request is forwarded to the
MakeSayHelloEndpoint
endpoint.
- The endpoint calls the
SayHello
method ofGreeterService
.
- The result from
GreeterService
is returned through the layers in reverse order. - Finally, the client receives the response and prints the greeting message.