DEV Community

Cover image for Getting Started with gRPC, .NET 6, and Amazon EC2

Getting Started with gRPC, .NET 6, and Amazon EC2

Now, I continue to gRPC, why? You have many options to get resources/data from other microservices, like REST or GraphQL. Since I'm exploring gRPC, I want to share my experience too. So, let's go!

Tools

Prepare the Project

You may skip this process if you are willing to try the server code using EC2.

  • Generate .gitignore file. Command: dotnet new gitignore

  • Generate solution file. Command: dotnet new sln

Now, we will move to generate gRPC Server Code.

gRPC Server

  • Using dotnet SDK to generate gRPC project. Command: dotnet new grpc -o GRPCExample
  • Add the project to the solution. Command: dotnet sln add GRPCExample
  • Try to run your project dotnet run --project GRPCExample

gRPC Client

  • Let's move on to our gRPC Client, we will use console app to call the server. First, you will need to generate the project. Command: dotnet new console -o GRPC.Client

  • Add the project to the solution. Command: dotnet sln add GRPC.Client

  • Add some packages that will be required. Command:

dotnet add GRPC.Client package Grpc.Net.Client
dotnet add GRPC.Client package Google.Protobuf
dotnet add GRPC.Client package Grpc.Tools
Enter fullscreen mode Exit fullscreen mode
  • Copy the greet.proto file from GRPCExample project, don't forget to have same directory, the directory is Protos.

  • Update the namespace in the file greet.proto at GRPC.Client.

option csharp_namespace = "GRPC.Client";
Enter fullscreen mode Exit fullscreen mode
  • Update Program.cs at our GRPC.Client. Don't forget to update the port.
using System.Threading.Tasks;
using Grpc.Net.Client;
using GRPC.Client;

// The port number must match the port of the gRPC server.
using var channel = GrpcChannel.ForAddress("http://localhost:5255");
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(
                  new HelloRequest { Name = "GreeterClient" });
Console.WriteLine("Greeting: " + reply.Message);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Enter fullscreen mode Exit fullscreen mode
  • Done! If you are already run the gRPC server, you may try to run the client project. Command: dotnet run --project GRPC.Client. You will get this output.

dotnet run

Let's go to our business

Setup EC2

  • I will use Ubuntu as my base operating system. Feel free to use another operating system.

image section

  • I use t3.micro just for evaluating my solution.

vm size

  • Don't forget to open the appropriate port. In my case, I will use 80.

Install .NET SDK on EC2

  • SSH into your EC2 Instance using the public IP.
  • You may use this guide to install the .NET SDK.

Command:

sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-6.0
Enter fullscreen mode Exit fullscreen mode

Publish gRPC Server

You will need to publish the gRPC Server source code. I will build the source in my EC2 Instance. There are many alternatives to deploying/delivering your app. Feel free to use another method. In my case, I uploaded my source code to Github.

Run gRPC Server

Note: Run all these commands in your EC2 Instance.

  • Pull the source code using git. Command: git clone https://github.com/berviantoleo/grpc-explore. Please update the command with your own repository if you want to use your code.
  • Publish the application. Command: dotnet publish -o published GRPCExample. Run this command inside the root directory of the project.
  • Run the server. Command: cd published && sudo dotnet GRPCExample.dll --urls=http://0.0.0.0:80. I'm using sudo since I will run in port 80 which generally can't be used by normal users.

Test using gRPC Client

  • Update the Program.cs in your client to point the public IP.
  • Run the client

test result

(Optional) Using another programming language as the client

Anyway, it's not limited to .NET code as the client. You may use another language, for example, GO. Here is an example. You need to read this article for the prerequisite.

  • You will need to install some tools using Go.
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28

go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
Enter fullscreen mode Exit fullscreen mode
  • Create new directory as example go_client and init the go module using go mod init go_client.

  • Install the gRPC dependencies. Command: go get -u google.golang.org/grpc.

  • Copy greet.proto into greet/greet.proto. Update the greet.proto like this.

syntax = "proto3";

option go_package = "go_client/greet";

package greet;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply);
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings.
message HelloReply {
  string message = 1;
}
Enter fullscreen mode Exit fullscreen mode
  • Generate the go code. Command: protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative greet/greet.proto

  • My main.go code will be like this.

package main

import (
    "context"
    "go_client/greet"
    "log"

    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
)

func main() {
    // don't forget to update the address
    conn, err := grpc.Dial("108.137.77.151:80", grpc.WithTransportCredentials(insecure.NewCredentials()))
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }
    defer conn.Close()
    greeterClient := greet.NewGreeterClient(conn)
    replyMessage, err := greeterClient.SayHello(context.TODO(), &greet.HelloRequest{
        Name: "Leo",
    })
    if err != nil {
        log.Fatalf("could not greet: %v", err)
    }
    log.Printf("Greeting: %s", replyMessage.GetMessage())
}
Enter fullscreen mode Exit fullscreen mode

result

So, don't worry if you want to use gRPC. You can use another language as well.

Thanks for Reading

Thanks for reading! If you have any feedback, feel free to comment here and give your best suggestion. Happy exploring!

Spongebob Happy GIF

Top comments (0)