syntax = "proto3";
package helloworld;
message HelloRequest {
string name = 1;
string colour = 2;
}
message HelloReply {
string message = 1;
bool authed = 2;
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
package helloworld;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc SayLotsOfHellos (stream HelloRequest) \
returns (stream HelloReply) {}
message HelloRequest {
string name = 1;
string colour = 2;
}
message HelloReply {
string message = 1;
bool authed = 2;
}
protoc -I helloworld/ helloworld/helloworld.proto \
--go_out=plugins=grpc:helloworld
type GreeterServer interface {
SayHello(context.Context, *HelloRequest) (*HelloReply, error)
}
func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) {
s.RegisterService(&_Greeter_serviceDesc, srv)
}
type server struct{}
func (s *server) SayHello(
ctx context.Context,
in *pb.HelloRequest,
) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.Name)
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}
func main() {
lis, err := net.Listen("tcp", 8080)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
conn, _ := grpc.Dial(address, grpc.WithInsecure()
c := pb.NewGreeterClient(conn
r, err := c.SayHello(ctx, &pb.HelloRequest{
Name: "Bob",
}
fmt.Println(r.Message)