Trying to build by first gRPC with GO, so I tried the following:
1.1. Installing required go lobraries
PS D:\grpc> go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
PS D:\grpc> go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
1.2. Adding GOPATH/bin to the PATH, so that protoc-gen-go
and protoc-gen-grpc
commands get recognized from the command line
2.1. Downloaded the protoc binary
fro windows from: https://github.com/protocolbuffers/protobuf/releases/
2.2. Extracting the protoc zip file, and adding the bin
folder to the path
3.1. Wrote my proto file as:
// file: grpc/chat/chat.proto
syntax = "proto3";
package chat;
option go_package = "github.com/hajsf/grpc/chat";
message MessageRequest {
string body = 1;
}
message MessageResponse {
string body = 1;
}
service ChatService {
rpc SayHello(MessageRequest) returns (MessageResponse) {}
}
3.3. Compiled my proto file to get chat.pb.go
from the grpc folder as:
PS D:\grpc> protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative chat.proto
Accordingly 2 files had been generated: chat.pb.go
and chat_grpc.pb.go
3.4. Adding the chat.go
file as:
// file: grpc/chat/chat.go
package main
import (
"log"
chat "github.com/hajsf/grpc/chat"
"golang.org/x/net/context"
)
type Server struct {
}
func (s *Server) SayHello(ctx context.Context, in *chat.MessageRequest) (*chat.MessageResponse, error) {
log.Printf("Receive message body from client: %s", in.Body)
return &chat.MessageResponse{Body: "Hello From the Server!"}, nil
}
- Bult my server file as:
// file: grpc/server/main.go
package main
import (
"fmt"
"log"
"net"
"github.com/hajsf/grpc/chat"
"google.golang.org/grpc"
)
func main() {
fmt.Println("Go gRPC Beginners Tutorial!")
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 9000))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := chat.Server{}
grpcServer := grpc.NewServer()
chat.RegisterChatServiceServer(grpcServer, &s)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %s", err)
}
}
- Built my client file as:
// file: grpc/client/main.go
package main
import (
"log"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/hajsf/grpc/chat"
)
func main() {
var conn *grpc.ClientConn
conn, err := grpc.Dial(":9000", grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %s", err)
}
defer conn.Close()
c := chat.NewChatServiceClient(conn) // Why not found?
response, err := c.SayHello(context.Background(), &chat.MessageRequest{Body: "Hello From Client!"})
if err != nil {
log.Fatalf("Error when calling SayHello: %s", err)
}
log.Printf("Response from server: %s", response.Body)
}
- In each of the 3 folders mentioned above,
chat
,server
andclient
GO modules had been created as:
PS D:\grpc> go mod init github.com/hajsf/grpc/<filenale>
PS D:\grpc> go mod tidy
- Tried running the
server
, but got the below error:
PS D:\grpc\server> go run github.com/hajsf/grpc/server
# github.com/hajsf/grpc/server
.\main.go:21:12: undefined: chat.Server
.\main.go:25:7: undefined: chat.RegisterChatServiceServer
- Tried running the
client
, but got the below error:
PS D:\grpc\client> go run github.com/hajsf/grpc/client
# github.com/hajsf/grpc/client
.\main.go:21:12: undefined: chat.NewChatServiceClient
The codes are loaded as GitHub repository at: hajsf/grpc (github.com)