Summary

This lecture introduces gRPC, a high-performance remote procedure call (RPC) framework. It details service and micro-services architecture, explaining how gRPC defines messages and services. The document also covers API implementation across multiple languages, and the concept of gRPC versus REST APIs.

Full Transcript

gRPC The world of services Services and micro services gRPC Google Remote Procedure Calls? General-purpose Remote Procedure Calls Opensource framework developed and used by Google 2015 Platform independent High Performance: low CPU to process, low bandwidth to transmit HTTP/2 a...

gRPC The world of services Services and micro services gRPC Google Remote Procedure Calls? General-purpose Remote Procedure Calls Opensource framework developed and used by Google 2015 Platform independent High Performance: low CPU to process, low bandwidth to transmit HTTP/2 and Protobuf gRPC Messages and services are defined using protocol buffer to (.proto file) gRPC will generate code Multiple programming languages are supported (C# /.NET, C++, Dart, Go, Java, Kotlin, Node, Objective- C, PHP, Python, Ruby) Just concentrate on implement the service logics Why gRPC? Easy to define messages and services using protobuf API definition separate from implementation Widely support multiple languages from single.proto file Payload in binary  lightweight, high performance Backward compatible (protobuf allow definition to support backward compatible, new param will not affects the old ones) Supported APIs Unary (simple, similar to REST API) Server streaming Client streaming Bi-directional streaming gRPC vs REST gRPC REST Protocon buffer, lightweight, fast JSON, text, slower, more bandwidth HTTP/2 (2015) HTTP/1.1 (1997) Bidirectional, Async Client/Server request response Stream support Request/Response API Oriented CRUD Oriented How to: basics Setting up a basic project with protobuf support Creating a.proto schema The generated code for data contracts, service clients, and service implementations in supported language Implement and host a service Create a client to use service Sample.proto file syntax = "proto3"; package calculator; option go_package="calculator/calculatorpb"; message SumRequest { int32 num1 = 1; int32 num2 = 2; } message SumResponse { int32 result = 1; } service CalculatorService { rpc Sum(SumRequest) returns (SumResponse) {} } Server package main fmt.Println("calculator is running...") err = s.Serve(lis) import ( "context" if err != nil { "fmt" log.Fatalf("err while serve %v", err) "log" } "net" } "calculator/calculatorpb" ) type server struct{} func (*server) Sum(ctx context.Context, req *calculatorpb.SumRequest) (*calculatorpb.SumResponse, error) { log.Println("sum called...") resp := &calculatorpb.SumResponse{ Result: req.GetNum1() + req.GetNum2(), } return resp, nil } func main() { lis, err := net.Listen("tcp", "0.0.0.0:50069") if err != nil { log.Fatalf("err while create listen %v", err) } Client package main if err != nil { log.Fatalf("call sum api err %v", err) import ( } "context" "log" log.Printf("sum api response %v\n", resp.GetResult()) } "calculator/calculatorpb" } "google.golang.org/grpc" ) func main() { cc, err := grpc.Dial("localhost:50069", grpc.withUnSecure()) if err != nil { log.Fatalf(" err while dial %v", err) } defer cc.Close() client := calculatorpb.NewCalculatorServiceClient(cc) callSum(client) } func callSum(c calculatorpb.CalculatorServiceClient) { log.Println("calling sum api") resp, err := c.Sum(context.Background(), &calculatorpb.SumRequest{ Num1: 7, Num2: 6, })

Use Quizgecko on...
Browser
Browser