Updated on 2024-07-01 GMT+08:00

Implementation Procedure

Prerequisites

  • Both the client and server are of the gRPC type.
  • The server 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 server.

Creating an API

  1. Log in to the APIG console.
  2. Select a gateway at the top of the navigation pane.
  3. In the navigation pane, choose API Management > APIs.
  4. Click Create API > Create gRPC API. For details, see Creating a gRPC API.
  5. Configure frontend information and click Next.

    Table 1 Frontend configuration

    Parameter

    Description

    API Name

    Enter a name that conforms to specific rules to facilitate search.

    Group

    The default option is DEFAULT.

    URL

    Method: Request method of the API. Default: POST.

    Protocol: Request protocol of the API. Default: GRPCS.

    Subdomain Name: The system automatically allocates a subdomain name to each API group for internal testing. The subdomain name can be accessed 1000 times a day.

    Path: Path for requesting the API. In this example, enter /helloworld.Greeter. For details about the request path, see the proto file. helloworld indicates the package name, and Greeter indicates the service name.

    Gateway Response

    Select a response to be displayed if the gateway fails to process an API request.

    The default gateway response is default.

    Matching

    Select Prefix match.

    Authentication Mode

    API authentication mode. Select None.

  6. Configure the backend information and click Next.

    Table 2 Backend configuration

    Parameter

    Description

    Load Balance Channel

    Determine whether the backend service will be accessed using a load balance channel. For this example, select Skip.

    URL

    Method: Request method of the API. Default: POST.

    Protocol: Request protocol of the backend service. Default: GRPCS.

    Backend Address: Address and port of the backend service.

    Path: Path of the backend service. In this example, enter /.

  7. Click Finish.

    On the APIs page, the API URL only shows the domain name and path. It does not show the request method or protocol. When sending a gRPC request, enter the domain name.

Publishing the API

  1. On the APIs tab, select the created API and click Publish.
  2. Select the environment where the API will be published, and enter a description.
  3. Click OK. After the API is published, the red exclamation mark (!) in the upper left corner of the Publish button disappears.

Verifying the API

Use the API test tool to invoke the created API, or invoke this API on a client.

  1. Enter the debugging domain name of the API's group.
  2. Import the proto file of the server.

    The proto file contains the following content:
    syntax = "proto3";
    package helloworld;
    // The greeting service definition.
    service Greeter {
    // Sends a greeting
    rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    // The request message containing the user's name.
    message HelloRequest {
    string name = 1;
    }
    // The response message containing the greetings
    message HelloReply {
    string message = 1;
    } 
    • helloworld: package name
    • Greeter: service name
    • SayHello: method name
    • HelloRequest: request body
    • HelloReply: response body

  3. Enter an API request in the message area according to the proto file.

    {
        "name": "world"
    }

  4. Click Invoke to send the request.
  5. View the response in the Response area. If status code 0 OK is displayed, the invocation is successful.