gRPC调用过程分析

gRPC代码示例

// server
type SearchService struct{}
func (s *SearchService) Search(ctx context.Context, req *pb.SearchRequest) (*pb.SearchResponse, error) {
    return &pb.SearchResponse{Response: req.GetRequest() + "Server"}, nil
}
func main() {
    grpcserver := grpc.NewServer()
    pb.RegisterSearchServiceServer(grpcserver, &SearchService{})
    lis, err := net.Listen("tcp", ":9001")
	if err != nil {
		log.Fatal("Listen Err", err)
    }
    grpcserver.Serve(lis)
}

// client
func main() {
    conn, err := grpc.Dial(":9001",grpc.WithInsecure())
    if err != nil {
		log.Fatalf("Dial Err: %v", err)
    }
    defer conn.Close()
    client := pb.NewSearchServiceClient(conn)
    resp, err := client.Search(ctx,&pb.SearchRequest{Request: "grpc Test"})
    if err != nil {
		log.Fatal("client.Search", err)
    }		
}