GRPC: Difference between revisions
Jump to navigation
Jump to search
Created page with "=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. =Define Your Service, Messages= <syntaxhighlight lang="proto"> 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 mo..." |
No edit summary |
||
Line 79: | Line 79: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Csharp= | |||
Lets start with a CSharp client | |||
<syntaxhighlight lang="bash"> | |||
dotnet new console -n grpc_client_cs | |||
cd grpc_client_cs | |||
mkdir -p api/proto/v1 | |||
</syntaxhighlight> | |||
Add the dependencies | |||
<syntaxhighlight lang="bash"> | |||
dotnet add package Grpc.Net.Client | dotnet add package Grpc.Net.Client | ||
dotnet add package Google.Protobuf | dotnet add package Google.Protobuf | ||
dotnet add package Grpc.Tools | dotnet add package Grpc.Tools | ||
</syntaxhighlight> | |||
Add the proto top the project. | |||
<syntaxhighlight lang="bash"> | |||
<ItemGroup> | |||
<Protobuf Include="api/proto/v1/dvdrental.proto" GrpcServices="Client" /> | |||
</ItemGroup> | |||
</syntaxhighlight> | |||
For basic client we have | |||
<syntaxhighlight lang="cs"> | |||
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})"); | |||
} | |||
</syntaxhighlight> | |||
Lets add oauth. | |||
<syntaxhighlight lang="bash"> | |||
dotnet add package Microsoft.Extensions.Http | |||
</syntaxhighlight> |
Revision as of 06:20, 28 June 2025
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.
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 Microsoft.Extensions.Http