-
-
Save laurencecwj/1ef0b4d177b6be27b760df5e6ed1395e to your computer and use it in GitHub Desktop.
Golang gRPC protobuf hello world example (also using grpcurl)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| syntax = "proto3"; | |
| package protos; | |
| service HelloWorld { | |
| rpc SayHello (HelloRequest) returns (HelloResponse) {} | |
| } | |
| message HelloRequest { | |
| string name = 1; | |
| } | |
| message HelloResponse { | |
| string message = 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "log" | |
| "net" | |
| context "golang.org/x/net/context" | |
| pb "github.com/user/repo/pb" | |
| "google.golang.org/grpc" | |
| "google.golang.org/grpc/reflection" | |
| ) | |
| const port = ":50051" | |
| // Server ... | |
| type Server struct{} | |
| // SayHello ... | |
| func (s *Server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) { | |
| return &pb.HelloResponse{Message: "Hello, " + in.Name}, nil | |
| } | |
| func main() { | |
| listen, err := net.Listen("tcp", port) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| grpcServer := grpc.NewServer() | |
| pb.RegisterHelloWorldServer(grpcServer, &Server{}) | |
| reflection.Register(grpcServer) | |
| grpcServer.Serve(listen) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protoc --go_out=plugins=grpc:. helloworld.proto | |
| go run main.go |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| go get github.com/fullstorydev/grpcurl | |
| go install github.com/fullstorydev/grpcurl/cmd/grpcurl | |
| grpcurl -v -plaintext -d '{"name":"bob"}' localhost:50051 protos.HelloWorld/SayHello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment