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

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

Other Decks in Programming

Transcript

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

    View Slide

  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

    View Slide

  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.

    View Slide

  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!

    View Slide

  5. 簡易 性能
    左右擺盪

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  11. 簡易
    REST
    過於簡單的模型,
    ⽋缺基本的服務治理。
    過於陽春的 HTTP,
    ⽋缺⾼效的傳輸介⾯。
    HTTP 1.1
    Swagger
    WebSocket
    OpenAPI
    缺點 ➠改進
    (個別)

    View Slide

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

    View Slide

  13. 簡易 性能
    左右擺盪
    gRPC
    過於簡單的模型,
    ⽋缺基本的服務治理。
    過於陽春的 HTTP,
    ⽋缺⾼效的傳輸介⾯。
    HTTP/2
    Protocol Buffers
    +
    =
    REST
    (綜合)
    ?

    View Slide

  14. 簡易 性能
    左右擺盪
    REST Unix RPC
    Java RMI
    SOAP Web Service
    CORBA
    gRPC

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  26. https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
    gRPC over HTTP2

    View Slide

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

    View Slide

  28. Demo #2
    Protocol buffers
    服務治理模型

    View Slide

  29. HTTP/2
    HTTP 1.1
    TCP + TLS 1.2
    IP
    HTTP app
    Content-Type: xxx/yyy
    Too free style…

    View Slide

  30. IDL Swagger
    Client Server
    Codegen
    Client SDK Server stub
    App
    REST with Design-First API Strategy
    java -jar swagger-codegen-cli …

    View Slide

  31. IDL .proto
    Client Server
    Codegen
    Client API Server API
    App
    protoc XXX.proto --go_out=plugins=grpc:.
    gRPC

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  35. IDL .proto
    route_guide.proto

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  39. IDL .proto
    App server.go
    out/server Server
    10000
    Server API
    protoc XXX.proto --go_out=plugins=grpc:.
    Codegen
    xxx.pb.go

    View Slide

  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

    View Slide

  41. Protobuf message

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  46. gRPC with Code-First Approach

    View Slide

  47. IDL Swagger
    Client Server
    Codegen
    Client SDK Server stub
    App
    REST
    java -jar swagger-codegen-cli …

    View Slide

  48. Client Server
    Client API Server API
    App
    Code-first example
    Java source code
    with annotation
    Swagger2
    (+ Spring Boot)

    View Slide

  49. IDL .proto
    Client Server
    Codegen
    Client API Server API
    App
    protoc XXX.proto --go_out=plugins=grpc:.
    gRPC

    View Slide

  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

    View Slide

  51. gRPC in the real world

    View Slide

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

    View Slide

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

    View Slide

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



    ❸ ❸
    Beyond microservices?

    View Slide

  55. Server
    REST
    gRPC
    Server
    Server
    Mobile
    app ?
    Question ❶

    View Slide

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

    View Slide

  57. gRPC mobile app
    Protobuf
    HTTP/2
    TCP + TLS 1.2
    UDP
    HTTP/3
    IP
    QUIC +
    TLS 1.3
    Challenge!
    Rescue!
    Connection migration

    View Slide

  58. Server
    gRPC
    Server
    Server
    Web
    browser ?
    Question ❷

    View Slide

  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

    View Slide

  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

    View Slide

  61. Server
    REST
    gRPC
    Server
    Server
    Mobile
    app
    ?
    Web
    browser
    ?
    Server
    ?
    Question ❸

    View Slide

  62. Server
    REST
    gRPC
    Server
    Server
    Server
    grpc-gateway
    Mobile
    app
    Web
    browser
    Solution #1

    View Slide

  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=.

    View Slide

  64. gRPC gateway - proto example

    View Slide

  65. REST
    gRPC
    Server
    Server
    Server
    Server
    API gateway
    Mobile
    app
    Web
    browser
    Solution #2
    Off-the-shelf software
    Custom software

    View Slide

  66. All-in-one SeparaGon of
    Concerns
    左右擺盪
    Client-side
    gRPC-gateway
    API gateway

    View Slide

  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?

    View Slide

  68. Conclusion

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  74. Server
    REST
    gRPC
    Server
    Server
    Desktop
    app
    Mobile
    app
    ?
    ?
    Web
    browser
    ?
    ?
    Server
    ?



    ❸ ❸
    Beyond microservices?

    View Slide

  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?

    View Slide

  76. View Slide

  77. • Newton's Cradle by Rflor from the Noun
    Project
    Attribution

    View Slide