Help Center/ FunctionGraph/ Best Practices/ Using FunctionGraph HTTP Functions to Process gRPC Requests
Updated on 2024-07-08 GMT+08:00

Using FunctionGraph HTTP Functions to Process gRPC Requests

Introduction

This chapter describes how to use gRPC and process gRPC requests in FunctionGraph.

This chapter uses example/helloworld in the gRPC example code project as an example to describe how to process gRPC requests with HTTP functions in FunctionGraph. Since HTTP functions do not directly support Go code deployment, this chapter provides an example of using binary conversion to deploy a Go program on FunctionGraph.

  • This feature is supported only in the LA-Santiago region.
  • By default, you do not have gRPC permissions. To use gRPC, submit a service ticket to add your account to the whitelist.

Procedure

  1. Building a code package

    Create the source file main.go. The code is as follows:

    // Package main implements a grpc_server for Greeter service.
    package main
    
    import (
         "context"
         "flag"
         "fmt"
         "log"
         "net"
    
         pb "helloworld/helloworld"
    
         "google.golang.org/grpc"
    )
    
    var (
         port = flag.Int("port", 8000, "The grpc_server port")
    )
    
    // server is used to implement helloworld.GreeterServer.
    type server struct {
         pb.UnimplementedGreeterServer
    }
    
    // SayHello implements helloworld.GreeterServer
    func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
         log.Printf("Received: %v", in.GetName())
         return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
    }
    
    func main() {
         flag.Parse()
         lis, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", *port))
         if err != nil {
            log.Fatalf("failed to listen: %v", err)
         }
         s := grpc.NewServer()
         pb.RegisterGreeterServer(s, &server{})
         log.Printf("grpc_server listening at %v", lis.Addr())
         if err := s.Serve(lis); err != nil {
            log.Fatalf("failed to serve: %v", err)
         }
     }
    # bootstrap
    $RUNTIME_CODE_ROOT/grpc-server

    In main.go, a gRPC server is started using port 8000 and helloworld.GreeterServer is registered. When the service is invoked, Hello XXX will be returned.

  2. Compiling and packaging
    1. On the Linux server, compile the preceding code using the go build -o grpc-server main.go command. Then, compress grpc-server and bootstrap into a ZIP package named xxx.zip.
    2. To use the Golang compiler to complete packaging on a Windows host, perform the following steps:
      # Switch the compilation environment
      # Check the previous Golang compilation environment
      go env 
      # Set the following parameters to the corresponding value of Linux
      set GOARCH=amd64 
      go env -w GOARCH=amd64 
      set GOOS=linux 
      go env -w GOOS=linux 
      
      # go build -o [target executable program] [source program]
      # Example
      go build -o grpc-server main.go 
      
      # Restore the compilation environment
      set GOARCH=amd64 
      go env -w GOARCH=amd64 
      set GOOS=windows 
      go env -w GOOS=windows
  3. Creating an HTTP function and uploading code

    Create an HTTP function and upload the xxx.zip package. For details, see Creating an HTTP Function.

  4. Creating an APIG trigger

    Create an APIG trigger. For details, see Using an APIG Trigger. You are advised to set Request Protocol to gRPC and Security Authentication to None to simplify the debugging process.

    Figure 1 APIG trigger
  5. Invocation test

    Use Postman to debug gRPC requests.

    Figure 2 gRPC request result