gRPC Quickstart using Golang
I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Search for a command to run...
I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
No comments yet. Be the first to comment.
Let's assume we have a sample Golang application (in a Go project folder). Contents of main.go package main import "fmt" func main() { fmt.Println("hello world") } In the same folder, run the following commands to initialize go modules # initial...

When you create a new Kubernetes Cluster in EKS - you still need to do several things until you can start using it. This simple set of steps aims to help people create an EKS cluster in simple steps and configure the most essential services like Log ...

To implement a stack in golang we can make use of the slice datatype which golang provides which allows us to modify the size on the fly. If we were doing it in C, we would need to define a max size for the array. Lets start with a simple structure ...

The purpose of this guide is to make a simple telegram bot, which replies with a compliment for each and every message that it receives. Create a bot in telegram Look for a bot named BotFather https://t.me/botfather . This bot is used to create bots...

gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.
source: grpc.io
Golang plugins for protocol compilers
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
Make sure the GOPATH/bin is part of your PATH env
export PATH="$PATH:$(go env GOPATH)/bin"
Now, we are ready to get started.
Setup Projects
Create gRPC server
Create gRPC client
Test RPCs
To keep things simple, lets create two projects
Server
Client
Add simple main.go files in each and initialize go module (ex - go mod init github.com/mridulganga/server), also make sure to add them in GOPATH as client would need to import a file exposed by the server.
Create a new file called helloworld.proto in server project's pkg/helloworld folder
syntax = "proto3";
option go_package = "github.com/mridulganga/server/pkg/helloworld";
package helloworld;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
Now cd to the folder and run the following command to generate the protobuf and gRPC interface
cd server/pkg/helloworld
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
helloworld.proto
The above step should generate two files in the same folder
helloworld.pb.go
helloworld_grpc.pb.go
Create the server in main.go (server/main.go)
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"google.golang.org/grpc"
pb "github.com/mridulganga/server/pkg/helloworld"
)
var (
port = flag.Int("port", 50051, "The server port")
)
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
func main() {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
Code taken from https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_server/main.go
Create the client in main.go (client/main.go)
package main
import (
"context"
"flag"
"log"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "github.com/mridulganga/server/pkg/helloworld"
)
const (
defaultName = "world"
)
var (
addr = flag.String("addr", "localhost:50051", "the address to connect to")
name = flag.String("name", defaultName, "Name to greet")
)
func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
}
code taken from https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_client/main.go
Run the server
go run server/main.go &
Note that we are using & to run server in background, alternatively you can open a new terminal for running the client
Run the client
go run client/main.go
go run client/main.go -name mridul
The client should log "Greeting world" or the name which is supplied. The client is using gRPC to connect to the server and call SayHello method on the server.