GRPC
GRPC
- Define a Service using Protocol Buffers
- Generate Client and Server Stubs in a variety of languages
- HTTP/2 brings capabilities like bidirectional streaming
INTERFACE DEFINITION LANGUAGE (IDL)
message HelloRequest {
string greeting = 1;
}
message HelloResponse {
string reply = 1;
}
service HelloService {
rpc SayHello(HelloRequest) returns (HelloResponse);
}
STREAMING
message Demo {
string Value = 1;
}
service DemoService {
rpc Hello(Demo) returns (Demo);
rpc Download(Demo) returns (stream Demo);
rpc Upload(stream Demo) returns (Demo);
rpc BestOfBoth(stream Demo) returns (stream Demo);
}
GO Client
func main() {
grpcAddr := "localhost:12345"
conn, err := grpc.Dial(grpcAddr)
if err != nil {
log.Fatalf("Dial(%s) = %v", grpcAddr, err)
}
client := NewDemoServiceClient(conn)
msg := &Demo{"hello world"}
reply, err := client.Hello(context.Background(), msg)
if err != nil {
log.Fatalf("UnaryCall(%#v) failed with %v", msg, err)
}
println("received message " + reply.Value)
}
what is context: blog.golang.org/context
GO SErver
type server struct{}
func (this *server) Hello(c context.Context, msg *Demo)
(*Demo, error) {
msg.Value = "Allo" + msg.Value
return msg, nil
}
func main() {
lis, err := net.Listen("tcp", "localhost:12345")
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
RegisterDemoServiceServer(grpcServer, &server{})
grpcServer.Serve(lis)
}
TCP
LENGTH DELIMITED
13025 ns/op
19130 ns/op
19885 ns/op
GRPC
11563 ns/op
23612 ns/op
21062 ns/op
PRELIMINARY SPEED
GOlang/protobuf
12460 ns/op
15972 ns/op
20603 ns/op
GOGO/protobuf
11105 ns/op
12332 ns/op
13536 ns/op
PRELIMINARY SPEED
REST HATEAOS
- Stateless
- Explorable API
- Curlable/Hackable URLs
- There is lots of technology to build on, including caching servers. No need to roll your own.
GRPC
- Stateful
- Follow the types
- I don't know yet
- Alpha
REST HATEAOS
- No standard IDL
- Streaming is a hack
- dynamic
- If GRPC did NOT exist I was about to use it.
GRPC
- IDL
- Streaming
- static
- Tools are emerging at a rapid pace
JSON/REST ON GRPC
gRPC GATEWAY
import "github.com/gengo/grpc-gateway/options/options.proto";
message Demo {
string value = 1;
}
service DemoService {
rpc Hello(Demo) returns (Demo) {
option (gengo.grpc.gateway.ApiMethodOptions.api_options) = {
path: "/v1/demo/hello"
method: "POST"
}
}
}
ProtoType GUI for GRPC
LETMEGRPC
message Allo {
string Name = 1;
int64 Age = 2;
string Country = 3;
bool Spy = 4;
}
service OnionSeller {
rpc OnlyOnce(Allo)
returns (Allo);
}
DEMO
COMPLEX STRUCTURES
DEMO
LETMETESTSERVER
cd $GOPATH/src/github.com/gogo/letmegrpc/letmetestserver/serve
LETMEGRPC -PORT=8080 -ADDR=localhost:12345 Serve.proto
DEMO
Integrated
DEMO
CUstomization
CONCLUSION
- Easy
- Fast
- Cross Language
- API
- Alpha
- Tools are being rapidly developed,
- Because its easy
GRPC
By Walter Schulze
GRPC
- 1,470