Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Full Stack optimization with gRPC and gRPC-web

Full Stack optimization with gRPC and gRPC-web

Você sabia que é possível adicionar uma maior agilidade na comunicação entre suas APIs ou nas chamadas para suas APIs via web usando gRPC, um framework open-source universal orientado ao ganho de performance?
Nessa talk, apresentaremos os benefícios deste framework e realizaremos uma análise comparativa de tempos de resposta entre um cluster de microsserviços que utiliza REST e mensagens com formato JSON e outro que utiliza gRPC e Protobuffer. Além disso, vamos demonstrar como é fácil integrar seu front com a utilização da interface gRPC-web. Vamos acelerar! 🚀

rodrigopandini

February 13, 2019
Tweet

More Decks by rodrigopandini

Other Decks in Technology

Transcript

  1. "Write programs that do one thing and do it well.

    Write programs to work together." — Unix Philosophy
  2. "The biggest issue in changing a monolith into microservices lies

    in changing the communication pattern" — Martin Flower
  3. gRPC RPC framework de ned on top of HTTP/2 Protobuf

    integrations Implementation in ~10 languages Streaming capabilities Feature rich
  4. HTTP/2 Data compression of HTTP headers Server-side Push Pipelining of

    requests Multiplexing multiple requests over a single TCP connection
  5. Protobuffer IDL (Interface de nition language) Data model Wire format

    "think XML, but smaller, faster, and simpler"
  6. syntax = "proto3"; package helloworld; service Greeter { rpc SayHello

    (HelloRequest) returns (HelloReply); rpc RepeatHello (HelloRequest) returns (stream HelloReply); } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
  7. JSON vs. Protobuffer Text vs. Binary Flexibility vs. Well de

    ned types Errors catch (De)serialization
  8. protoc Compile .proto les Code-generator Plugins $ protoc -I helloworld/

    helloworld/helloworld.proto --go_out=plugins=grpc:helloworld // generates "helloworld.pb.go" file
  9. // server package main import ( "context" "log" "net" "google.golang.org/grpc"

    pb "google.golang.org/grpc/examples/helloworld/helloworld" ) const ( port = ":50051" ) // server is used to implement helloworld.GreeterServer. type server struct{} // SayHello implements helloworld.GreeterServer func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { log.Printf("Received: %v", in.Name) return &pb.HelloReply{Message: "Hello " + in.Name}, nil } func main() { lis, err := net.Listen("tcp", port) if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{}) if err := s.Serve(lis); err != nil {
  10. // client package main import ( "context" "log" "os" "time"

    "google.golang.org/grpc" pb "google.golang.org/grpc/examples/helloworld/helloworld" ) const ( address = "localhost:50051" defaultName = "world" ) func main() { // Set up a connection to the server. conn, err := grpc.Dial(address, grpc.WithInsecure()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := pb.NewGreeterClient(conn) // Contact the server and print out its response. name := defaultName if len(os.Args) > 1 { name = os.Args[1] } ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  11. But is all JS grpc-node: Use a Node C++ Addon

    Communication inter-microservices Browser !== Server
  12. gRPC-web Limitations don't allow implements the full gRPC protocol in

    the web browser Proposed gRPC-web speci cation: currently implemented with Envoy Experimental support for TypeScript
  13. protoc generate JS $ protoc -I=. helloworld.proto --js_out=import_style=commonjs:. --grpc-web_out=import_style=commonjs,mode=grpcwebtext:. const

    { HelloRequest, HelloReply } = require('./helloworld_pb.js'); var request = new HelloRequest(); request.setName('John'); const { GreeterClient } = require('./helloworld_grpc_web_pb.js'); const client = new GreeterClient('https://api.myhost.com'); client.sayHello(request, metadata, (err, response) => { console.log(response.getMessage()); });
  14. Envoy Con guration Envoy proxy con guration to run gRCP-web

    - "*" allow_methods: GET, PUT, DELETE, POST, OPTIONS allow_headers: | keep-alive,user-agent,cache-control,content-type
  15. Advantages End-to-end gRPC Tighter coordination between frontend and backend teams:

    .proto as source of truth Easily generate client libraries
  16. syntax = "proto3"; option optimize_for = SPEED; package transactionproto; service

    Transaction { rpc PostTransaction (TransactionModel) returns (Response) { } } message Response{ string status = 1; } message TransactionModel { string mti = 1; string caller = 201; string correlation_id = 200; string field2_primary_account_number = 2; string field3_processing_code = 3; string field4_amount_transaction = 4; string field5_amount_settlement = 5; string field6_amount_cardholder_billing = 6; string field7_tranmission_date_and_time = 7; string field8_amount_cardholder_billing_fee = 8; string field9_conversion_rate_settlement = 9; string field10_conversion_rate_cardholder_billing = 10; string field11_stan = 11; string field12_time_local_transaction = 12; string field13_date_local_transaction = 13; string field14_date_expiration = 14; string field15_date_settlement = 15; string field16_date_conversion = 16;
  17. const transaction = { mti: "0100", caller: "BANDEIRA", correlation_id: "meetup",

    field2_primary_account_number: "9999999999999999", field3_processing_code: "009988", field4_amount_transaction: "13.98", field5_amount_settlement: "13.98", field6_amount_cardholder_billing: "13.98", field7_tranmission_date_and_time: "SAMPLE", field8_amount_cardholder_billing_fee: "SAMPLE", field9_conversion_rate_settlement: "SAMPLE", field10_conversion_rate_cardholder_billing: "SAMPLE", field11_stan: "SAMPLE", field12_time_local_transaction: "SAMPLE", field13_time_local_transaction: "SAMPLE", field14_date_expiration: "SAMPLE", field15_date_settlement: "SAMPLE", field16_date_conversion: "SAMPLE", field17_date_capture: "SAMPLE", field18_merchant_type: "SAMPLE", field19_omitempty: "SAMPLE", field20_pan: "SAMPLE", field21_forwarding_institution_country_code: "SAMPLE", field22_pos_entry_mode: "SAMPLE", field23_card_sequence_number: "SAMPLE", field24_network_international_id: "SAMPLE", field25_pos_condition_code: "SAMPLE", field26_pos_pin_capture_code: "SAMPLE", field27_authorization_id_response_length: "SAMPLE", field28_amount_transaction_fee: "SAMPLE", field29_amount_settlement_fee: "SAMPLE", field30_amount_transaction_processing_fee: "SAMPLE", field31_amount_settlement_processing_fee: "SAMPLE",
  18. syntax = "proto3"; package helloworld; service Greeter { rpc SayHello

    (HelloRequest) returns (HelloReply); } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
  19. Sample Service Generated according to the google.api.http annotations e {

    string value = 1;}service YourService { rpc Echo(StringMessage) returns (StringMessa
  20. Conclusion microservices: communication importance! HTTP/1.x -> HTTP/2.0 | JSON ->

    Protobuffer gRPC will become dominant very soon? gRPC-web ready? ROADMAP Performance!