gRPC
- Basic Usage
Code Source: https://github.com/gogf/examples/tree/main/grpc/basic
Description
This example demonstrates the basic usage of gRPC
in GoFrame
applications. It shows how to:
- Create
gRPC
servers and clients - Define and use protocol buffers
- Implement service handlers
- Make
RPC
calls
Structure
.
├── client/ # Client example
│ └── client.go # Client implementation
├── controller/ # Service controllers
│ └── hello.go # Hello service implementation
├── protobuf/ # Protocol buffer definitions
├── server/ # Server example
│ ├── config.yaml # Server configuration
│ └── server.go # Server implementation
├── go.mod # Go module file
└── go.sum # Go module checksums
Features
The example showcases the following features:
-
Service Implementation
- Protocol buffer definitions
- Service handlers
- Request/response handling
- Error management
-
Client Usage
- Connection management
RPC
calls- Error handling
- Context usage
-
Protocol Support
gRPC
services- Protocol buffer messages
- Custom metadata
- Stream processing
Requirements
Prerequisites
- Protocol buffer compiler installed:
# For macOS
brew install protobuf
# Install protoc-gen-go and protoc-gen-go-grpc
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
Usage
-
Generate protocol buffer code:
cd protobuf
protoc --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. *.proto -
Start the server:
cd server
go run server.go -
Run the client:
cd client
go run client.go
Implementation Details
The example demonstrates:
- Service definition using
gRPC
and protocol buffers - Client/server implementation with
GoFrame
integration