How to Use API7 Enterprise to Proxy gRPC Services?

January 2, 2024

Products

In today's rapidly evolving technological landscape, gRPC (Google Remote Procedure Call) has become an indispensable component in many applications. However, to fully harness the potential of gRPC, a robust API gateway is essential for the effective management of gRPC services, providing crucial functionalities such as protocol conversion, load balancing, authentication, and authorization.

Whether you are familiar with gRPC or not, a quick 5-minute read of this article will equip you with the knowledge of how to use the API7 Enterprise to proxy gRPC services.

API7 Enterprise and gRPC

Prerequisites

  1. Install API7 Enterprise.

  2. Install grpcurl to simulate gRPC client interactions with your gRPC server.

  3. Start a gRPC service for test:

    • API7.ai provides a gRPC service example for testing. Alternatively, you can run it using the following docker command:
    docker run -d --name grpc-service -p 50051:50051 --restart always api7/grpc-server-example:1.0.0
    
    • Once started, use grpcurl to view the available gRPC service list and methods:
    $ grpcurl -plaintext 127.0.0.1:50051 list
    
    grpc.reflection.v1alpha.ServerReflection
    helloworld.Greeter
    helloworld.TestImport
    
    $ grpcurl -plaintext 127.0.0.1:50051 list helloworld.Greeter
    
    helloworld.Greeter.GetErrResp
    helloworld.Greeter.Plus
    helloworld.Greeter.SayHello
    helloworld.Greeter.SayHelloAfterDelay
    helloworld.Greeter.SayHelloBidirectionalStream
    helloworld.Greeter.SayHelloClientStream
    helloworld.Greeter.SayHelloServerStream
    

Using API7 Enterprise to Proxy gRPC Services

Update Gateway Instance Configuration

By default, API7 Enterprise instances only support TLS-encrypted HTTP/2 on port 9443. For testing convenience, modify the gateway instance configuration file in the node_listen section, adding a port 9081 to support non-encrypted HTTP/2.

apisix:
  node_listen:
    - port: 9080
      enable_http2: false
    - port: 9081
      enable_http2: true

After making the changes, rerun docker-compose up -d in the api7-ee directory where API7 Enterprise is installed.

Configure Services and Routes

Next, on the dashboard of API7 Enterprise, configure corresponding resources to proxy the prepared gRPC service.

  1. Add a service named grpc-example and select gRPC as the upstream scheme.

Use API7 to proxy grpc-1

  1. Add a route within the grpc-example service

    • Route path format: /{service}/{method}
    • For example, based on the previously queried gRPC service list and methods, configure the path as /helloworld.Greeter/SayHello.

Use API7 to proxy grpc-2

Publish Services for Testing

After creating the service and route, publish the service to the gateway group to make it effective.

  1. Click on "Publish Service," select the specified gateway group, and choose the service to publish.

  2. Add a gRPC service node (IP + port), then click "Publish".

  3. Test gRPC service using grpcurl:

    • Use the helloworld.proto file to ensure grpcurl aligns the request and response format with the gRPC service definition. The .proto file for this example can be found here.
    • Run the following command to see the successful proxy of gRPC service using API7 Enterprise.
    $ grpcurl -plaintext -proto helloworld.proto -d '{"name":"apisix"}' 127.0.0.1:9081 helloworld.Greeter.SayHello
    
    {
      "message": "Hello apisix"
    }
    

Conclusion

API7 Enterprise boasts 100+ built-in plugins, covering a wide range of functionalities such as authentication, authorization, rate limiting, logging, and monitoring. Utilizing API7 Enterprise to proxy gRPC services significantly enhances system flexibility, security, performance, and manageability, providing developers and operations teams with powerful tools and control mechanisms. Take the step to try out API7 Enterprise and empower your gRPC application!

Tags:
API7 EnterprisegRPC