Simple PRC (Ping-Pong)
// - proto/unary.proto -
syntax = "proto3";
option go_package = "proto;unary_proto";
package ua.made.unary;
// The greeting service definition.
service Service {
// Sends a greeting
rpc Hello(Request) returns (Reply) {}
}
// The request message containing the user's name.
message Request { string message = 1; }
// The response message containing the greetings
message Reply { string message = 1; }
// - server.go -
// Package main implements a server for Greeter service.
package main
import (
"context"
"fmt"
"log"
"net"
pb "github.com/butuzov/sandbox/grpc/simple-rpc/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
const (
address = "localhost:4772"
)
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedServiceServer
}
var _ pb.ServiceServer = (*server)(nil)
// SayHello implements helloworld.GreeterServer
func (s *server) Hello(ctx context.Context, in *pb.Request) (*pb.Reply, error) {
log.Printf("(incoming server) %#v \n", in.GetMessage())
switch in.GetMessage() {
case "Ping":
return &pb.Reply{Message: "Pong"}, nil
}
return &pb.Reply{Message: "<echo>" + in.GetMessage()}, nil
}
func main() {
listener, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
fmt.Println("SERVER RUNS@", listener.Addr().String())
s := grpc.NewServer()
pb.RegisterServiceServer(s, &server{})
reflection.Register(s)
log.Fatalf("failed to serve: %v", s.Serve(listener))
}
// - client.go -
package main
import (
"context"
"log"
pb "github.com/butuzov/sandbox/grpc/simple-rpc/proto"
"google.golang.org/grpc"
)
const (
address = "localhost:4772"
)
func main() {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewServiceClient(conn)
if response, err := c.Hello(context.Background(), &pb.Request{Message: "Ping"}); err != nil {
log.Fatalf("Error connecting to server: %v", err)
} else {
log.Printf("(incoming client) %#v \n", response.GetMessage())
}
}