syntax="proto3";optiongo_package=".;proto";packagehelloworld;// The greeting service definition
serviceGreeter{// Sends a greeting
rpcSayHello(HelloRequest)returns(HelloReply){}}// The request message containing the user's name
messageHelloRequest{stringname=1;}// The response message containing the greetings
messageHelloReply{stringmessage=1;}
packagemainimport("context""flag""fmt""log""net"pb"github.com/lixd/grpc-go-example/features/proto/helloworld""google.golang.org/grpc")varport=flag.Int("port",50051,"the port to serve on")typeserverstruct{pb.UnimplementedGreeterServer}func(s*server)SayHello(ctxcontext.Context,in*pb.HelloRequest)(*pb.HelloReply,error){return&pb.HelloReply{Message:"hello "+in.Name},nil}funcmain(){// Create a gRPC server objects:=grpc.NewServer()// Attach the Greeter service to the serverpb.RegisterGreeterServer(s,&server{})// Serve gRPC Serverlis,err:=net.Listen("tcp",fmt.Sprintf(":%d",*port))iferr!=nil{log.Fatalf("failed to listen: %v",err)}log.Println("Serving gRPC on 0.0.0.0"+fmt.Sprintf(":%d",*port))iferr:=s.Serve(lis);err!=nil{log.Fatalf("failed to serve: %v",err)}}
packagemainimport("log""golang.org/x/net/context""google.golang.org/grpc"pb"i-go/grpc/gateway/proto/helloworld")const(address="localhost:8080"defaultName="world")funcmain(){conn,err:=grpc.Dial(address,grpc.WithInsecure(),grpc.WithBlock())iferr!=nil{panic(err)}deferconn.Close()c:=pb.NewGreeterClient(conn)r,err:=c.SayHello(context.Background(),&pb.HelloRequest{Name:defaultName})iferr!=nil{log.Fatalf("could not greet: %v",err)}log.Printf("Greeting: %s",r.Message)}
packagemainimport("context""flag""fmt""log""net""net/http""github.com/grpc-ecosystem/grpc-gateway/v2/runtime"pb"github.com/lixd/grpc-go-example/features/proto/helloworld""google.golang.org/grpc")varport=flag.Int("port",50051,"the port to serve on")varrestful=flag.Int("restful",8080,"the port to restful serve on")typeserverstruct{pb.UnimplementedGreeterServer}func(s*server)SayHello(ctxcontext.Context,in*pb.HelloRequest)(*pb.HelloReply,error){return&pb.HelloReply{Message:"hello "+in.Name},nil}funcmain(){// Create a gRPC server objects:=grpc.NewServer()// Attach the Greeter service to the serverpb.RegisterGreeterServer(s,&server{})// Serve gRPC Serverlis,err:=net.Listen("tcp",fmt.Sprintf(":%d",*port))iferr!=nil{log.Fatalf("failed to listen: %v",err)}log.Println("Serving gRPC on 0.0.0.0"+fmt.Sprintf(":%d",*port))gofunc(){iferr:=s.Serve(lis);err!=nil{log.Fatalf("failed to serve: %v",err)}}()// 2. 启动 HTTP 服务// Create a client connection to the gRPC server we just started// This is where the gRPC-Gateway proxies the requestsconn,err:=grpc.Dial("localhost:50051",grpc.WithInsecure(),)iferr!=nil{log.Fatalln("Failed to dial server:",err)}gwmux:=runtime.NewServeMux()// Register Greetererr=pb.RegisterGreeterHandler(context.Background(),gwmux,conn)iferr!=nil{log.Fatalln("Failed to register gateway:",err)}gwServer:=&http.Server{Addr:fmt.Sprintf(":%d",*restful),Handler:gwmux,}log.Println("Serving gRPC-Gateway on http://0.0.0.0"+fmt.Sprintf(":%d",*restful))log.Fatalln(gwServer.ListenAndServe())}
主要增加了 启动 HTTP 服务的部分。
4)run
运行并测试效果。
1
2
3
lixd@17x:~/17x/projects/grpc-go-example/features/gateway/server$ go run main.go
2021/01/30 10:32:15 Serving gRPC on 0.0.0.0:50051
2021/01/30 10:32:15 Serving gRPC-Gateway on http://0.0.0.0:8080
gRPC 请求
1
2
lixd@17x:~/17x/projects/grpc-go-example/features/gateway/client$ go run main.go
2021/01/30 10:32:18 Greeting: hello world