How to Use API7 Enterprise to Proxy gRPC Services?
Published: 2024-01-02 10:20:46

In today's rapidly evolving technological landscape, [gRPC (Google Remote Procedure Call)](https://en.wikipedia.org/wiki/GRPC) has become an indispensable component in many applications. However, to fully harness the potential of gRPC, [a robust API gateway](https://api7.ai/enterprise) 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](https://api7.ai/enterprise) to proxy gRPC services.

![API7 Enterprise and gRPC](https://static.apiseven.com/uploads/2024/01/02/ITu9xgjT_img_v3_026n_141d4850-e004-495e-95a0-66a004458e9g.jpg)

## Prerequisites

1. Install [API7 Enterprise](https://api7.ai/try?product=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](https://github.com/api7/grpc_server_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.

```yaml
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](https://static.apiseven.com/uploads/2024/01/02/DYCtoDdb_img_v3_026n_a733636b-d199-4eeb-b8a9-7a128631ae3g.jpg)

2.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](https://static.apiseven.com/uploads/2024/01/02/W739sg0C_img_v3_026n_9f63da6d-0b70-41e4-9c29-9d150c599adg.jpg)

### 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](https://github.com/api7/grpc_server_example/blob/master/proto/helloworld.proto).
   - 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](https://docs.api7.ai/hub), covering a wide range of functionalities such as [authentication](https://api7.ai/blog/api-gateway-authentication), authorization, [rate limiting](https://api7.ai/blog/rate-limiting-in-api-management), logging, and [monitoring](https://api7.ai/blog/api7-portal-monitor-api-metrics). 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!