Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

gRPC - czyli jak skutecznie rozmawiać (rg-dev#14)

221 views

Published on

Skuteczna komunikacja w naszych aplikacjach, czy to w mikroserwisach, czy w monolicie, to kluczowa kwestia. A im większa skala i ilość danych, tym problem bardziej złożony. Podczas prezentacji wprowadzę Was w świat usług gRPC, omówimy na przykładzie sposób działania, znajdziemy jego mocne i słabe strony oraz porównamy gRPC do innych rozwiązań.

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

gRPC - czyli jak skutecznie rozmawiać (rg-dev#14)

  1. 1. RG-DEV #14
  2. 2. MICHAŁ KRUCZEK So ware Engineer & PHP Developer since 2010 bass guitarist and trumpeter kruczek.it Created by /Michał Kruczek @partikus
  3. 3. GRPC HOW TO COMMUNICATE EFFECTIVELY?
  4. 4. AGENDA Intro The RPC story gRPC Demo
  5. 5. EFFECTIVE COMMUNICATION A two way information sharing process which involves one party sending a message that is easily understood by the receiving party.
  6. 6. Accounting Boss Employees Warehouse
  7. 7. Store Invoices Reports Warehouse
  8. 8. Order InvoiceFactory NotificationService
  9. 9. THE RPC STORY
  10. 10. ~1970 - RPC ~1980 - IMPLEMENTATION ~1990 - OOP & CORBA
  11. 11. WHAT'S RPC? Remote Procedure Call (RPC) is a protocol that one program can use to request a service from a program located in another computer on a network without having to understand the network's details. A procedure call is also sometimes known as a function call or a subroutine call.
  12. 12. WHY RPC? WRAPS NETWORKS INTERACTIONS FLEXIBLE & MAINTAINABLE EASY TO DEVELOP DISTRIBUTED SYSTEMS
  13. 13. RPC'S DICTIONARY
  14. 14. STUBS A stub in distributed computing is a piece of code that converts parameters passed between client and server during a remote procedure call (RPC).
  15. 15. IDL INTERFACE DEFINITION LANGUAGE IDL is a specification language used to describe a so ware component's application programming interface (API)
  16. 16. CHALLANGE
  17. 17. PHP GO PYTHONJAVA
  18. 18. CONTRACT CLIENTS' IMPLEMENTATIONS SERVERS' STUBS PERFORMANCE
  19. 19. GRPC A HIGH PERFORMANCE, OPEN-SOURCE UNIVERSAL RPC FRAMEWORK
  20. 20. WHY WOULD I WANT TO USE GRPC?
  21. 21. LOW LATENCY, HIGHLY SCALABLE, DISTRIBUTED SYSTEMS. DEVELOPING MOBILE CLIENTS WHICH ARE COMMUNICATING TO A CLOUD SERVER. DESIGNING A NEW PROTOCOL THAT NEEDS TO BE ACCURATE, EFFICIENT AND LANGUAGE INDEPENDENT. LAYERED DESIGN TO ENABLE EXTENSION EG. AUTHENTICATION, LOAD BALANCING, LOGGING AND MONITORING ETC.
  22. 22. HTTP/2
  23. 23. WHAT ARE THE KEY DIFFERENCES TO HTTP/1.X? IS BINARY, INSTEAD OF TEXTUAL IS FULLY MULTIPLEXED, INSTEAD OF ORDERED AND BLOCKING CAN THEREFORE USE ONE CONNECTION FOR PARALLELISM USES HEADER COMPRESSION TO REDUCE OVERHEAD ALLOWS SERVERS TO “PUSH” RESPONSES PROACTIVELY INTO CLIENT CACHES
  24. 24. PROTOCOL BUFFERS AS THE IDL
  25. 25. syntax = "proto3"; package Kruczek.RgDev.Twitter; enum ResultType { MIXED = 0; RECENT = 1; POPULAR = 2; } message Query { string q = 1; string since = 2; int32 count = 3; string lang = 4; ResultType type = 5; repeated string tracks = 6; repeated string follows = 7;
  26. 26. ... message Author { string id = 1; string name = 2; string screenName = 3; string avatar = 4; } message Tweet { string id = 1; string text = 2; Author author = 3; int32 retweetCount = 4; int32 favoriteCount = 5; }
  27. 27. SERVICE METHODS' TYPES
  28. 28. UNARY RPC rpc SayHello(HelloRequest) returns (HelloResponse) { }
  29. 29. SERVER STREAMING RPC rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse) { }
  30. 30. CLIENT STREAMING RPC rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse) { }
  31. 31. BIDIRECTIONAL STREAMING RPC rpc BidiHello(stream HelloRequest) returns (stream HelloResponse) { }
  32. 32. PROS IDIOMATIC CLIENT LIBRARIES IN 10 LANGUAGES HIGHLY EFFICIENT ON WIRE AND WITH A SIMPLE SERVICE DEFINITION FRAMEWORK BI-DIRECTIONAL STREAMING WITH HTTP/2 BASED TRANSPORT PLUGGABLE AUTH, TRACING, LOAD BALANCING AND HEALTH CHECKING
  33. 33. CONS MISSING PHP SERVER MISSING OFFICIAL JS-WEB CLIENT
  34. 34. DEMO APP TWITTER LIVE STREAMING
  35. 35. LET'S DEFINE A CONTRACT
  36. 36. syntax = "proto3"; package Kruczek.RgDev.Twitter; service TwitterBoard { rpc GetTweets(Query) returns (stream Tweet) {} } ...
  37. 37. GENERATE PHP CLIENT #!/usr/bin/env bash set -ex php_output="php/grpc" rm -rf "$php_output" && mkdir -p "$php_output" protoc --php_out="$php_output" --grpc_out="$php_output" --plugin=protoc-gen-grpc=/usr/local/bin/grpc_php_plugin twitter.proto
  38. 38. GENERATE NODEJS SERVER #!/usr/bin/env bash nodejs_output="nodejs/grpc" rm -rf "$nodejs_output" && mkdir -p "$nodejs_output" protoc --js_out="import_style=commonjs,binary:$nodejs_output" --grpc_out="$nodejs_output" --plugin=protoc-gen-grpc=/usr/local/bin/grpc_node_plugin twitter.proto
  39. 39. NODEJS SERVER THAT USES REST API
  40. 40. import grpc from "grpc"; import twitter from "twitter"; import twitterService from "./../server/twitter.service"; import services from "./../grpc/twitter_grpc_pb"; const client = new twitter(credentials); const service = new twitterService(client); function main() { const server = new grpc.Server(); server.addService(services.TwitterBoardService, service); server.bind('0.0.0.0:50052', grpc.ServerCredentials.createInsecure()) server.start(); } main();
  41. 41. NODEJS SERVER THAT USES TWITTER STREAMING
  42. 42. ANOTHER SERVICE IMPEMENTATION ... import twitterService from "./../server/streamTwitter.service"; ...
  43. 43. <?php class Client { private $client; public function __construct(string $hostname) { $this->client = new TwitterBoardClient($hostname, ['credentials' = } }
  44. 44. /** * @param Query $query * @return Generator|Tweet[] */ public function getTweets(Query $query): Generator { /** @var ServerStreamingCall $response */ $response = $this->client->GetTweets($query); foreach ($response->responses() as $response) { yield $response; } }
  45. 45. LIVE DEMO LET'S TWEETS ;-)
  46. 46. QUESTIONS? SLIDES: SLIDESHARE.NET/MICHASZCZUR TWITTER: @PARTIKUS HOMEPAGE: KRUCZEK.IT
  47. 47. THANKS!
  48. 48. LINKS github.com/partikus/grpc-example grpc.io developers.google.com/protocol-buffers

×