Create the structure
1mkdir myapp
2cd myapp && go mod init myapp
3go mod tidy
The steps
- Create the protofile descript the message
 1syntax = "proto3";
 2package proto;
 3
 4option go_package = "./proto";
 5
 6//A sample service which contains all our rpc.
 7service MyService{
 8 //The definition of rpc.
 9 rpc MyFunc(Request) returns (StringMessage) {}
10}
11
12//Empty Request.
13message Request {
14}
15
16//The message to Return when rpc is invoked.
17message StringMessage {
18    string  reply = 1;
19}
- Compile the proto file use protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. hello-world.proto
- Move the pb file to protofoldermv hello-world.pb.go ./proto/
- Finally folder structure
1├── go.mod
2├── go.sum
3├── hello-world.proto
4├── main.go
5└── proto
6    ├── hello-world.pb.go
7    └── hello-world_grpc.pb.go
 1package main
 2
 3import (
 4        "context"
 5        "fmt"
 6        "net"
 7
 8        pb "myapp/proto"
 9
10        "google.golang.org/grpc"
11)
12
13type server struct {
14        pb.UnimplementedMyServiceServer
15}
16
17func (s *server) MyFunc(ctx context.Context, input *pb.Request) (*pb.StringMessage, error) {
18        fmt.Print("Inside The actual caller rpc FUnction")
19        return &pb.StringMessage{Reply: "Hey There"}, nil
20}
21
22func main() {
23        plistener, err := net.Listen("tcp", ":2408")
24        if err != nil {
25                panic("Failed to bind to port")
26        }
27
28        //Creating a New gRPC server
29        gServ := grpc.NewServer()
30        //Binding the stub function with the func we created
31        pb.RegisterMyServiceServer(gServ, &server{})
32        fmt.Print("gRPC server starting at port 2408")
33        if err := gServ.Serve(plistener); err != nil {
34                panic("Unable to start gRPC server")
35        }
36}
- To testing the grpc server go run main.go
- To build the server binary go build -o gRPC-server main.go
- Run the server ./gRPC-server
To create python client invoker the grpc server endpoint
- Create the virtual environment python3 -m venv ~/.venv/global
- Active the environment source ~/.venv/global/bin/activate
- Install the gRPC tools pip install grpcio-tools
- Generate the client python code python3 -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. hello-world.proto
- Create client.py
 1import grpc
 2import os
 3import hello_world_pb2_grpc
 4import hello_world_pb2
 5import time
 6
 7def invoke():
 8    server_addr =  os.getenv("SERVER_URL")
 9    channel = grpc.insecure_channel(server_addr)
10    stub = hello_world_pb2_grpc.MyServiceStub(channel)
11    while True:
12        response = stub.MyFunc(hello_world_pb2.Request())
13        print(response)
14        time.sleep(5)
15
16invoke()
- Run the client endpoint to testingSERVER_URL=127.0.0.1:2408 python3 ./client.py
How to invoke the GRPC service