I was curious about GRPC. So I developed simple GRPC Client and Server projects via .NET Core 6.
Main goal is: Develop a API that has GRPC. Stream large files for example excel files etc. In this project I used excel file.
- Developed via .Net Core 6
- Uses GRPC for streaming large files for example excel file etc.
Note
Currently I use Docker version 20.10.21 and Docker Compose version v2.6.0
Currently I use Linux Ubuntu 18.06 LTS machine on Google Cloud.
Docker Engine and Docker Compose must be installed. Check out on Docker's offical site.
- Download excel file from this site: https://eforexcel.com/wp/wp-content/uploads/2020/09/5m-Sales-Records.zip
- Move file to "GRPC_Server/bin/Debug/net6.0 directory". Rename file as "sales_records.csv"
Parameters
Name | Required | Type | Description |
---|---|---|---|
n/a | n/a | n/a | n/a |
Response
Content of Excel file will be streamed to client. You can see from console.
Note
❗ ❗ ❗ I used deadline as 5 seconds from Now. You can remove it or change it to whatever you want ❗ ❗ ❗
I didn't use any design patterns. This project is for learning about GRPC.
- Connect to GRPC Server.
- Invoke GetSalesData Method without filter. I used deadline but you can change or remove it.
- Read messages until all the messages are fetched. Actually read excel file line by line (one row at a time) via GRPC Stream and print on console.
- If stream ended print total number of records from excel file that streamed via GRPC.
- If deadline or exception occurs print exception message
- Read csv file named "sales_records.csv" line by line via loop.
- Split row data using comma ",". Seperator is comma in excel file.
- Write data into model named "SalesDataModel". This model is autogenerated from proto file named "sales.proto"
- Return model as a stream by simply writing our data model to the responseStream.
- If exception occurs return exception.
I used this proto file.
syntax = "proto3";
import "google/protobuf/timestamp.proto";
option csharp_namespace = "Protos";
package sales;
service SalesService { //Service name
rpc GetSalesData(Request) returns (stream SalesDataModel) {} //Method name is GetSalesData, takes type of Request object, returns SalesDataModel object as stream.
}
message Request{
string filters=1;
}
message SalesDataModel { //Excel file model
int32 OrderID = 1;
string Region = 2;
string Country = 3;
string ItemType=4;
google.protobuf.Timestamp OrderDate=5;
google.protobuf.Timestamp ShipDate=6;
int32 UnitsSold=7;
float UnitCost=8;
float UnitPrice=9;
int32 TotalRevenue=10;
int32 TotalCost=11;
int32 TotalProfit=12;
}
- Use docker files to build docker images.
- Use docker-compose files to run containers.
Example Commands to build images:
docker build -t cenkcamkiran/gcloud-projects:grpcexample_v1.0 . #build to use grpc server
docker build -t cenkcamkiran/gcloud-projects:grpcclient_v1.0 . #build to use grpc client
I am open every advice for my project. I am planning to improve myself on .NET Core 6, Microservices and Container Technologies. So don't hesitate comment on my project. Every idea is plus for me.
Please use the Github issues.
I want to learn more details about GRPC.