Microservices Communication in .NET Using gRPC - Fiodar Sazanavets - E-Book

Microservices Communication in .NET Using gRPC E-Book

Fiodar Sazanavets

0,0
27,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 463

Veröffentlichungsjahr: 2022

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Microservices Communication in .NET Using gRPC

A practical guide for .NET developers to build efficient communication mechanism for distributed apps

Fiodar Sazanavets

BIRMINGHAM—MUMBAI

Microservices Communication in .NET Using gRPC

Copyright © 2022 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Richa Tripathi

Publishing Product Manager: Sathya Mohan

Senior Editor: Rohit Singh

Content Development Editor: Kinnari Chohan

Technical Editor: Maran Fernandes

Copy Editor: Safis Editing

Project Coordinator: Deeksha Thakkar

Proofreader: Safis Editing

Indexer: Sejal Dsilva

Production Designer: Prashant Ghare

Marketing Coordinator: Sonakshi Bubbar

First published: January 2022

Production reference: 1050122

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80323-643-8

www.packt.com

To my mother, Liliya Sazanavets, and to the memory of my father, Dzmirty Sazanavets, who sadly isn't with us anymore, for their sacrifices and for making me the person I am today. To my wife, Olga Sazanavets, who has always inspired and supported me in all of my endeavors.

– Fiodar Sazanavets

Contributors

About the author

Fiodar Sazanavets is an experienced lead software developer. His main areas of expertise are ASP.NET, SQL Server, Azure, Docker, Internet of Things, microservices architecture, and various frontend technologies.

Fiodar built his software engineering experience while working in a variety of industries, including water engineering, financial, railway, and defense. He has played a leading role in various projects and, as well as writing software, his duties have included performing architectural tasks.

Fiodar is passionate about teaching other people programming skills. He has published a number of programming courses on various online platforms. Fiodar regularly writes about software development on his personal website, scientificprogrammer.net.

I want to thank all the people who have supported and mentored me throughout my career, including Dikaios Papadogkonas, Vache Chek, Ian Turner, Paul Eccleston, Frank Lawrence, and all the other people I have worked or collaborated with.

About the reviewer

James Carter is a self-taught software developer located in the Midlands, UK. He is especially passionate about microservice-driven architectures and building large-scale cloud-based platforms. He is currently working at CSL Group doing just that – building a scalable IoT platform that will be capable of handling millions of devices.

In just 5 years of being a developer, James has developed a large range of skills in this area, from gRPC to Docker, building on solid expertise in Linux and Windows systems.

Besides developing software, James enjoys running and maintaining his home lab, comprising many servers and pieces of networking equipment, as well as spending time with his family and two cats, Oreo and Kitkat.

Table of Contents

Preface

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

Other Books You May Enjoy

Section 1: Basics of gRPC on .NET

This part teaches how to build a basic .NET application that uses gRPC. The chapters cover the most fundamental parts of gRPC functionality. After completing this part, you will know how to enable gRPC inside an ASP.NET Core application and how to connect to it from external applications. This part contains the following chapters:

Chapter 1, Creating a Basic gRPC Application on ASP.NET CoreChapter 2, When gRPC Is the Best Tool and When It Isn'tChapter 3, Protobuf – the Communication Protocol of gRPC