gRPC
Example for fiber as a client to gRPC server.
A sample program to showcase fiber as a client to a gRPC server.
Prerequisites
- Go 1.27 or higher
- Go modules
Setup
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/grpcInstall dependencies:
go mod tidy
Running the Application
Run the gRPC server:
go run server/main.goRun the Fiber client:
go run client/main.goThe Fiber client listens on
http://localhost:3000and calls the gRPC server onlocalhost:4040.
Endpoints
| Method | URL | Return value |
|---|---|---|
| GET | /add/:a/:b | a + b |
| GET | /mult/:a/:b | a * b |
Output
-> curl http://localhost:3000/add/33445/443234
{"result":"476679"}
-> curl http://localhost:3000/mult/33445/443234
{"result":"14823961130"}Regenerating Proto Files
If you modify proto/service.proto, regenerate the Go bindings. You need protoc and the two Go plugins:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latestThen run this in the grpc directory:
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
proto/service.protoThis writes proto/service.pb.go (the messages) and proto/service_grpc.pb.go (the client and server code). Keep these two details in mind when you change the proto file or the server:
- The
go_packageoption inservice.protohas to be a full import path followed by the package name (github.com/gofiber/recipes/fiber-grpc/proto;proto).protoc-gen-gorejects a short path such asproto;servicewithThe import path must contain at least one period ('.') or forward slash ('/') character. protoc-gen-go-grpcrequires the server to embedproto.UnimplementedAddServiceServer, asserver/main.godoes. Without it the server does not compile (missing method mustEmbedUnimplementedAddServiceServer).
Additional Information
gRPC (gRPC Remote Procedure Calls) is a high-performance, open-source universal RPC framework initially developed by Google. It uses HTTP/2 for transport, Protocol Buffers as the interface description language, and provides features such as authentication, load balancing, and more.
For more information, visit the official gRPC documentation.