$30 off During Our Annual Pro Sale. View Details »

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

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. um framework moderno e performático para comunicação entre microsserviços

  2. servidor

  3. servidor

  4. navegador servidor

  5. navegador servidor requisição

  6. navegador servidor db requisição

  7. navegador servidor db requisição resposta

  8. navegador requisição resposta db

  9. navegador requisição resposta

  10. navegador requisição resposta

  11. navegador requisição resposta

  12. navegador requisição resposta

  13. navegador requisição resposta

  14. navegador requisição resposta

  15. navegador requisição resposta

  16. navegador requisição resposta

  17. None
  18. Pode piorar…

  19. None
  20. Mas tudo pode piorar…

  21. Data Center #1

  22. Data Center #1 Data Center #2

  23. None
  24. None
  25. None
  26. X

  27. None
  28. None
  29. None
  30. None
  31. None
  32. Rafael Ponte @rponte

  33. None
  34. Fortaleza - Terra do Sol

  35. None
  36. gRPC Remote Procedure Call framework

  37. gRPC Remote Procedure Call framework

  38. Loja Virtual

  39. Online Shop Payment Service Pagamento Online

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

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

    de crédito 1234… SUCCESS
  42. 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…
  43. 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
  44. Como implementamos essa lógica?

  45. None
  46. None
  47. None
  48. None
  49. Spring Boot App

  50. Spring Boot App

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

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

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

    Local Calls
  54. Spring Boot App Client 
 (Online Shop) Service (Payment Service)

    Local Calls
  55. 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... }
  56. 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... }
  57. 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... }
  58. 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... }
  59. 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... }
  60. 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... }
  61. Mas quando trabalhamos com sistemas distribuídos…

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

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

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

    de crédito 1234… SUCCESS
  65. Spring Boot App Spring Boot App

  66. Spring Boot App Spring Boot App

  67. Spring Boot App Spring Boot App Client 
 (Online Shop)

  68. Spring Boot App Spring Boot App Service (Payment Service) Client

    
 (Online Shop)
  69. Spring Boot App Spring Boot App 
 Internet Client 


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


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

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

    Client 
 (Online Shop) Service (Payment Service)
  73. 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... }
  74. 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... }
  75. framework RPC

  76. Online Shop Payment Service RPC Framework

  77. Online Shop Payment Service RPC Framework RPC client RPC server

  78. Online Shop Payment Service RPC Framework RPC client RPC server

    processPayment() stub
  79. Online Shop Payment Service RPC Framework RPC client RPC server

    processPayment() stub waiting
  80. Online Shop Payment Service RPC Framework RPC client RPC server

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

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

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

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

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

    processPayment() stub processPayment( ) implementation {
  86. 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... }
  87. 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
  88. Mas na prática…

  89. Spring Boot App Spring Boot App 
 Internet Remote Calls

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

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

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

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

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

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

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


    (Online Shop) Service (Payment Service) Remote Calls
  97. https://deniseyu.io/art/

  98. Falando em frameworks…

  99. 1990

  100. CORBA 1990

  101. CORBA 1990 DCOM 
 e Java RMI 199x

  102. CORBA 1990 DCOM 
 e Java RMI SOAP 1998 199x

  103. CORBA 1990 DCOM 
 e Java RMI SOAP Thrift 2007

    1998 199x
  104. CORBA 1990 DCOM 
 e Java RMI SOAP Thrift gRPC

    2015 2007 1998 199x
  105. https://tools.ietf.org/html/rfc707

  106. https://tools.ietf.org/html/rfc707

  107. https://tools.ietf.org/html/rfc707

  108. gRPC Remote Procedure Call framework

  109. gRPC Remote Procedure Call framework

  110. gRPC Remote Procedure Call framework simple & idiomatic

  111. gRPC Remote Procedure Call framework simple & idiomatic performant &

    scalable
  112. gRPC Remote Procedure Call framework simple & idiomatic performant &

    scalable interoperable & extensible
  113. None
  114. gRPC vs REST API with JSON https://docs.microsoft.com/en-us/aspnet/core/grpc/comparison?view=aspnetcore-5.0

  115. gRPC Remote Procedure Call framework simple & idiomatic performant &

    scalable interoperable & extensible
  116. gRPC Remote Procedure Call framework IDL performant & scalable interoperable

    & extensible
  117. gRPC Remote Procedure Call framework IDL HTTP/2 interoperable & extensible

  118. gRPC Remote Procedure Call framework IDL HTTP/2 Protobuf v3

  119. None
  120. None
  121. None
  122. Public REST API

  123. API Gateway Public REST API

  124. API Gateway http http http http http Public REST API

  125. API Gateway Public REST API http http http http http

  126. API Gateway http http http http http Public REST API

    gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC
  127. API Gateway http http http http http Public REST API

    gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC gRPC
  128. None
  129. None
  130. None
  131. Performance

  132. 3x increase in throughput Performance

  133. 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
  134. Seu smartphone agradece!

  135. IDL (Interface De fi nition Language)

  136. 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
  137. 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
  138. 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
  139. 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
  140. 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
  141. 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
  142. 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
  143. 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
  144. 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
  145. 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
  146. 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
  147. 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
  148. 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
  149. Compilando o arquivo .proto

  150. 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
  151. 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
  152. 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
  153. 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
  154. 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
  155. 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
  156. 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
  157. No fi nal das contas…

  158. Protobuf Work fl ow

  159. .proto 1 Define Protobuf Work fl ow

  160. .proto protoc 1 Define 2 Compile Protobuf Work fl ow

  161. .proto protoc go java python 1 Define 2 Compile Protobuf

    Work fl ow
  162. .proto protoc go java python 1 Define 2 Compile 0101010


    1010101
 0101010
 3 Serialize Protobuf Work fl ow
  163. gRPC Work fl ow

  164. .proto 1 Define gRPC Work fl ow

  165. .proto protoc 1 Define 2 Compile gRPC Work fl ow

  166. .proto protoc go java python 1 Define 2 Compile gRPC

    Work fl ow
  167. .proto protoc go java python 1 Define 2 Compile server

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

    3 Implement gRPC Work fl ow client
  169. 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
  170. 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
  171. 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
  172. 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
  173. 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
  174. 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
  175. 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
  176. 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
  177. 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
  178. 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
  179. 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
  180. 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
  181. 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
  182. Padrões de comunicação

  183. None
  184. 1 2 3 4

  185. server client Simple RPC (Unary RPC) 2 3 4

  186. server client server client Simple RPC (Unary RPC) Server-side Streaming

    RPC 3 4
  187. server client server client server client Simple RPC (Unary RPC)

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

    (Unary RPC) Server-side Streaming RPC Client-side Streaming RPC Bidirectional Streaming RPC
  189. 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
  190. 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)
  191. 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
  192. 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
  193. 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
  194. 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
  195. 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
  196. 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
  197. 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) { } }
  198. .proto protoc go java python 1 Define 2 Compile server

    3 Implement gRPC Work fl ow client
  199. CONCLUINDO

  200. Microservices

  201. Microservices Mobile

  202. Microservices Mobile Realtime

  203. https://grpc.io/

  204. None
  205. https://www.zup.com.br/orange-talents

  206. @rponte Rafael Ponte