Updated on 2024-11-27 GMT+08:00

Creating a gRPC API

APIG supports gRPC API creation. gRPC is a modern, open-source, high-performance Remote Procedure Call (RPC) framework that can run in any environment. You only need to define the request and response of each API, and let the gRPC framework take care of the rest. gRPC uses protocol buffers (protobuf) as its Interface Definition Language (IDL) and for bottom-layer message exchange. The following table compares gRPC and REST APIs.

Table 1 gRPC vs REST

Parameter

gRPC

REST

Message encoding

protobuf

JSON

Transmission protocol

HTTP/2

HTTP

Transmission performance

Fast, with less content to transmit

More content to transmit

Transmission mode

  • Unary RPC

    Send a single request and receive a single response.

  • Server streaming RPC

    Send a single request and receive multiple responses.

  • Client streaming RPC

    Send multiple requests and receive a single response.

  • Bidirectional streaming RPC

    Send multiple requests and receive multiple responses.

Send a single request and receive a single response.

If both your client and server are of the gRPC type, you can create an gRPC API to open up your backend capabilities. gRPC features low resource consumption and high transmission rate. It is suitable for internal service invocation and governance.

Restrictions

  • gRPC APIs cannot be imported, exported, or debugged, and do not support the import of API design files, CSE microservices, or CCE workloads.
  • Circuit breaker policies whose backend policy type is Mock, HTTP&HTTPS, or FunctionGraph are not supported.

Prerequisites

  • You have created an API group. If no API group is available, create one by referring to Creating an API Group.
  • If the backend service needs to use a load balance channel, create a channel first.
  • The backend service has a proto file that defines API request and response parameters. The proto file is used in gRPC to define data structures and service APIs. It describes data structures and interactions using protobuf and serves as a contract for communication between the client and backend services.

Procedure

  1. Go to the APIG console.
  2. Select a gateway at the top of the navigation pane.
  1. In the navigation pane, choose API Management > API Groups.
  2. Click a group name.
  3. On the APIs tab page, choose Create API > Create gRPC API.
  4. Configure the frontend definition according to 5.a.

    For gRPC APIs, the default frontend request method is POST and the protocol is GRPCS.

    Set the path to any of the following:
    • /
    • /{Package name}.{Service name}
    • /{Package name}.{Service name}/{Method name}
    • Obtain the package name, service name, and method name from the proto file.
    • Absolute match can be used only when the frontend path is set to /{Package name}.{Service name}/{Method name}.
    • Base64 encoding is not supported.

  5. Configure the authentication mode by referring to 5.b.
  6. Click Next.
  7. Configure the default backend by referring to 1.

    The backend service type of gRPC APIs can be GRPC&GRPCS or FunctionGraph.
    • When the type is GRPC&GRPCS, the backend service uses the POST request method, / path, and GRPC or GRPCS protocol, and does not support parameter orchestration.
    • When the type is FunctionGraph, the backend service uses V2 network architecture and Synchronous invocation type by default, and does not support parameter orchestration.

      gRPC APIs with a FunctionGraph backend are supported in CN Southwest-Guiyang1, CN East-Shanghai1, CN North-Beijing4, CN East-Shanghai2, and LA-Santiago.

  8. (Optional) Add a backend policy by referring to 5.

(Optional) Creating a Policy

You can create policies for the API after publishing it.

  1. On the APIs tab, click Create Policy.
  2. Select a policy type and set parameters.

  3. Click OK.

Related Documents

Routing gRPC Service Requests