Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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!

Slide 5

Slide 5 text

簡易 性能 左右擺盪

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Is REST still good enough in the microservice world?

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

• Nginx ≥ 1.9.5 • Add “http2” in config Enable HTTP/2 for Nginx http://nginx.org/en/docs/http/ngx_http_v2_module.html

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Demo #2 Protocol buffers 服務治理模型

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

IDL .proto route_guide.proto

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Protobuf message

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

gRPC with Code-First Approach

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

gRPC in the real world

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

Server REST gRPC Server Server Desktop app Mobile app ? ? Web browser ? ? Server ? ❶ ❷ ❸ ❸ ❸ Beyond microservices?

Slide 55

Slide 55 text

Server REST gRPC Server Server Mobile app ? Question ❶

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

Server gRPC Server Server Web browser ? Question ❷

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

gRPC gateway - proto example

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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?

Slide 68

Slide 68 text

Conclusion

Slide 69

Slide 69 text

Is REST still good enough in the microservice world?

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

• 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

Slide 74

Slide 74 text

Server REST gRPC Server Server Desktop app Mobile app ? ? Web browser ? ? Server ? ❶ ❷ ❸ ❸ ❸ Beyond microservices?

Slide 75

Slide 75 text

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?

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

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