This is a simple gRPC Echo Server implemented in Go. It provides unary and streaming echo services.
docker build -t grpc-echo:1 .
docker run -p443:443 --rm grpc-echo:1
cd client
docker build -t grpc-echo-client:1 .
docker run --rm grpc-echo-client:1 {{ server_ip_address_or_hostname }}:{{ server_port }}
- Go programming language (version 1.16 or higher)
- Protocol Buffers compiler (
protoc
) - Go gRPC plugin (
protoc-gen-go-grpc
)
-
Clone the repository or download the source code.
-
Install the required dependencies:
go get google.golang.org/grpc
go get google.golang.org/protobuf/cmd/protoc-gen-go
go get google.golang.org/grpc/cmd/protoc-gen-go-grpc
- Generate the Go code from the Protocol Buffers definition:
protoc -I=proto --go_out=. --go-grpc_out=. proto/echo.proto
- This will generate the
echo.pb.go
andecho_grpc.pb.go
files.
- Run the server:
go run main.go
- The server will start listening on port 5050.
The server provides the following gRPC API:
UnaryEcho performs a unary RPC call to echo the message received.
rpc UnaryEcho(EchoRequest) returns (EchoResponse) {}
ServerStreamingEcho performs a server-streaming RPC call to repeatedly send the same message at intervals.
rpc ServerStreamingEcho(EchoRequest) returns (stream EchoResponse) {}
ClientStreamingEcho performs a client-streaming RPC call to receive multiple messages from the client.
rpc ClientStreamingEcho(stream EchoRequest) returns (EchoResponse) {}
BidirectionalStreamingEcho performs a bidirectional-streaming RPC call to send and receive multiple messages.
rpc BidirectionalStreamingEcho(stream EchoRequest) returns (stream EchoResponse) {}
You can use the generated client code to interact with the server. Here's an example of a client calling the UnaryEcho service:
package main
import (
"context"
"log"
"time"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/features/proto/echo"
)
func main() {
conn, err := grpc.Dial("localhost:5050", grpc.WithInsecure())
if err != nil {
log.Fatalf("failed to connect: %v", err)
}
defer conn.Close()
client := pb.NewEchoClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
response, err := client.UnaryEcho(ctx, &pb.EchoRequest{Message: "Hello, gRPC!"})
if err != nil {
log.Fatalf("failed to call UnaryEcho: %v", err)
}
log.Printf("Response: %s", response.Message)
}
This project is licensed under the MIT License.