Microservices Communication in .NET Using gRPC: A practical guide for .NET developers to build efficient communication mechanism for distributed apps
- Length: 486 pages
- Edition: 1
- Language: English
- Publisher: Packt Publishing
- Publication Date: 2022-02-11
- ISBN-10: 1803236434
- ISBN-13: 9781803236438
- Sales Rank: #174704 (See Top 100 Books)
Learn how to implement gRPC on the .NET platform step by step and cover how to use gRPC on .NET, including fundamentals, use cases, and best practices
Key Features
- Explore all aspects of gRPC implementation on .NET, from the most basic features to advanced ones
- Discover best practices for using gRPC to make sure that your applications are as efficient and as scalable as possible
- Identify when gRPC is the best tool for the job and when it isn’t
Book Description
Explore gRPC’s capabilities for faster communication between your microservices using the HTTP/2 protocol in this practical guide that shows you how to implement gRPC on the .NET platform.
gRPC is one of the most efficient protocols for communication between microservices that is also relatively easy to implement. However, its official documentation is often fragmented and.NET developers might find it difficult to recognize the best way to map between C# data types and fields in gRPC messages. This book will address these concerns and much more. Starting with the fundamentals of gRPC, you’ll discover how to use it inside .NET apps. You’ll explore best practices for performance and focus on scaling a gRPC app. Once you’re familiar with the inner workings of the different call types that gRPC supports, you’ll advance to learning how to secure your gRPC endpoints by applying authentication and authorization.
With detailed explanations, this gRPC .NET book will show you how the Protobuf protocol allows you to send messages efficiently by including only the necessary data. You’ll never get confused again while translating between C# data types and the ones available in Protobuf.
By the end of the book, you’ll have gained practical gRPC knowledge and be able to use it in .NET apps to enable direct communication between microservices.
What you will learn
- Get to grips with the fundamentals of gRPC and Protobuf
- Debug gRPC components inside a .NET application to locate and fix errors
- Understand gRPC best practices, such as performance enhancement
- Effectively translate between gRPC and native C# code by applying well-known types
- Secure gRPC communication inside a .NET application
- Discover how to monitor gRPC on .NET by applying logging and metrics
Who this book is for
This book is for NET developers who are working with microservices and are looking for efficient solutions to facilitate communication between services using gRPC. Anyone who is familiar with microservices architecture and has knowledge of the fundamentals of .NET Core, but not necessarily of gRPC, will also find this book useful.
Microservices Communication in .NET Using gRPC Contributors About the author About the reviewer Preface Who this book is for What this book covers To get the most out of this book Download the example code files Code in Action Download the color images Conventions used Get in touch Share Your Thoughts Section 1: Basics of gRPC on .NET Chapter 1: Creating a Basic gRPC Application on ASP.NET Core Technical requirements Introduction to gRPC gRPC on ASP.NET Core Using gRPC in your own distributed ASP.NET Core application Preparing your system Setting up your environment on Windows Setting up your environment on Mac Setting up your environment on Linux Downloading the .NET SDK (all operating systems) Setting up a gRPC server Initializing an ASP.NET Core project via an IDE Adding gRPC server components to an ASP.NET Core project Adding some code to use gRPC components Setting up a gRPC client Initializing the project for the client application Adding gRPC client components to the application Applying gRPC client components to the code Understanding how proto files generate C# code Where is auto-generated code stored? Modifying Protobuf namespaces Sharing a proto file between the client and the server Creating a shared class library Adding shared gRPC components to the class library Sharing gRPC dependencies between different projects Running a gRPC service on Mac Configuring server-side components Modifying the client-side configuration Summary Questions Further reading Chapter 2: When gRPC Is the Best Tool and When It Isn't Technical requirements Why gRPC is a great tool for microservices Setting up a solution and shared dependencies Setting up the status manager microservice Setting up a REST API gateway service Launching the distributed application How gRPC can be a good tool for asynchronous communication Adding client-streaming and server-streaming gRPC endpoints Configuring the gRPC client for asynchronous communication Testing asynchronous gRPC endpoints Why gRPC is not the best tool for browsers Setting up a Blazor WebAssembly gRPC client Modifying the gRPC server to enable gRPC-Web Launching the gRPC-Web application Where SignalR would beat gRPC Setting up a SignalR application Adding a SignalR client and launching the application Summary Questions Further reading Chapter 3: Protobuf – the Communication Protocol of gRPC Technical requirements The RPC types supported by gRPC The RPC types that Protobuf supports Making comments in Protobuf Reviewing the native Protobuf data types Integer data types Non-integer numeric types Non-numeric data types Enums Nested messages Using collections in Protobuf Repeated fields Map fields Using special keywords in Protobuf How the oneof keyword can make communication more efficient Customizing the behavior with the option keyword Referencing other proto files Importing external proto packages Referencing internal proto files Using proto files as relays Summary Questions Further reading Section 2: Best Practices of Using gRPC Chapter 4: Performance Best Practices for Using gRPC on .NET Technical requirements Why you need to reuse a gRPC channel Setting up the server application Setting up the client application Comparing the performance of different client types How to not get held up by a concurrent stream limit Configuring connection concurrency on the gRPC client Comparing the performance between a single connection and multiple connections Ensuring that your connection remains alive Setting up keep-alive pings on the gRPC client When streaming is better than individual calls Setting up a bi-directional streaming RPC Monitoring the performance of the bi-directional streaming call Using binary payloads to decrease the data's size Adding binary fields to Protobuf Summary Questions Further reading Chapter 5: Applying Versioning to the gRPC API Technical requirements Why an API versioning strategy is important Creating a server application Implementing the server-side gRPC components Creating the gRPC client application Implementing the gRPC client logic Verifying that the client can talk to the server What the sequence numbers in the proto file represent Modifying the Protobuf definition in the server application Modifying the Protobuf definition in the client application Launching modified applications Why you must not modify existing fields in future Protobuf versions Modifying Protobuf definitions on the client side Launching the applications Making further changes to the client application Re-launching the applications How to deprecate old, unused fields in gRPC Applying the reserved keyword to the server-side Protobuf interface Testing the application How to factor in API versioning at the design stage Adding multiple Protobuf versions to the server application Allowing the server application to use multiple Protobuf versions Making the gRPC client implementation version-specific Making a gRPC call to a versioned endpoint Summary Questions Further reading Chapter 6: Scaling a gRPC Application Technical requirements Introduction to load balancing Adding shared gRPC dependencies Creating a shared library for server-side application instances Creating multiple instances of the server-side application Creating a client application Running a load-balanced application Client-side load balancing with gRPC Updating the NuGet package Enabling client-side load balancing components Enabling a DNS resolver for the load balancer Using a static resolver for the load balancer Creating custom load balancers and resolvers Proxy load balancing with gRPC Building a web application to act as a proxy Launching the HTTP/2 proxy Summary Questions Further reading Section 3: In-Depth Look at gRPC on .NET Chapter 7: Using Different Call Types Supported by gRPC Technical requirements Making unary calls on gRPC Setting up shared gRPC dependencies Creating server-side implementations of the Protobuf definitions Building the gRPC client Applying different types of client-side call implementations Using gRPC dependencies in the client application Testing different types of unary call endpoints Streaming data from the client Adding a client-streaming call to the server application Adding client logic for a client-streaming gRPC call Reading streams from the server Adding a server-streaming RPC to Protobuf Setting up a server-streaming call on the server side Making a server-streaming call from a gRPC client Enabling bi-directional streaming Enabling server-side components for bi-directional streaming Adding a client-side implementation of a bi-directional streaming call Testing how to stream gRPC calls Summary Questions Further reading Chapter 8: Using Well-Known Types to Make Protobuf More Handy Technical requirements Using nullable types in Protobuf Setting up a gRPC server application Examining auto-generated code for wrapper fields Adding logic to gRPC server application Setting up shared dependencies Setting up the gRPC client Running the application Using dates and times in Protobuf Adding timestamp and duration to the server Applying changes to the gRPC client and launching the app Exchanging empty messages Adding the Empty data type to the server-side application Applying an Empty object on the client Using loosely typed fields in a Protobuf message Adding Any and Value data types to the gRPC server Populating the Any and Value fields from the gRPC client Summary Questions Further reading Chapter 9: Securing gRPC Endpoints in Your ASP.NET Core Application with SSL/TLS Technical requirements Configuring the gRPC client and server for unencrypted communication The role of TLS certificates Setting up a gRPC service application Removing TLS on both HTTP/1.1 and HTTP/2 Exposing Protobuf definitions to clients Building the client for gRPC communication Adding the remaining client logic Creating and trusting a self-signed certificate The basics of a TLS certificate Trusting a default development certificate Creating a self-signed certificate on Windows using PowerShell Creating a self-signed certificate on Unix using OpenSSL Applying a certificate on ASP.NET Core Testing custom certificates and HTTPS redirection Applying certificate authentication on the gRPC client and server Configuring the gRPC server for certificate authentication Enabling certificate authentication on the gRPC client Testing certificate authentication Summary Questions Further reading Chapter 10: Applying Authentication and Authorization to gRPC Endpoints Technical requirements Setting up the authentication backend OpenID Connect and OAuth flow Configuring IdentityServer4 Adding SSO users, roles, and clients Forcing login redirect on a web application Restricting gRPC endpoints to authenticated users Setting up shared gRPC dependencies Setting up the gRPC server Enabling gRPC client functionality Restricting endpoints to authorized users only Configuring SSO provider to insert role claim into the JWT Applying different authorization rules to different gRPC endpoints Applying gRPC client changes Summary Questions Further reading Chapter 11: Using Logging, Metrics, and Debugging in gRPC on .NET Technical requirements Debugging gRPC client components inside a .NET application Setting up shared gRPC dependencies Adding a gRPC service application and getting it to display detailed errors Adding a gRPC client with additional debugging capabilities Viewing gRPC error information on the client Debugging gRPC server components inside a .NET application Viewing the debug output on the gRPC server console Applying logs to gRPC Configuring a logger on the gRPC client Applying a logger on the gRPC server Testing our log output Applying metrics to gRPC Configuring metrics on the gRPC server Enabling metric collection on the gRPC client Viewing gRPC metrics Summary Questions Further reading Assessments Chapter 1, Creating a Basic gRPC Application on ASP.NET Core Chapter 2, When gRPC Is the Best Tool and When It Isn't Chapter 3, Protobuf – the Communication Protocol of gRPC Chapter 4, Performance Best Practices for Using gRPC on .NET Chapter 5, Applying Versioning to the gRPC API Chapter 6, Scaling a gRPC Application Chapter 7, Using Different Call Types Supported by gRPC Chapter 8, Using Well-Known Types to Make Protobuf More Handy Chapter 9, Securing gRPC Endpoints in Your ASP.NET Core Application with SSL/TLS Chapter 10, Applying Authentication and Authorization to gRPC Endpoints Chapter 11, Using Logging, Metrics, and Debugging in gRPC on .NET Why subscribe? Other Books You May Enjoy Packt is searching for authors like you Share Your Thoughts
Donate to keep this site alive
How to download source code?
1. Go to: https://github.com/PacktPublishing
2. In the Find a repository… box, search the book title: Microservices Communication in .NET Using gRPC: A practical guide for .NET developers to build efficient communication mechanism for distributed apps
, sometime you may not get the results, please search the main title.
3. Click the book title in the search results.
3. Click Code to download.
1. Disable the AdBlock plugin. Otherwise, you may not get any links.
2. Solve the CAPTCHA.
3. Click download link.
4. Lead to download server to download.