gRPC
gRPC* is a modern open source high performance Remote Procedure Call framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.
Iris and gRPC integration lives inside the mvc package.
Have you ever have difficulties converting your app or parts of it from HTTP to gGRPC or did you ever wish you had decent HTTP framework support as well for your gRPC services? Now, with Iris you have the best of two worlds. Without change a bit of your existing gRPC services code, you can register them as Iris HTTP routes through a Controller (your service struct value).
_assets/grpc-compatible-question.png

Step by step

We will follow the official helloworld gRPC example. If you had already work with gRPC services you can skip 1-5.
1. Let's write our proto schema for request and response.
1
syntax = "proto3";
2
3
package helloworld;
4
5
// The greeting service definition.
6
service Greeter {
7
// Sends a greeting
8
rpc SayHello (HelloRequest) returns (HelloReply) {}
9
}
10
11
// The request message containing the user's name.
12
message HelloRequest {
13
string name = 1;
14
}
15
16
// The response message containing the greetings
17
message HelloReply {
18
string message = 1;
19
}
Copied!
2. Install the protoc Go plugin
1
$ go get -u github.com/golang/protobuf/protoc-gen-go
Copied!
3. Generate Go file from the helloworld.proto file above
1
$ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
Copied!
4. Implement a gRPC service as usually, with or without Iris
1
import (
2
// [...]
3
pb "myapp/helloworld"
4
"context"
5
)
Copied!
1
type Greeter struct { }
2
3
// SayHello implements helloworld.GreeterServer.
4
func (c *Greeter) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
5
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
6
}
Copied!
Iris automatically binds the standard "context" context.Context to iris.Context.Request().Context() and any other structure that is not mapping to a registered dependency as a payload (depending on the request), e.g XML, YAML, Query, Form, JSON, Protobuf.
5. Register your service to the gRPC server
1
import (
2
// [...]
3
pb "myapp/helloworld"
4
"google.golang.org/grpc"
5
)
Copied!
1
grpcServer := grpc.NewServer()
2
3
myService := &Greeter{}
4
pb.RegisterGreeterServer(grpcServer, myService)
Copied!
6. Register this myService to Iris
The mvc.New(party).Handle(ctrl, mvc.GRPC{...}) option allows to register gRPC services per-party (without the requirement of a full wrapper) and optionally strict access to gRPC-only clients.
Register MVC application controller for gRPC services. You can bind as many mvc gRpc services in the same Party or app, as the ServiceName differs.
1
import (
2
// [...]
3
"github.com/kataras/iris/v12"
4
"github.com/kataras/iris/v12/mvc"
5
)
Copied!
1
app := iris.New()
2
3
rootApp := mvc.New(app)
4
rootApp.Handle(myService, mvc.GRPC{
5
Server: grpcServer, // Required.
6
ServiceName: "helloworld.Greeter", // Required.
7
Strict: false,
8
})
Copied!
7. Generate TLS Keys
The Iris server should ran under TLS (it's a gRPC requirement).
1
$ openssl genrsa -out server.key 2048
2
$ openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
Copied!
8. Listen and Serve
1
app.Run(iris.TLS(":443", "server.crt", "server.key"))
Copied!
POST: https://localhost:443/helloworld.Greeter/SayHello with request data: {"name": "John"} xpected output: {"message": "Hello John"}.
Both HTTP Client and gRPC client will be able to communicate with our Iris+gRPC service.

Exercise files

Full Server, Clients and Testing Code can be found at: https://github.com/kataras/iris/tree/master/_examples/mvc/grpc-compatible.
Last modified 1yr ago
Export as PDF
Copy link