Slide 1

Slide 1 text

qaware.de Von REST zu gRPC und zurück mit Go betterCode(Go) Mario-Leander Reimer mario-leander.reimer@qaware.de @LeanderReimer

Slide 2

Slide 2 text

2 Mario-Leander Reimer Principal Software Architect @LeanderReimer #cloudnativenerd #qaware

Slide 3

Slide 3 text

Agenda QAware | 3 QAware | 3 REST Beer Service REST Beer Services GET /api/beers POST /api/beers GET /api/beers/{asin} PUT /api/beers/{asin} DELETE /api/beers/{asin} application/json application/x-protobuf gRPC Beer Service gRPC Beer Service gRPC Beer Web UI gRPC Beer Client gRPC Beer Gateway application/json gRPC Beer Nginx gRPC gRPC gRPC gRPC gRPC Web UI

Slide 4

Slide 4 text

https://github.com/lreimer/from-rest-to-grpc

Slide 5

Slide 5 text

“Man kann nicht nicht kommunizieren.” - Paul Watzlawick

Slide 6

Slide 6 text

A Quick History Lesson on Inter Process Communication (IPC) QAware | 6 DCOM 18.09.1996 Win95 RPC 14.01.1976 RFC 707 REST 2000 by Roy T. Fielding Java RMI Feb 1997 JDK 1.1 HTTP/1.0 Mai 1996 RFC 1945 HTTP/1.1 Juni 1999 RFC 2616 HTTP/2.0 Mai 2015 RFC 7540 SOAP 1.2 2003 RPC Oct 1983 Birrel und Nielson CORBA 1.0 Oct 1991 CORBA 2.0 August 1996 CORBA 2.3 Juni 1999 XML-RPC 1998 gRPC 1.0 Aug 2016 RESTful Applications 2014 (?) CORBA 3.0 July 2002

Slide 7

Slide 7 text

HTTP/1.1 200 OK Content-Length: 139 Content-Type: application/json; charset=utf-8 Date: Wed, 10 Nov 2021 10:21:54 GMT { "alcohol": 5.6, "asin": "B01AU6LWNC", "brand": "Augustiner Brauerei München", "country": "Germany", "name": "Edelstoff Exportbier", "type": "Lager" } GET /api/beers/B01AU6LWNC HTTP/1.1 Accept: application/json Accept-Encoding: gzip, deflate Connection: keep-alive Host: localhost:8080 User-Agent: HTTPie/2.5.0 REST APIs GET /api/beers POST /api/beers GET /api/beers/{asin} PUT /api/beers/{asin} DELETE /api/beers/{asin}

Slide 8

Slide 8 text

Richardson REST Maturity Model QAware | 8 https://martinfowler.com/articles/richardsonMaturityModel.html POST /bookingService HTTP/1.1 [various other headers] POST /bookings HTTP/1.1 [various other headers] GET /bookings/1234567890?user=lreimer HTTP/1.1 Accept: application/json [various other headers] GET /bookings/1234567890?user=lreimer HTTP/1.1 Accept: application/json Link: /users/lreimer [various other headers]

Slide 9

Slide 9 text

QAware | 9 1. The network is reliable 2. Latency is zero 3. Bandwidth is infinite 4. The network is secure 5. Topology doesn’t change 6. There is one administrator 7. Transport cost is zero 8. The network is homogeneous The 8 Fallacies of Distributed Computing

Slide 10

Slide 10 text

Protocol Buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data. QAware | 10 ■ https://developers.google.com/protocol-buffers ■ Wie XML oder JSON - nur kleiner, schneller und einfacher! ■ Protobuf verwendet ein effizientes binäres Format. ■ Die Definition von Datenstrukturen und Nachrichten erfolgt per Interface Definition Language (IDL) ■ Protocol Buffers unterstützt die Code-Generierung für Java, Python, Objective-C, C++, Kotlin, Dart, Go, Ruby und C#. ■ Protobuf unterstützt Schema Evolution und Erweiterung. Backward- und Forward-Compatibility werden unterstützt. – you must not change the tag numbers of any existing fields. – you may delete fields. – you may add new fields but you must use fresh tag numbers (i.e. tag numbers that were never used in this protocol buffer, not even by deleted fields).

Slide 11

Slide 11 text

syntax = "proto3"; option go_package = "github.com/lreimer/from-rest-to-grpc/proto"; package beer; message Beer { string asin = 1; string name = 2; string brand = 3; string country = 4; float alcohol = 5; enum BeerType{ IndianPaleAle = 0; SessionIpa = 1; Lager = 2; } BeerType type = 6; } // Protobuf marshalling beer := &pb.Beer{} out, err := proto.Marshal(beer) if err != nil { log.Fatalln("Failed to encode beer:", err) } // Protobuf unmarshalling beer := &pb.Beer{} if err := proto.Unmarshal(in, beer); err != nil { log.Fatalln("Failed to parse beer:", err) } $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest $ protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/beer.proto

Slide 12

Slide 12 text

gRPC. A modern, high performance, open source and universal RPC framework. ■ Nutzt HTTP/2 als modernes Web-friendly Transportprotokoll (Multiplexing, TLS, Compression, …) ■ Unterstützt zahlreiche Kommunikationsarten: Request-Response und Streaming (Client-side, Server-side, Uni- und Bi-Directional) ■ Nutzt Protocol Buffers als binäres und effizientes Nachrichten-Format ■ Zahlreiche Load-Balancing Optionen: Proxy, Client Sode und Lookaside Balancing ■ Flexible Unterstützung für Tracing, Health Checking und Authentication ■ Client und Server Code in zahlreichen Sprachen können bequem aus der IDL generiert werden – https://github.com/grpc/grpc-go – https://buf.build ■ gRPC Gateway und gRPC Web aus dem Ecosystem sorgen für gute Interoperabilität im Web – https://grpc-ecosystem.github.io/grpc-gateway/ – https://grpc.io/docs/platforms/web/quickstart/ ■ grPC ist ein CNCF Incubating Project. QAware | 12

Slide 13

Slide 13 text

syntax = "proto3"; option go_package = "github.com/lreimer/from-rest-to-grpc/grpc-beer-service/proto"; import "google/protobuf/empty.proto"; package beer; service BeerService { rpc AllBeers (google.protobuf.Empty) returns (GetBeersResponse) {} rpc GetBeer (GetBeerRequest) returns (GetBeerResponse) {} rpc CreateBeer (CreateBeerRequest) returns (google.protobuf.Empty) {} rpc UpdateBeer (UpdateBeerRequest) returns (google.protobuf.Empty) {} rpc DeleteBeer (DeleteBeerRequest) returns (google.protobuf.Empty) {} } // more Protobuf message definitions ... $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

Slide 14

Slide 14 text

qaware.de QAware GmbH Aschauer Straße 32 81549 München Tel. +49 89 232315-0 info@qaware.de twitter.com/qaware linkedin.com/company/qaware-gmbh xing.com/companies/qawaregmbh slideshare.net/qaware github.com/qaware