Building Server-side Microservices with gRPC in Golang! 🌟
Welcome back to our gRPC in Golang series! In this article, we'll delve into the implementation of a gRPC server-side microservice using Golang. Let's take a closer look at the provided code for the PingService and understand how the controller and main.go facilitate this microservice.
Understanding the Structure:
Proto File - ping.proto:
The ping.proto
file serves as the contract for the PingService. It defines the message types and the service:
protobuf
syntax = "proto3"; package grpc_tutorial; option go_package = ".;pb"; option java_package = "com.github.MrAzharuddin.grpc_tutorial.pb"; option java_multiple_files = true; message PingRequest { } message PingResponse { string message = 1; } service PingService { rpc Ping(PingRequest) returns (PingResponse); }
This file specifies the service methods and the expected request and response message formats.
Controller - ping.controller.go:
The ping.controller.go
file contains the business logic for the PingService:
go
package controllers import ( "context" "github.com/MrAzharuddin/grpc_tutorial/pb" ) type PingServer struct { pb.UnimplementedPingServiceServer } func NewPingServer() *PingServer { return &PingServer{} } func (ps *PingServer) Ping(ctx context.Context, req *pb.PingRequest) (*pb.PingResponse, error) { return &pb.PingResponse{Message: "Hello! It's me :) Your server"}, nil }
In this file, PingServer
struct implements the Ping
method defined in the Proto file. The Ping
method handles incoming requests, creating and returning a PingResponse
with a "Hello!" message.
Main Entry - main.go:
The main.go
file initializes the gRPC server and registers the Ping service.
go
package main import ( "fmt" "log" "net" "os" "github.com/MrAzharuddin/grpc_tutorial/cmd/server/controllers" "github.com/MrAzharuddin/grpc_tutorial/pb" "google.golang.org/grpc" "google.golang.org/grpc/reflection" ) var ( host = "localhost" port = ":50051" ) func main() { // merging host and port addr := fmt.Sprintf("%s%s", host, port) // create a network listener and handle errors lis, err := net.Listen("tcp", addr) if err != nil { log.Println("error while starting tcp listener: ", err) os.Exit(1) } log.Println("server started at: ", "http://" + addr) // create a new gRPC server grpcServer := grpc.NewServer() // get controllers of services pingCtl := controllers.NewPingServer() // register the ping service pb.RegisterPingServiceServer(grpcServer, pingCtl) reflection.Register(grpcServer) if err := grpcServer.Serve(lis); err != nil { log.Println("error serving gRPC: ", err) os.Exit(1) } }
The main.go
file initiates the gRPC server, sets up the listener, and registers the PingService
with the gRPC server using the generated controllers
from the Proto file.
Server Configuration and Setup:
This code sets up a gRPC server that listens for connections on localhost:50051
. The PingServer
controller handles the Ping
method, returning a "Hello!" message when invoked.
The main.go
orchestrates the entire server setup, registering the service for handling gRPC requests.
Stay tuned for Next Part, where we'll dive deeper into creating a gRPC client in Golang and explore the client-server communication process.
Github: Source code
LinkedIn: Click here