gRPC materials for Taste of IT 2022 talk
- .NET SDK (I used version 6)
- Python 3 with the venv module installed.
Detailed instructions assume a Linux environment. There are helper scripts for Linux and Windows. Each step has a complete description of the changes to manually apply, and also instructions for running the helper script, if you want to save time.
First, reset everything with helper script:
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 999 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 999 |
Create root folder for the projects:
mkdir src
cd src
Create service:
dotnet new grpc -o GrpcGreeterService
Update the applicationUrl in launchsettings.json to this:
"applicationUrl": "http://localhost:6000;https://localhost:6001"
(You can use whatever ports you need in your own projects. We're using these for consistency in the examples.)
Build the service:
cd GrpcGreeterService
dotnet build
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 101 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 101 |
Update Protos/greet.proto, extending it with a couple of new messages:
message GoodbyeRequest { string name = 1; }
message GoodbyeReply { string message = 1; }
Build again:
dotnet build
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 102 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 102 |
Add a new method using the new messages:
service Greeter {
// Sends a Hello greeting
rpc SayHello(HelloRequest) returns (HelloReply);
// Sends a Goodbye greeting
rpc SayGoodbye(GoodbyeRequest) returns (GoodbyeReply);
}
Build again:
dotnet build
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 103 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 103 |
Open GreeterService.cs and add a new method using the new messages:
public override Task<GoodbyeReply> SayGoodbye(GoodbyeRequest request, ServerCallContext context)
{
return Task.FromResult(new GoodbyeReply
{
Message = "Goodbye " + request.Name
});
}
Build again:
dotnet build
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 104 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 104 |
Run the server:
Operating System | Server Script |
---|---|
Linux | ./run_server.sh |
Windows | powershell -ExecutionPolicy ByPass -file run_server.ps1 |
Change to the project root (src):
cd ..
Create the client project:
dotnet new console -o GrpcGreeterClient
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 201 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 201 |
Switch to the folder and add dependencies:
cd GrpcGreeterClient
dotnet add package Grpc.Net.Client
dotnet add package Google.Protobuf
dotnet add package Grpc.Tools
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 202 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 202 |
Create a Protos folder, then copy the .proto file from the service project:
mkdir Protos
cp ../GrpcGreeterService/Protos/greet.proto Protos/
Update the namespace inside the greet.proto file to the project's namespace:
option csharp_namespace = "GrpcGreeterClient";
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 203 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 203 |
Edit the GrpcGreeterClient.csproj project file and add an item group with a <Protobuf>
element that refers to the greet.proto file:
<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
</ItemGroup>
Build the client to generate the types:
dotnet build
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 204 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 204 |
Replace the contents of Program.cs with this:
using System.Threading.Tasks;
using Grpc.Net.Client;
using GrpcGreeterClient;
// The port number must match the port of the gRPC server.
using var channel = GrpcChannel.ForAddress("http://localhost:6000");
var client = new Greeter.GreeterClient(channel);
var helloReply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
Console.WriteLine("Greeting: " + helloReply.Message);
var goodbyeReply = await client.SayGoodbyeAsync(new GoodbyeRequest { Name = "GreeterClient" });
Console.WriteLine("Greeting: " + goodbyeReply.Message);
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 205 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 205 |
Run the client:
Operating System | Server Script |
---|---|
Linux | ./run_dotnet_client.sh |
Windows | powershell -ExecutionPolicy ByPass -file run_dotnet_client.ps1 |
You should see this:
Greeting: Hello GreeterClient
Press any key to exit...
Create a virtual Python environment:
cd src
python3 -m venv grpc_env
cd grpc_env
source bin/activate
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 301 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 301 |
Create requirements.txt file:
echo 'grpcio-tools ~= 1.30' > requirements.txt
Install the dependencies:
pip install -r requirements.txt
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 302 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 302 |
Create project folders:
mkdir grpc_client
cd grpc_client
mkdir protobufs
mkdir src
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 303 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 303 |
Copy the protocol buffer file from the server project:
cp ../../GrpcGreeterService/Protos/greet.proto protobufs/
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 304 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 304 |
Generate Python code:
cd src
python -m grpc_tools.protoc -I ../protobufs --python_out=. --grpc_python_out=. ../protobufs/greet.proto
This generates two files:
- greet_pb2_grpc.py
- greet_pb2.py
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 305 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 305 |
Create a client.py file with the following contents:
import grpc
import greet_pb2_grpc
from greet_pb2 import HelloRequest, GoodbyeRequest
channel = grpc.insecure_channel("localhost:6000")
client = greet_pb2_grpc.GreeterStub(channel)
hello_request = HelloRequest (
name = "GreeterClient"
)
goodbye_request = GoodbyeRequest (
name = "GreeterClient"
)
hello_result = client.SayHello(hello_request)
goodbye_result = client.SayGoodbye(goodbye_request)
print(hello_result.message)
print(goodbye_result.message)
Operating System | Helper Script |
---|---|
Linux | ./helper.sh 306 |
Windows | powershell -ExecutionPolicy ByPass -file helper.ps1 306 |
Run the Python client:
Operating System | Server Script |
---|---|
Linux | ./run_python_client.sh |
Windows | powershell -ExecutionPolicy ByPass -file run_python_client.ps1 |
Results:
Hello GreeterClient
Goodbye GreeterClient