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

Introdução a gRPC: um framework moderno e perfo...

Introdução a gRPC: um framework moderno e performático para comunicação entre microsserviços

Com a grande adoção no uso de microsserviços e implantação de sistemas nas nuvens ficou evidente que muitas das tecnologias e protocolos de comunicação utilizados não tiram o real proveito da infraestrutura existente nos grandes provedores de cloud. Não só isso, algumas vezes esta comunicação se torna o gargalo quando temos centenas ou milhares de serviços em momentos de pico.

Não à toa, em 2015, a Google publicou de forma open-source seu projeto interno de  framework RPC como solução alternativa na integração de microsserviços, aplicações mobile e real-time, conhecido como gRPC. Um framework que se apoia no ombro de gigantes como HTTP/2 e Protobuf v3 para prover alta-performance, baixa latência, uso eficiente de CPU e interoperabilidade entre plataformas. Não bastasse ser mais rápido e eficiente do que APIs REST, com gRPC abraçamos uma forma mais idiomática e expressiva de descrever APIs fortemente tipadas na linguagem de sua escolha.

Se você nunca ouviu falar ou entende pouco sobre gRPC, então essa palestra é para você!

(GRAVAÇÃO: https://youtu.be/r8gOKuWgNvw?list=PLHMMERsvy9EyWQPru4SrJAYHEGKfkjRgP&t=456)

Rafael Ponte

March 04, 2021
Tweet

More Decks by Rafael Ponte

Other Decks in Technology

Transcript

  1. X

  2. Online Shop Payment Service Pagamento Online cobre R$120.20 do cartão

    de crédito 1234… SUCCESS cobre R$16.99 do cartão de crédito 9876…
  3. Online Shop Payment Service Pagamento Online cobre R$120.20 do cartão

    de crédito 1234… SUCCESS cobre R$16.99 do cartão de crédito 9876… ERROR
  4. PaymentRequest request = new PaymentRequest() ; request.setCardNumber("1234 5678 8765 4321");

    request.setExpiryDate("03/2028"); request.setCvc("123") ; request.setAmount(120.2) ; request.setCurrency(Currency.BRL) ; Result result = paymentService.processPayment(request) ; if (result.isSuccess()) { // processa fluxo do pedido... }
  5. PaymentRequest request = new PaymentRequest() ; request.setCardNumber("1234 5678 8765 4321");

    request.setExpiryDate("03/2028"); request.setCvc("123") ; request.setAmount(120.2) ; request.setCurrency(Currency.BRL) ; Result result = paymentService.processPayment(request) ; if (result.isSuccess()) { // processa fluxo do pedido... }
  6. PaymentRequest request = new PaymentRequest() ; request.setCardNumber("1234 5678 8765 4321");

    request.setExpiryDate("03/2028"); request.setCvc("123") ; request.setAmount(120.2) ; request.setCurrency(Currency.BRL) ; Result result = paymentService.processPayment(request) ; if (result.isSuccess()) { // processa fluxo do pedido... }
  7. PaymentRequest request = new PaymentRequest() ; request.setCardNumber("1234 5678 8765 4321");

    request.setExpiryDate("03/2028"); request.setCvc("123") ; request.setAmount(120.2) ; request.setCurrency(Currency.BRL) ; Result result = paymentService.processPayment(request) ; if (result.isSuccess()) { // processa fluxo do pedido... }
  8. PaymentRequest request = new PaymentRequest() ; request.setCardNumber("1234 5678 8765 4321");

    request.setExpiryDate("03/2028"); request.setCvc("123") ; request.setAmount(120.2) ; request.setCurrency(Currency.BRL) ; Result result = paymentService.processPayment(request) ; if (result.isSuccess()) { // processa fluxo do pedido... }
  9. PaymentRequest request = new PaymentRequest() ; request.setCardNumber("1234 5678 8765 4321");

    request.setExpiryDate("03/2028"); request.setCvc("123") ; request.setAmount(120.2) ; request.setCurrency(Currency.BRL) ; Result result = paymentService.processPayment(request) ; if (result.isSuccess()) { // processa fluxo do pedido... }
  10. Spring Boot App Spring Boot App 
 Internet Client 


    (Online Shop) Service (Payment Service)
  11. Spring Boot App Spring Boot App 
 Internet Client 


    (Online Shop) Service (Payment Service)
  12. Spring Boot App Spring Boot App 
 Internet Remote Calls

    Client 
 (Online Shop) Service (Payment Service)
  13. Spring Boot App Spring Boot App 
 Internet Remote Calls

    Client 
 (Online Shop) Service (Payment Service)
  14. PaymentRequest request = new PaymentRequest() ; request.setCardNumber("1234 5678 8765 4321");

    request.setExpiryDate("03/2028"); request.setCvc("123") ; request.setAmount(120.2) ; request.setCurrency(Currency.BRL) ; Result result = paymentService.processPayment(request) ; if (result.isSuccess()) { // processa fluxo do pedido... }
  15. PaymentRequest request = new PaymentRequest() ; request.setCardNumber("1234 5678 8765 4321");

    request.setExpiryDate("03/2028"); request.setCvc("123") ; request.setAmount(120.2) ; request.setCurrency(Currency.BRL) ; Result result = paymentService.processPayment(request) ; if (result.isSuccess()) { // processa fluxo do pedido... }
  16. Online Shop Payment Service RPC Framework RPC client RPC server

    processPayment() stub processPayment( ) implementation {
  17. Online Shop Payment Service RPC Framework RPC client RPC server

    processPayment() stub processPayment( ) implementation {
  18. Online Shop Payment Service RPC Framework RPC client RPC server

    processPayment() stub processPayment( ) implementation {
  19. Online Shop Payment Service RPC Framework RPC client RPC server

    processPayment() stub processPayment( ) implementation {
  20. PaymentRequest request = new PaymentRequest() ; request.setCardNumber("1234 5678 8765 4321");

    request.setExpiryDate("03/2028"); request.setCvc("123") ; request.setAmount(120.2) ; request.setCurrency(Currency.BRL) ; Result result = paymentService.processPayment(request) ; if (result.isSuccess()) { // processa fluxo do pedido... }
  21. PaymentRequest request = new PaymentRequest() ; request.setCardNumber("1234 5678 8765 4321");

    request.setExpiryDate("03/2028"); request.setCvc("123") ; request.setAmount(120.2) ; request.setCurrency(Currency.BRL) ; Result result = paymentService.processPayment(request) ; if (result.isSuccess()) { // processa fluxo do pedido... } Location Transparency
  22. Spring Boot App Spring Boot App 
 Internet Remote Calls

    Client 
 (Online Shop) Service (Payment Service)
  23. Spring Boot App Spring Boot App Client 
 (Online Shop)

    Service (Payment Service) 
 Internet Remote Calls
  24. Spring Boot App Client 
 (Online Shop) Spring Boot App

    Service (Payment Service) 
 Internet Remote Calls
  25. Spring Boot App Client 
 (Online Shop) Spring Boot App

    Service (Payment Service) 
 Internet Remote Calls
  26. Spring Boot App Client 
 (Online Shop) Spring Boot App

    Service (Payment Service) 
 Internet Remote Calls
  27. Spring Boot App Client 
 (Online Shop) Spring Boot App

    Service (Payment Service) 
 Internet Remote Calls
  28. Spring Boot App Client 
 (Online Shop) Spring Boot App

    Service (Payment Service) 
 Internet Remote Calls
  29. Spring Boot App Spring Boot App 
 Internet Client 


    (Online Shop) Service (Payment Service) Remote Calls
  30. API Gateway http http http http http Public REST API

    gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC
  31. API Gateway http http http http http Public REST API

    gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC
  32. 3x increase in throughput 11x di ff erence per CPU

    Performance https://cloud.google.com/blog/products/gcp/announcing-grpc-alpha-for-google-cloud-pubsub
  33. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  34. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  35. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  36. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  37. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  38. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  39. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  40. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  41. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  42. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  43. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  44. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  45. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  46. Channel channel = ManagedChannelBuilde r .forAddress("paymentservice.com", 50051 ) .build() ;

    PaymentServiceBlockingStub paymentService = PaymentServiceGrpc.newBlockingStub(channel) ; PaymentRequest request = newBuilder( ) .setCardNumber("1234 5678 8765 4321" ) .setExpiryDate("03/2028" ) .setCvc("123" ) .setAmount(120.2 ) .setCurrency(Currency.BRL ) .build() ; PaymentResponse result = paymentService.processPayment(request) ; if (result.getSuccess()) { // processa fluxo do pedido.. . } gRPC Client | Consumindo endpoint de pagamento
  47. Channel channel = ManagedChannelBuilde r .forAddress("paymentservice.com", 50051 ) .build() ;

    PaymentServiceBlockingStub paymentService = PaymentServiceGrpc.newBlockingStub(channel) ; PaymentRequest request = newBuilder( ) .setCardNumber("1234 5678 8765 4321" ) .setExpiryDate("03/2028" ) .setCvc("123" ) .setAmount(120.2 ) .setCurrency(Currency.BRL ) .build() ; PaymentResponse result = paymentService.processPayment(request) ; if (result.getSuccess()) { // processa fluxo do pedido.. . } gRPC Client | Consumindo endpoint de pagamento
  48. Channel channel = ManagedChannelBuilde r .forAddress("paymentservice.com", 50051 ) .build() ;

    PaymentServiceBlockingStub paymentService = PaymentServiceGrpc.newBlockingStub(channel) ; PaymentRequest request = newBuilder( ) .setCardNumber("1234 5678 8765 4321" ) .setExpiryDate("03/2028" ) .setCvc("123" ) .setAmount(120.2 ) .setCurrency(Currency.BRL ) .build() ; PaymentResponse result = paymentService.processPayment(request) ; if (result.getSuccess()) { // processa fluxo do pedido.. . } gRPC Client | Consumindo endpoint de pagamento
  49. Channel channel = ManagedChannelBuilde r .forAddress("paymentservice.com", 50051 ) .build() ;

    PaymentServiceBlockingStub paymentService = PaymentServiceGrpc.newBlockingStub(channel) ; PaymentRequest request = newBuilder( ) .setCardNumber("1234 5678 8765 4321" ) .setExpiryDate("03/2028" ) .setCvc("123" ) .setAmount(120.2 ) .setCurrency(Currency.BRL ) .build() ; PaymentResponse result = paymentService.processPayment(request) ; if (result.getSuccess()) { // processa fluxo do pedido.. . } gRPC Client | Consumindo endpoint de pagamento
  50. Channel channel = ManagedChannelBuilde r .forAddress("paymentservice.com", 50051 ) .build() ;

    PaymentServiceBlockingStub paymentService = PaymentServiceGrpc.newBlockingStub(channel) ; PaymentRequest request = newBuilder( ) .setCardNumber("1234 5678 8765 4321" ) .setExpiryDate("03/2028" ) .setCvc("123" ) .setAmount(120.2 ) .setCurrency(Currency.BRL ) .build() ; PaymentResponse result = paymentService.processPayment(request) ; if (result.getSuccess()) { // processa fluxo do pedido.. . } gRPC Client | Consumindo endpoint de pagamento
  51. Channel channel = ManagedChannelBuilde r .forAddress("paymentservice.com", 50051 ) .build() ;

    PaymentServiceBlockingStub paymentService = PaymentServiceGrpc.newBlockingStub(channel) ; PaymentRequest request = newBuilder( ) .setCardNumber("1234 5678 8765 4321" ) .setExpiryDate("03/2028" ) .setCvc("123" ) .setAmount(120.2 ) .setCurrency(Currency.BRL ) .build() ; PaymentResponse result = paymentService.processPayment(request) ; if (result.getSuccess()) { // processa fluxo do pedido.. . } gRPC Client | Consumindo endpoint de pagamento
  52. Channel channel = ManagedChannelBuilde r .forAddress("paymentservice.com", 50051 ) .build() ;

    PaymentServiceBlockingStub paymentService = PaymentServiceGrpc.newBlockingStub(channel) ; PaymentRequest request = newBuilder( ) .setCardNumber("1234 5678 8765 4321" ) .setExpiryDate("03/2028" ) .setCvc("123" ) .setAmount(120.2 ) .setCurrency(Currency.BRL ) .build() ; PaymentResponse result = paymentService.processPayment(request) ; if (result.getSuccess()) { // processa fluxo do pedido.. . } gRPC Client | Consumindo endpoint de pagamento
  53. .proto protoc go java python 1 Define 2 Compile 0101010


    1010101
 0101010
 3 Serialize Protobuf Work fl ow
  54. .proto protoc go java python 1 Define 2 Compile server

    3 Implement gRPC Work fl ow client
  55. .proto protoc go java python 1 Define 2 Compile server

    3 Implement gRPC Work fl ow client
  56. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  57. syntax = "proto3" ; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } } message PaymentRequest { enum Currency { BRL = 0 ; USD = 1 ; } string cardNumber = 1 ; string expiryDate = 2 ; string cvc = 3 ; double amount = 4 ; Currency currency = 5 ; } message PaymentResponse { bool success = 1 ; string txId = 2 ; } paymentService.proto
  58. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Override public void processPayment(PaymentRequest

    request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  59. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Override public void processPayment(PaymentRequest

    request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  60. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Override public void processPayment(PaymentRequest

    request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  61. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Overrid e public void

    processPayment(PaymentRequest request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  62. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Overrid e public void

    processPayment(PaymentRequest request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  63. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Overrid e public void

    processPayment(PaymentRequest request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  64. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Overrid e public void

    processPayment(PaymentRequest request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  65. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Overrid e public void

    processPayment(PaymentRequest request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  66. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Overrid e public void

    processPayment(PaymentRequest request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  67. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Overrid e public void

    processPayment(PaymentRequest request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  68. public class PaymentEndpoint extends PaymentServiceGrpc.PaymentServiceImplBase { @Overrid e public void

    processPayment(PaymentRequest request , StreamObserver<PaymentResponse> responseObserver) { // lógica de negóci o Payment payment = toPayment(request); // converte para modelo de domíni o UUID transactionId = new PaymentService().process(payment) ; // envia resposta para o clien t responseObserver.onNext(PaymentResponse.newBuilder( ) .setTxId(transactionId.toString() ) .setSuccess(true ) .build()) ; responseObserver.onCompleted() ; } } gRPC Server | Expondo endpoint de pagamento
  69. server client server client server client Simple RPC (Unary RPC)

    Server-side Streaming RPC Client-side Streaming RPC 4
  70. server client server client server client server client Simple RPC

    (Unary RPC) Server-side Streaming RPC Client-side Streaming RPC Bidirectional Streaming RPC
  71. syntax = "proto3"; service PaymentService { rpc processPayment(PaymentRequest) returns (PaymentResponse)

    { } rpc listenToTransactions(TransactionRequest) returns (stream TransactionResponse) { } rpc processPaymentInBatch(stream PaymentInBatchRequest) returns (PaymentInBatchResponse) { } rpc validateCreditCard(stream CreditCardRequest) returns (stream ValidateCCResponse) { } } paymentService.proto
  72. syntax = "proto3"; service PaymentService { rpc processPayment(PaymentRequest) returns (PaymentResponse)

    { } rpc listenToTransactions(TransactionRequest) returns (stream TransactionResponse) { } rpc processPaymentInBatch(stream PaymentInBatchRequest) returns (PaymentInBatchResponse) { } rpc validateCreditCard(stream CreditCardRequest) returns (stream ValidateCCResponse) { } } paymentService.proto Simple RPC (Unary RPC)
  73. syntax = "proto3"; service PaymentService { rpc processPayment(PaymentRequest) returns (PaymentResponse)

    { } rpc listenToTransactions(TransactionRequest) returns (stream TransactionResponse) { } rpc processPaymentInBatch(stream PaymentInBatchRequest) returns (PaymentInBatchResponse) { } rpc validateCreditCard(stream CreditCardRequest) returns (stream ValidateCCResponse) { } } paymentService.proto
  74. syntax = "proto3"; service PaymentService { rpc processPayment(PaymentRequest) returns (PaymentResponse)

    { } rpc listenToTransactions(TransactionRequest) returns (stream TransactionResponse) { } rpc processPaymentInBatch(stream PaymentInBatchRequest) returns (PaymentInBatchResponse) { } rpc validateCreditCard(stream CreditCardRequest) returns (stream ValidateCCResponse) { } } paymentService.proto Server-side Streaming RPC
  75. syntax = "proto3"; service PaymentService { rpc processPayment(PaymentRequest) returns (PaymentResponse)

    {} rpc listenToTransactions(TransactionRequest) returns (stream TransactionResponse) {} rpc processPaymentInBatch(stream PaymentInBatchRequest) returns (PaymentInBatchResponse) { } rpc validateCreditCard(stream CreditCardRequest) returns (stream ValidateCCResponse) { } } paymentService.proto
  76. syntax = "proto3"; service PaymentService { rpc processPayment(PaymentRequest) returns (PaymentResponse)

    { } rpc listenToTransactions(TransactionRequest) returns (stream TransactionResponse) {} rpc processPaymentInBatch(stream PaymentInBatchRequest) returns (PaymentInBatchResponse) { } rpc validateCreditCard(stream CreditCardRequest) returns (stream ValidateCCResponse) { } } paymentService.proto Client-side Streaming RPC
  77. syntax = "proto3"; service PaymentService { rpc processPayment(PaymentRequest) returns (PaymentResponse)

    { } rpc listenToTransactions(TransactionRequest) returns (stream TransactionResponse) { } rpc processPaymentInBatch(stream PaymentInBatchRequest) returns (PaymentInBatchResponse) {} rpc validateCreditCard(stream CreditCardRequest) returns (stream ValidateCCResponse) { } } paymentService.proto
  78. syntax = "proto3"; service PaymentService { rpc processPayment(PaymentRequest) returns (PaymentResponse)

    { } rpc listenToTransactions(TransactionRequest) returns (stream TransactionResponse) {} rpc processPaymentInBatch(stream PaymentInBatchRequest) returns (PaymentInBatchResponse) { } rpc validateCreditCard(stream CreditCardRequest) returns (stream ValidateCCResponse) { } } paymentService.proto Bidirectional Streaming RPC
  79. paymentService.proto syntax = "proto3"; service PaymentService { rpc processPayment(PaymentRequest) returns

    (PaymentResponse) { } rpc listenToTransactions(TransactionRequest) returns (stream TransactionResponse) { } rpc processPaymentInBatch(stream PaymentInBatchRequest) returns (PaymentInBatchResponse) { } rpc validateCreditCard(stream CreditCardRequest) returns (stream CreditCardResponse) { } }
  80. .proto protoc go java python 1 Define 2 Compile server

    3 Implement gRPC Work fl ow client