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

gRPC:更高效的微服務介面

William Yeh
September 25, 2020

 gRPC:更高效的微服務介面

gRPC: a more efficient microservice interface.

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

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

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

William Yeh

September 25, 2020
Tweet

More Decks by William Yeh

Other Decks in Programming

Transcript

  1. 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
  2. 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.
  3. 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!
  4. • Nginx ≥ 1.9.5 • Add “http2” in config Enable

    HTTP/2 for Nginx http://nginx.org/en/docs/http/ngx_http_v2_module.html
  5. 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
  6. gRPC app Protobuf HTTP/2 HTTP 1.1 TCP + TLS 1.2

    IP HTTP app gRPC Protocol Buffers + = HTTP/2
  7. HTTP/2 HTTP 1.1 TCP + TLS 1.2 IP HTTP app

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

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

    protoc XXX.proto --go_out=plugins=grpc:. gRPC
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. IDL .proto App server.go out/server Server 10000 Server API protoc

    XXX.proto --go_out=plugins=grpc:. Codegen xxx.pb.go
  16. 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
  17. gRPC app Protobuf HTTP/2 HTTP 1.1 TCP + TLS 1.2

    IP HTTP app gRPC Protocol Buffers + = HTTP/2
  18. 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
  19. IDL Swagger Client Server Codegen Client SDK Server stub App

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

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

    protoc XXX.proto --go_out=plugins=grpc:. gRPC
  22. 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
  23. Server REST gRPC Server Server Desktop app Mobile app ?

    ? Web browser ? ? Server ? ❶ ❷ ❸ ❸ ❸ Beyond microservices?
  24. gRPC mobile app Protobuf HTTP/2 TCP + TLS 1.2 UDP

    HTTP/3 IP QUIC + TLS 1.3 Challenge! Rescue! Connection migration
  25. 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
  26. 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
  27. 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=.
  28. REST gRPC Server Server Server Server API gateway Mobile app

    Web browser Solution #2 Off-the-shelf software Custom software
  29. 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?
  30. 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
  31. • 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
  32. Server REST gRPC Server Server Desktop app Mobile app ?

    ? Web browser ? ? Server ? ❶ ❷ ❸ ❸ ❸ Beyond microservices?
  33. 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?