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

Beyond HTTP in ASP.NET Core 3 with gRPC

Steve Gordon
September 11, 2019

Beyond HTTP in ASP.NET Core 3 with gRPC

In this session, Steve will share gRPC, a modern high-performance RPC framework for inter-service communication. You'll discover how gRPC stacks up against existing REST-based communication and explore the benefits this technology has to offer, for example, its use of HTTP/2 as its transport protocol and ProtoBuf encoded content for efficient, fast communication.

You'll explore C# examples of how to build client and server gRPC components using ASP.NET Core 3.0, using the blazingly fast Kestrel web server. You'll learn how to define your proto3 service definition (contract) and integrate with MSBuild tooling to code-gen the server and client components you'll need. You'll be amazed at how quickly a service can be created with little effort or boilerplate code required. You'll also see how additional clients can easily be generated for other languages such as Node.js to make connecting polyglot systems a breeze.

It's a great time to begin taking notice of gRPC as a worthy contender to take over from REST for inter-service communication in microservice-based environments. It's time to start letting service talk their own language!

Steve Gordon

September 11, 2019
Tweet

More Decks by Steve Gordon

Other Decks in Technology

Transcript

  1. BEYOND HTTP IN ASP.NET
    CORE 3.0 WITH gRPC
    Steve Gordon
    @stevejgordon | stevejgordon.co.uk
    http://bit.ly/dotnet-grpc

    View full-size slide

  2. FULL DISCLOSURE

    View full-size slide

  3. STORY TIME
    ARE YOU SITTING COMFORTABLY?

    View full-size slide

  4. @stevejgordon
    www.stevejgordon.co.uk
    HTTP 3rd Party API

    View full-size slide

  5. WHAT HAPPENED NEXT?...

    View full-size slide

  6. …BUZZWORDS

    View full-size slide

  7. @stevejgordon
    www.stevejgordon.co.uk
    3rd Party API
    3rd Party API
    3rd Party API

    View full-size slide

  8. WHAT IS WRONG
    WITH REST?

    View full-size slide

  9. gRPC REMOTE PROCEDURE CALL

    View full-size slide

  10. @stevejgordon
    www.stevejgordon.co.uk
    • Contract based
    • Supports code generation
    • Multi-platform
    C#, C++, Dart, Go, Java, Node, Objective-C, PHP, Python, Ruby
    • HTTP/2
    • Uses a binary, Protobuf payload

    View full-size slide

  11. @stevejgordon
    www.stevejgordon.co.uk
    2001
    Google Stubby
    March 2015
    gRPC open
    sourced
    August 2016
    gRPC v1.0.0
    Sept 2019
    gRPC .NET with
    .NET Core 3.0

    View full-size slide

  12. @stevejgordon
    www.stevejgordon.co.uk
    Getting Started
    Create a new project
    Select 'gRPC Service'

    View full-size slide

  13. @stevejgordon
    www.stevejgordon.co.uk
    Project Structure

    View full-size slide

  14. @stevejgordon
    www.stevejgordon.co.uk
    syntax = "proto3";
    option csharp_namespace = "HelloWorld";
    package Greet;
    service Greeter {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    message HelloRequest {
    string name = 1;
    }
    message HelloReply {
    string message = 1;
    }

    View full-size slide

  15. @stevejgordon
    www.stevejgordon.co.uk
    syntax = "proto3";
    option csharp_namespace = "HelloWorld";
    package Greet;
    service Greeter {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    message HelloRequest {
    string name = 1;
    }
    message HelloReply {
    string message = 1;
    }

    View full-size slide

  16. @stevejgordon
    www.stevejgordon.co.uk
    syntax = "proto3";
    option csharp_namespace = "HelloWorld";
    package Greet;
    service Greeter {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    message HelloRequest {
    string name = 1;
    }
    message HelloReply {
    string message = 1;
    }

    View full-size slide

  17. @stevejgordon
    www.stevejgordon.co.uk
    syntax = "proto3";
    option csharp_namespace = "HelloWorld";
    package Greet;
    service Greeter {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    message HelloRequest {
    string name = 1;
    }
    message HelloReply {
    string message = 1;
    }

    View full-size slide

  18. @stevejgordon
    www.stevejgordon.co.uk
    syntax = "proto3";
    option csharp_namespace = "HelloWorld";
    package Greet;
    service Greeter {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    message HelloRequest {
    string name = 1;
    }
    message HelloReply {
    string message = 1;
    }

    View full-size slide

  19. GETTING STARTED DEMO

    View full-size slide

  20. gRPC & MICROSERVICES

    View full-size slide

  21. @stevejgordon
    www.stevejgordon.co.uk
    SERVICE B (NODE) SERVICE C (.NET)
    SERVER
    SERVICE A (.NET)
    SERVER
    CLIENT
    SERVICE D (GO)
    SERVER
    CLIENT
    CLIENT
    PROTO A
    PROTO A
    PROTO B
    PROTO B
    PROTO C
    PROTO C

    View full-size slide

  22. @stevejgordon
    www.stevejgordon.co.uk
    • Unary
    Single request with a single response – A normal function call
    • Server Streaming
    Single request with a stream of responses for the client to consume
    • Client Streaming
    Client sends a stream of requests, each receiving a single response
    • Bidirectional Streaming
    Client sends a stream of requests and consumes a stream of responses

    View full-size slide

  23. STREAMING DEMOS

    View full-size slide

  24. REST Body
    10,072 Bytes
    GRPC Body
    1,916 Bytes
    RESULTS
    80.98% SMALLER

    View full-size slide

  25. @stevejgordon
    www.stevejgordon.co.uk
    • No need for *.proto
    • Apply attributes to .NET interfaces and classes
    • Interface(s) define your services
    • Classes define your message models
    • Can also use a *.proto to generate the C# code
    • Share contracts as libraries between .NET servers and clients

    View full-size slide

  26. CODE-FIRST gRPC DEMO

    View full-size slide

  27. THE BIG FINISH DEMO!

    View full-size slide

  28. @stevejgordon
    www.stevejgordon.co.uk
    ASP.NET Core
    Service (.NET)
    CLIENT
    weather.proto
    gRPC Weather
    Service (.NET)
    SERVER
    weather.proto
    HTTP
    SignalR
    HTTP gRPC

    View full-size slide

  29. @stevejgordon
    www.stevejgordon.co.uk
    • Performance
    • Efficient binary serialisation (low CPU overhead) using Protobuf
    • Smaller payloads
    • HTTP/2 is fundamentally quicker
    • Code-gen server and clients for many languages/frameworks
    • Contract based
    • Supports first class streaming

    View full-size slide

  30. @stevejgordon
    www.stevejgordon.co.uk
    • Code generation in .NET is C# only
    • Kestrel doesn't support HTTP/2 with TLS on macOS
    https://docs.microsoft.com/en-us/aspnet/core/grpc/aspnetcore?view=aspnetcore-3.0&tabs=visual-studio#grpc-and-aspnet-core-on-macos
    • Harder to exercise without tools such as Postman

    View full-size slide

  31. @stevejgordon
    www.stevejgordon.co.uk
    • A great choice for microservice communication
    • A good way to support server-to-server communication in
    polyglot environments
    • Easy to get started using the gRPC service template
    • gRPC .NET libraries make creating servers and clients very easy
    https://github.com/grpc/grpc-dotnet

    View full-size slide

  32. THANK YOU
    @stevejgordon
    www.stevejgordon.co.uk
    https://app.pluralsight.com/profile/author/steve-gordon
    http://bit.ly/dotnet-grpc

    View full-size slide