GRPC
Jump to navigation
Jump to search
Introduction
gRPC is a high-performance, open-source RPC framework that uses Protocol Buffers for serialization and HTTP/2 for transport. It enables efficient communication between services across different languages and platforms.
Message Types
Type | Client Sends | Server Responds | Use Case Example |
---|---|---|---|
Unary | Single request | Single response | Fetching user profile |
Server Streaming | Single request | Stream of responses | Receiving a list of updates or logs |
Client Streaming | Stream of requests | Single response | Uploading a batch of telemetry data |
Bidirectional Streaming | Stream of requests | Stream of responses | Real-time chat or collaborative editing |
Define Your Service, Messages
syntax = "proto3";
package dvdrental.v1;
import "google/protobuf/empty.proto";
option go_package = "git.bibble.co.nz/bibble235/grpc_server_go/pb;pb";
// Film represents a movie in the DVD rental system
message Film {
// Unique identifier for the film
string id = 1;
// Title of the film
string title = 2;
// Description or synopsis of the film
string description = 3;
// Year the film was released
string release_year = 4;
}
// Actor represents a person who acts in films
message Actor {
// Unique identifier for the actor
string id = 1;
// First name of the actor
string first_name = 2;
// Last name of the actor
string last_name = 3;
}
// ActorList contains multiple Actor records
message ActorList {
// List of actors
repeated Actor actor = 1;
}
// FilmList contains multiple Film records
message FilmList {
// List of films
repeated Film film = 1;
}
// FilmIdRequest is used to request a specific film by ID
message FilmIdRequest {
// ID of the film to retrieve
string id = 1;
}
// ActorIdRequest is used to request a specific actor by ID
message ActorIdRequest {
// ID of the actor to retrieve
string id = 1;
}
// DVDRentalService provides operations to interact with the DVD rental system
service DVDRentalService {
// GetActor retrieves an actor by their ID
rpc GetActor(ActorIdRequest) returns (Actor);
// GetActorListWithStream streams all actors one by one
rpc GetActorListWithStream(google.protobuf.Empty) returns (stream Actor);
// GetActorListWithRepeated retrieves all actors in a single response
rpc GetActorListWithRepeated(google.protobuf.Empty) returns (ActorList);
// GetFilm retrieves a film by its ID
rpc GetFilm(FilmIdRequest) returns (Film);
// GetFilmListWithStream streams all films one by one
rpc GetFilmListWithStream(google.protobuf.Empty) returns (stream Film);
// GetFilmListWithRepeated retrieves all films in a single response
rpc GetFilmListWithRepeated(google.protobuf.Empty) returns (FilmList);
}
Csharp
Lets start with a CSharp client
dotnet new console -n grpc_client_cs
cd grpc_client_cs
mkdir -p api/proto/v1
Add the dependencies
dotnet add package Grpc.Net.Client
dotnet add package Google.Protobuf
dotnet add package Grpc.Tools
Add the proto top the project.
<ItemGroup>
<Protobuf Include="api/proto/v1/dvdrental.proto" GrpcServices="Client" />
</ItemGroup>
For basic client we have
using Grpc.Net.Client;
using Dvdrental.V1;
using Google.Protobuf.WellKnownTypes;
var channel = GrpcChannel.ForAddress("https://localhost:50051");
var client = new DVDRentalService.DVDRentalServiceClient(channel);
// Example: Get an actor by ID
var actorRequest = new ActorIdRequest { Id = "1" };
var actor = await client.GetActorAsync(actorRequest);
Console.WriteLine($"Actor: {actor.FirstName} {actor.LastName}");
// Example: Get all films (repeated)
var filmsResponse = await client.GetFilmListWithRepeatedAsync(new Empty());
foreach (var film in filmsResponse.Film)
{
Console.WriteLine($"Film: {film.Title} ({film.ReleaseYear})");
}
Lets add oauth.
dotnet add package DotNetEnv
dotnet add package Microsoft.Extensions.Http
dotnet add package Microsoft.Extensions.Configuration
dotnet add package System.IdentityModel.Tokens.Jwt
dotnet add package Microsoft.Extensions.Configuration.Json