Building High Performance APIs In Go Using gRPC And Protocol Buffers
-
Upload
shiju-varghese -
Category
Technology
-
view
282 -
download
2
Transcript of Building High Performance APIs In Go Using gRPC And Protocol Buffers
![Page 1: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/1.jpg)
Building High Performance APIs in Go with gRPC and
Protocol Buffers
Shiju Varghese medium.com/@shijuvar
GopherCon India 2017
![Page 2: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/2.jpg)
Agenda
• gRPC
• Protocol Buffers
• Building APIs with gRPC
![Page 3: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/3.jpg)
Monolithic Architecture
Catalog Management
Customer Accounts
Orders Management
Payment
Monolithic Database
Monolithic E-Commerce App
Shipment
eCom Store Web
![Page 4: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/4.jpg)
Moving to MicroserviceseCom Store Web
Catalog Management
Customer Accounts
Order Management
Payment
Shipment
Store DB
Catalog DB
Customer DB
Order DB
Payment DB
Shipment DB
![Page 5: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/5.jpg)
Communications on Microservices
• Inter-process communication between Microservices
• Serialisation and Deserialisation of Messages
• Scaling APIs into billions of APIs calls
• Building streaming APIs
• RESTful with JSON or RPC with Binary Encoding?
![Page 6: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/6.jpg)
Why Not REST• Uses HTTP/1.x; Separate TCP Connection per
request
• Text on the wire; Not performance efficient
• Harder API Evolution
• Not Domain-Specific, Not Strongly-Typed
• Lack of Streaming capabilities
![Page 7: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/7.jpg)
![Page 8: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/8.jpg)
• High performance, open-source RPC framework
• Open source version of Google’s internal framework Stubby
• Uses Protocol Buffers as the IDL
• HTTP/2 for transport
• Bi-Directional streaming
• RPC is Efficient, Domain-Specific and Strongly-Typed
• Works across languages and platforms
![Page 9: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/9.jpg)
• Google's language-neutral, platform-neutral, extensible mechanism for serialising structured data
• IDL - Describe once and generate interfaces for multiple languages
• Structure of the Request and Response
• Binary format for network transmission
• Supports multiple languages
Protocol Buffers
![Page 10: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/10.jpg)
Communication between gRPC Server and Client app
![Page 11: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/11.jpg)
Types of RPC Methods
• Simple RPC
• Server-side streaming RPC
• Client-side streaming RPC
• Bi-directional streaming RPC
![Page 12: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/12.jpg)
gRPC Workflow
ProtoBuf Definitions
protoc Compiler Go
Ruby
Java
gRPC Server
gRPC Client Define 1 Compile 2
Implement 3
Generate Code
protoc --go_out=plugins=grpc
![Page 13: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/13.jpg)
syntax="proto3";packageorder;
serviceOrderService{//AsimpleRPCrpcCreateOrder(Order)returns(OrderResponse){}}messageOrder{stringid=1;stringstatus=2;int64created_on=3;messageOrderItem{stringcode=1;stringname=2;floatunit_price=3;int32quantity=4;}
repeatedOrderItemorder_items=4;}messageOrderResponse{stringorder_id=1;stringerror=2;}
![Page 14: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/14.jpg)
typeserverstruct{}
func(s*server)CreateOrder(ctxcontext.Context,in*pb.Order)(*pb.OrderResponse,error){//Implementationgoeshere}funcmain(){lis,err:=net.Listen("tcp",port)iferr!=nil{log.Fatalf("failedtolisten:%v",err)}//CreatesanewgRPCservers:=grpc.NewServer()pb.RegisterOrderServiceServer(s,&server{})s.Serve(lis)}
gRPC Server
![Page 15: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/15.jpg)
gRPC Clientconn,err:=grpc.Dial(address,grpc.WithInsecure())iferr!=nil{log.Fatalf("Unabletoconnect:%v",err)}deferconn.Close()client:=pb.NewOrderServiceClient(conn)createOrders(client)
![Page 16: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/16.jpg)
funccreateOrders(clientpb.OrderServiceClient){order:=&pb.Order{Id:uuid.NewV4().String(),Status:"Created",CreatedOn:time.Now().Unix(),OrderItems:[]*pb.Order_OrderItem{&pb.Order_OrderItem{Code:"knd100",Name:"KindleVoyage",UnitPrice:220,Quantity:1,},&pb.Order_OrderItem{
Code:"kc101",Name:"KindleVoyageSmartShellCase",UnitPrice:10,Quantity:2,},},}resp,err:=client.CreateOrder(context.Background(),order)}
![Page 17: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/17.jpg)
Installation # Install Protocol Buffers compiler
$ brew install protobuf (macOS)
(Or download from https://github.com/google/protobuf/releases/tag/v3.0.0)
# Install Go Plugin
$ go get github.com/golang/protobuf/protoc
$ go get github.com/golang/protobuf/protoc-gen-go
# Install Go RPC Framework
$ go get google.golang.org/grpc
![Page 18: Building High Performance APIs In Go Using gRPC And Protocol Buffers](https://reader031.fdocuments.net/reader031/viewer/2022022205/58cee83a1a28ab333d8b56d1/html5/thumbnails/18.jpg)
medium.com/@shijuvar linkedin.com/in/shijuvar email: [email protected]
THANK YOU
Articles:Building High Performance APIs In Go Using gRPC And Protocol Buffers
Benchmarking Protocol Buffers, JSON and XML in Go