|
| 1 | +# gRPC |
| 2 | +We have already seen how GoFr can help ease the development of HTTP servers, but there are |
| 3 | +cases where performance is primiraliy required sacrificing flexibility. In these types of |
| 4 | +scenarios gRPC protocol comes into picture. gRPC is an open-source RPC(Remote Procedure Call) |
| 5 | +framework initially developed by Google. [Learn more](https://grpc.io/docs/what-is-grpc/introduction/) |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | +- Install the `protoc` protocol buffer compilation |
| 9 | + - Linux, using `apt` or `apt-get` |
| 10 | + ```shell |
| 11 | + $ apt install -y protobuf-compiler |
| 12 | + $ protoc --version # Ensure compiler version is 3+ |
| 13 | + ``` |
| 14 | + - MacOS, using homebrew |
| 15 | + ```shell |
| 16 | + $ brew install protobuf |
| 17 | + $ protoc --version # Ensure compiler version is 3+ |
| 18 | + ``` |
| 19 | +- Install **Go Puligins** for protocol compiler: |
| 20 | + 1. Install prtocol compiler plugins for Go |
| 21 | + ```shell |
| 22 | + $ go install google.golang.org/protobuf/cmd/[email protected] |
| 23 | + $ go install google.golang.org/grpc/cmd/[email protected] |
| 24 | + ``` |
| 25 | + 2. Update `PATH` for `protoc` compiler to find the plugins: |
| 26 | + ```shell |
| 27 | + $ export PATH="$PATH:$(go env GOPATH)/bin" |
| 28 | + ``` |
| 29 | + |
| 30 | +## Creating protocol buffers |
| 31 | +For a detailed guide, please take a look at the [Tutorial](https://grpc.io/docs/languages/go/basics/) at official gRPC docs. |
| 32 | + |
| 33 | +Fistly, we need to create a `customer.proto` file to define our service and the rpc methods that the service provides. |
| 34 | +```protobuf |
| 35 | +// Indicates the protocol buffer version that is being used |
| 36 | +syntax = "proto3"; |
| 37 | +// Indicates the go package where the generated file will be produced |
| 38 | +option go_package = ""; |
| 39 | +
|
| 40 | +service CustomerService { |
| 41 | + // ... |
| 42 | +} |
| 43 | +``` |
| 44 | +Inside the service one can define all the `rpc` methods, specifying the request and responses types. |
| 45 | +```protobuf |
| 46 | +service CustomerService { |
| 47 | + // GetCustomer is a rpc method to get customer data using specific filters |
| 48 | + rpc GetCustomer(CustomerFilter) returns(CustomerData) {} |
| 49 | +} |
| 50 | +``` |
| 51 | +The `CustomerFilter` and `CustomerData` are two types of messages that will be exchanged between server |
| 52 | +and client. Users must define those for protocol buffer to serialize them when making a remote procedure call. |
| 53 | +```protobuf |
| 54 | +syntax = "proto3"; |
| 55 | +
|
| 56 | +message CustomerFilter { |
| 57 | + int64 id = 1; |
| 58 | + string name = 2; |
| 59 | + // other fields that can be passed |
| 60 | +} |
| 61 | +
|
| 62 | +message CustomerData { |
| 63 | + int64 id = 1; |
| 64 | + string name = 2; |
| 65 | + string address = 3; |
| 66 | + // other customer releated fields |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +Now run the following command to gegnerate go code using the Go gRPC plugins: |
| 71 | +```shell |
| 72 | +protoc \ |
| 73 | +--go_out=. \ |
| 74 | +--go_opt=paths=source_relative \ |
| 75 | +--go-grpc_out=. \ |
| 76 | +--go-grpc_opt=paths=source_relative \ |
| 77 | +customer.proto |
| 78 | +``` |
| 79 | +Above command will generate two files `customer.pb.go` and `customer_grpc.pb.go` and these contain necessary code to perform rpc calls. |
| 80 | +In `customer.pb.go` you can find `CustomerService` interface- |
| 81 | +```go |
| 82 | +// CustomerServiceServer is the server API for CustomerService service. |
| 83 | +type CustomerServiceServer interface { |
| 84 | + GetCustomer(context.Context, *CustomerFilter) (*CustomerData, error) |
| 85 | +} |
| 86 | +``` |
| 87 | +User needs to implement this interface to serve the content to the client calling the method. |
| 88 | +```go |
| 89 | +package customer |
| 90 | +
|
| 91 | +import ( |
| 92 | + "context" |
| 93 | +) |
| 94 | +
|
| 95 | +type Handler struct { |
| 96 | + // required fields to get the customer data |
| 97 | +} |
| 98 | +
|
| 99 | +func (h *Handler) GetCustomer(ctx context.Context, filter *CustomerFilter) (*CustomerData, error) { |
| 100 | + // get the customer data and handler error |
| 101 | + return data, nil |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +Lastly to register the gRPC service to the gofr server, user can call the `RegisterCustomerServiceServer` in `customer_grpc.pb.go` |
| 106 | +to register the service giving gofr app and the Handler struct. |
| 107 | +```go |
| 108 | +package main |
| 109 | +
|
| 110 | +import ( |
| 111 | + "gofr.dev/pkg/gofr" |
| 112 | + "gofr.dev/examples/grpc-server/customer" |
| 113 | +) |
| 114 | +
|
| 115 | +func main() { |
| 116 | + app := gofr.New() |
| 117 | +
|
| 118 | + customer.RegisterCustomerServiceServer(app, customer.Handler{}) |
| 119 | +
|
| 120 | + app.Run() |
| 121 | +} |
| 122 | +``` |
| 123 | +>Note: By default, grpc server will run on port 9000, to customize the port users can set GRPC_PORT config in the .env |
0 commit comments