grpc: beyond rest
TRANSCRIPT
gRPC: Beyond RESThistory repeats itself infinitely
About me
• JVM developer since 1998
• Cofounder @ CirculoSiete
• Staff @ JVM Mexico City
• @domix
History
• Lupine. Powered Courier by XEROX (1981)
• Bruce Jay Nelson coined the term ‘RPC’
• SUN’s RPC (ONC RPC) ~NFS
• CORBA (1991)
• RMI
• Burlap/Hessian
gRPC?
• Stands for ‘gRPC Remote Procedure Call’
• Open source project from Google hosted by the Cloud Native Computing Foundation
• High Performance, general purpose standard base, RPC framework.
• Open sourced of ‘Stubby RPC’ at Google
Why gRPC?• We already have REST|GraphQL, why?
• Maybe performance?
• https://goo.gl/yM7XPM
• https://goo.gl/oYY8dr
• https://goo.gl/TtzXad
• Contract first approach
• Multi platform
Performance
• HTTP/2
• Single TCP connection
• Binary framing layer
• Header compression
• Persistent connections
• http://www.http2demo.io
Contract first approach
Protocol Buffers
• Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data, smaller, faster, and simpler than XML or JSON.
• You can update your data structure without breaking deployed programs.
• Language/platform agnostic.
• You have to write a definition in a text file ‘.proto’.
• A compiler is needed to generate the desired source code.
syntax = "proto3";
package com.circulosiete.cursos.k8s;
import "google/protobuf/timestamp.proto"; import "google/protobuf/wrappers.proto"; import "google/api/annotations.proto";
option java_multiple_files = true; option go_package = "warehouse";
message PageProductResponse { google.protobuf.BoolValue first = 1; //unique numbered tag: 1 google.protobuf.BoolValue last = 2; //unique numbered tag: 2 google.protobuf.Int32Value number = 3; //unique numbered tag: 3 google.protobuf.Int32Value numberOfElements = 4; //unique numbered tag: 4 google.protobuf.Int32Value size = 5; //unique numbered tag: 5 google.protobuf.Int64Value totalElements = 6; //unique numbered tag: 6 google.protobuf.Int32Value totalPages = 7; //unique numbered tag: 7 repeated ProductModel content = 8; }
message ProductModel { int64 id = 1; string name = 2; string description = 3; string price = 4; google.protobuf.Timestamp createdAt = 5; google.protobuf.Timestamp modifiedDate = 6; int64 version = 7; }
Contract!service ProductService { rpc Create (ProductRequest) returns (ProductResponse); rpc Read (EntityId) returns (ProductResponse); rpc Update (ProductModel) returns (ProductResponse); rpc Delete (EntityId) returns (ProductResponse); rpc Paged (Page) returns (PageProductResponse); rpc List (Page) returns (stream ProductResponse); }
service ValidacionService { rpc ValidacionCreate (CreateRequest) returns (ValidacionResponse); rpc ValidacionDelete (EntityId) returns (ValidacionResponse); }
Multiplatform
gRPC compiler
• Takes proto files then generates specific source files
• It supports several plugins, for different programming languages
• It generates Server files, client files.
JVM
• Netty Powered!
• Gradle and Maven support
• Spring Boot community support
Live Coding!gRPC intro demo
What about REST?
REST and gRPC
• REST is still necessary
• JavaScript applications
• Web Browser
• We can reuse our gRPC endpoints in REST
• https://github.com/grpc-ecosystem/grpc-gateway
gRPC gateway
• For now only supports Go Lang
• We have to use some Google Annotations
• The good
• Fast as hell!
• The Ugly
• Sometimes the Go Lang dev env is difficult to setup
Live Coding!gRPC complete demo
Photos
• https://unsplash.com/photos/_cb5MjNG3DI
• https://unsplash.com/photos/1qkyck-UL3g
• https://unsplash.com/photos/w7ZyuGYNpRQ
• https://unsplash.com/photos/YucYLQ6Dm8A