gRPC:更高效的微服務介面

B20a8b620ff35bb1c675a3b7527a70e2?s=47 William Yeh
September 25, 2020

 gRPC:更高效的微服務介面

gRPC: a more efficient microservice interface.

在 Web service 時代,基於 HTTP 的 REST 簡單易用,是最流行的 API 服務介面。然而,到了微服務時代,服務互連現象更為頻繁緊密,以前 REST 的優點卻變成缺點:過於簡單的模型,欠缺基本的服務治理;過於陽春的 HTTP,欠缺高效的傳輸介面。

新一代的 gRPC,則是在 HTTP/2 的基礎上,盡量維持簡單易用的特性,又具備微服務世界該有的服務治理與高效傳輸。

本演講將示範 gRPC 的工作流程:介面定義、編程、測試、雲端應用實務,讓你可以開始嘗試導入 gRPC。

B20a8b620ff35bb1c675a3b7527a70e2?s=128

William Yeh

September 25, 2020
Tweet

Transcript

  1. gRPC:更⾼效的微服務介⾯ A more efficient microservice interface 葉秉哲 (William Yeh) 2020-09-25

    Sr. Software Engineer
  2. PhD in Computer Science, NCTU, Taiwan CSPO (Certified Scrum Product

    Owner) from Scrum Alliance PSM I (Professional Scrum Master I) from Scrum.org Sr. Software Engineer & Scrum Master @ Carousell (since Jan 2020) Server director & Scrum master @ Gogolook Active speaker and trainer for software architecture, DevOps, agile, and theory of constraints
  3. Carousell is a classifieds marketplace that makes selling as easy

    as taking a photo, buying as simple as chaAng. Launched in August 2012, Carousell began in Singapore and now has a presence in eight markets across Asia. With over 250 million lisGngs, we are one of the world’s largest and fastest-growing marketplaces in Southeast Asia, Taiwan and Hong Kong, and are backed by Telenor Group, Rakuten Ventures, Sequoia India and Naspers. The Carousell marketplace has a diverse range of products across a variety of categories, including cars, lifestyle, gadgets and fashion accessories.
  4. With over 250 million lisBngs, we are one of the

    world’s largest and fastest-growing marketplaces in Southeast Asia, Taiwan and Hong Kong. The Carousell marketplace has a diverse range of products across a variety of categories, including cars, lifestyle, gadgets and fashion accessories. We use gRPC!
  5. 簡易 性能 左右擺盪

  6. 簡易 性能 左右擺盪 Unix RPC Java RMI SOAP Web Service

    REST CORBA
  7. 臺灣開放銀⾏⼤進展!⾸版 Open API 標準出爐 https://www.ithome.com.tw/news/131648

  8. 臺灣開放銀⾏⼤進展!⾸版 Open API 標準出爐 https://www.ithome.com.tw/news/131648

  9. hWps://github.com/dotnet-architecture/eShopOnContainers Microservice world…

  10. Is REST still good enough in the microservice world?

  11. 簡易 REST 過於簡單的模型, ⽋缺基本的服務治理。 過於陽春的 HTTP, ⽋缺⾼效的傳輸介⾯。 HTTP 1.1 Swagger

    WebSocket OpenAPI 缺點 ➠改進 (個別)
  12. ➠改進 (個別) https://www.npmjs.com/package/swagger-model-validator

  13. 簡易 性能 左右擺盪 gRPC 過於簡單的模型, ⽋缺基本的服務治理。 過於陽春的 HTTP, ⽋缺⾼效的傳輸介⾯。 HTTP/2

    Protocol Buffers + = REST (綜合) ?
  14. 簡易 性能 左右擺盪 REST Unix RPC Java RMI SOAP Web

    Service CORBA gRPC
  15. https://grpc.io/about/#the-story-behind-grpc

  16. gRPC app Protobuf HTTP/2 HTTP 1.1 TCP + TLS 1.2

    IP HTTP app
  17. HTTP/2 HTTP 1.1 TCP + TLS 1.2 IP HTTP app

  18. Demo #1 HTTP/2 ⾼效的傳輸介⾯

  19. curl http://nghttp2.org/ HTTP 1.1 HTTP/2

  20. curl -vkso /dev/null http://nghttp2.org/ HTTP/1.1 header size = 385

  21. curl --http2 -vkso /dev/null http://nghttp2.org/ HTTP/2 header size = 224

  22. HTTP/2 HTTP 1.1 TCP + TLS 1.2 IP HTTP app

  23. curl http://nghttp2.org/ HTTP 1.1 HTTP/2 http header size 42% ‑

  24. • Nginx ≥ 1.9.5 • Add “http2” in config Enable

    HTTP/2 for Nginx http://nginx.org/en/docs/http/ngx_http_v2_module.html
  25. HTTP/2 key differences ✓binary, instead of textual ✓header compression to

    reduce overhead • fully multiplexed, instead of ordered and blocking • can use one connection for parallelism • server “push” hWps://hWp2.github.io/faq/#what-are-the-key-differences-to-hWp1x
  26. https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md gRPC over HTTP2

  27. gRPC app Protobuf HTTP/2 HTTP 1.1 TCP + TLS 1.2

    IP HTTP app gRPC Protocol Buffers + = HTTP/2
  28. Demo #2 Protocol buffers 服務治理模型

  29. HTTP/2 HTTP 1.1 TCP + TLS 1.2 IP HTTP app

    Content-Type: xxx/yyy Too free style…
  30. IDL Swagger Client Server Codegen Client SDK Server stub App

    REST with Design-First API Strategy java -jar swagger-codegen-cli …
  31. IDL .proto Client Server Codegen Client API Server API App

    protoc XXX.proto --go_out=plugins=grpc:. gRPC
  32. Protocol buffers and competitors 2008 Protobuf 2 by Google Apache

    Thrip by Facebook 2009 Apache Avro by Hadoop 2016 Protobuf 3 by Google gRPC 1.0 by Google 2017 gRPC donated to CNCF
  33. IDL .proto Client Server Codegen Client API Server API App

    protoc XXX.proto --go_out=plugins=grpc:. gRPC programming flow hWps://grpc.io/docs/what-is-grpc/core-concepts/#synchronous-vs-asynchronous sync or async
  34. IDL .proto Server Mock App grpcurl Server Mock gRPC dev/test

    with mock & grpcurl 50051 10000 hWps://github.com/William-Yeh/grpcurl-and-ghz-demo
  35. IDL .proto route_guide.proto

  36. @google https://developers.google.com/protocol-buffers/docs/proto3

  37. IDL .proto Mock Server Mock 50051 docker run -it \

    --name mock --rm -p 50051:50051 \ -v $(pwd)/routeguide:/proto \ -v $(pwd)/mock:/mock \ ghcr.io/william-yeh/grpc-mock /mock/mock.js mock.js
  38. IDL .proto Mock grpcurl Server Mock 50051 grpcurl -plaintext \

    -d '{"latitude":407838351, "longitude":-746143763}' \ -import-path ./routeguide \ -proto route_guide.proto \ 127.0.0.1:50051 \ routeguide.RouteGuide.GetFeature
  39. IDL .proto App server.go out/server Server 10000 Server API protoc

    XXX.proto --go_out=plugins=grpc:. Codegen xxx.pb.go
  40. grpcurl -plaintext \ -d '{"latitude":407838351, "longitude":-746143763}' \ -import-path ./routeguide \

    -proto route_guide.proto \ 127.0.0.1:10000 \ routeguide.RouteGuide.GetFeature IDL .proto Server grpcurl 10000
  41. Protobuf message

  42. IDL .proto Server grpcurl 10000 Try to input invalid message?

  43. Server Server Mock 50051 10000 IDL Mock App .proto Differences

    Header Reflection . . .
  44. gRPC app Protobuf HTTP/2 HTTP 1.1 TCP + TLS 1.2

    IP HTTP app gRPC Protocol Buffers + = HTTP/2
  45. gRPC benefits ✓ binary, instead of textual ✓ header compression

    to reduce overhead • fully multiplexed, instead of ordered and blocking • can use one connection for parallelism • server “push” ✓ binary marshaling format ✓ IDL ✓ language neutral HTTP/2 Protobuf • Bidirectional streaming • Deadline
  46. gRPC with Code-First Approach

  47. IDL Swagger Client Server Codegen Client SDK Server stub App

    REST java -jar swagger-codegen-cli …
  48. Client Server Client API Server API App Code-first example Java

    source code with annotation Swagger2 (+ Spring Boot)
  49. IDL .proto Client Server Codegen Client API Server API App

    protoc XXX.proto --go_out=plugins=grpc:. gRPC
  50. Client Server Client API Server API App Code-first example C#

    source code with annotation protobuf-net.Grpc https://github.com/protobuf-net/protobuf-net.Grpc
  51. gRPC in the real world

  52. gRPC app Protobuf HTTP/2 HTTP 1.1 TCP + TLS 1.2

    IP HTTP app
  53. Server gRPC Server Server gRPC:更⾼效的微服務介⾯ ⾼效的傳輸介⾯ 服務治理模型

  54. Server REST gRPC Server Server Desktop app Mobile app ?

    ? Web browser ? ? Server ? ❶ ❷ ❸ ❸ ❸ Beyond microservices?
  55. Server REST gRPC Server Server Mobile app ? Question ❶

  56. hWps://grpc.io/docs/languages/ Mobile app (up to 2020-09-19) Swift https://github.com/grpc/grpc-swift Swift 5.0+

  57. gRPC mobile app Protobuf HTTP/2 TCP + TLS 1.2 UDP

    HTTP/3 IP QUIC + TLS 1.3 Challenge! Rescue! Connection migration
  58. Server gRPC Server Server Web browser ? Question ❷

  59. Server gRPC Server Server grpc-web proxy Web browser grpc-web client

    stub hWps://github.com/grpc/grpc-web Limited gRPC functionalities e.g., Envoy
  60. gRPC-Web Roadmap Features • Non-Binary Message Encoding • Streaming Support

    • Bidi Streaming • Security • In-process Proxies hWps://github.com/grpc/grpc-web/blob/master/doc/roadmap.md • Web Framework Integration • TypeScript Support • Non-Closure compiler support • Web UI Support
  61. Server REST gRPC Server Server Mobile app ? Web browser

    ? Server ? Question ❸
  62. Server REST gRPC Server Server Server grpc-gateway Mobile app Web

    browser Solution #1
  63. IDL .proto Server Codegen Reverse proxy code App protoc XXX.proto

    --grpc-gateway_out=. grpc-gateway hWps://github.com/grpc-ecosystem/grpc-gateway protoc XXX.proto --swagger_out=.
  64. gRPC gateway - proto example

  65. REST gRPC Server Server Server Server API gateway Mobile app

    Web browser Solution #2 Off-the-shelf software Custom software
  66. All-in-one SeparaGon of Concerns 左右擺盪 Client-side gRPC-gateway API gateway

  67. via gRPC via REST Mobile app ✔ (use native lib)

    grpc-gateway api gateway Web client ⁉ grpc-web grpc-gateway api gateway Desktop client or other servers ✔ (use native lib) grpc-gateway api gateway Summary: How to communicate with gRPC servers?
  68. Conclusion

  69. Is REST still good enough in the microservice world?

  70. 簡易 性能 gRPC 過於簡單的模型, ⽋缺基本的服務治理。 過於陽春的 HTTP, ⽋缺⾼效的傳輸介⾯。 HTTP/2 Protocol

    Buffers + = REST
  71. gRPC app Protobuf HTTP/2 HTTP 1.1 TCP + TLS 1.2

    IP HTTP app
  72. gRPC benefits ✓ binary, instead of textual ✓ header compression

    to reduce overhead • fully multiplexed, instead of ordered and blocking • can use one connection for parallelism • server “push” ✓ binary marshaling format ✓ IDL ✓ language neutral HTTP/2 Protobuf • Bidirectional streaming • Deadline
  73. • Efficient for inter-process communication • Simple, well-defined service interfaces

    and schema • Strongly typed • Polyglot • Duplex streaming • Built-in commodity features • Integrated with cloud native ecosystems • Mature and has been widely adopted • It may not be suitable for external-facing services • Drastic service definition changes are a complicated development process • The ecosystem is relatively small Pros Cons gRPC: Up and Running https://www.amazon.com/dp/1492058335
  74. Server REST gRPC Server Server Desktop app Mobile app ?

    ? Web browser ? ? Server ? ❶ ❷ ❸ ❸ ❸ Beyond microservices?
  75. via gRPC via REST Mobile app ✔ (use native lib)

    grpc-gateway api gateway Web client ⁉ grpc-web grpc-gateway api gateway Desktop client or other servers ✔ (use native lib) grpc-gateway api gateway Summary: How to communicate with gRPC servers?
  76. None
  77. • Newton's Cradle by Rflor from the Noun Project Attribution