All Products
Search
Document Center

Microservices Engine:Route requests of gRPC applications based on cloud-native gateways

Last Updated:Feb 06, 2024

gRPC uses the HTTP/2 protocol and supports features such as bidirectional streaming, header compression, and multiplexing. This improves the efficiency of network transmission. In this example, a gRPC application is deployed in a Container Service for Kubernetes cluster and a cloud-native gateway is used to route requests of the gRPC application.

Prerequisites

Step 1: Deploy a gRPC application in the ACK cluster

For more information about how to deploy an application, see Create a stateless application by using a Deployment.

In this example, the ACK cluster is used for service discovery. The backend service is registered with CoreDNS by using annotation-based service APIs. This example uses an application named grpcbin as a backend service to implement multiple gRPC services. The following code shows the configuration of the resource that is used by the grpcbin application in the ACK cluster.

Note

The name of the gRPC service in the port field of the Kubernetes service must contain grpc.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: grpcbin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grpcbin
  template:
    metadata:
      labels:
        app: grpcbin
    spec:
      serviceAccountName: grpcbin
      containers:
      - image: docker.io/moul/grpcbin
        imagePullPolicy: IfNotPresent
        name: grpcbin
        ports:
        - containerPort: 9000
        - containerPort: 9001
        resources:
            requests:
              cpu: '1'
              memory: 2Gi
            limit:
              cpu: '1'
              memory: 2Gi
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: grpcbin
---
apiVersion: v1
kind: Service
metadata:
  name: grpcbin-grpc
  labels:
    app: grpcbin
spec:
  ports:
  - name: grpc
    port: 9000
    targetPort: 9000
  selector:
    app: grpcbin

The following code shows the proto definition for the gRPC service.

syntax = "proto3";

package grpcbin;

service GRPCBin {
  rpc Index(EmptyMessage) returns (IndexReply) {}
  // The method that is called without parameters specified. An empty response is returned after the call.
  rpc Empty(EmptyMessage) returns (EmptyMessage) {}
  // The request parameters are returned.
  rpc DummyUnary(DummyMessage) returns (DummyMessage) {}
  // A streaming call is initiated for the response, and 10 response messages are returned.
  rpc DummyServerStream(DummyMessage) returns (stream DummyMessage) {}
  // A streaming call is initiated for the request, 10 requests are received, and the last request body is returned.
  rpc DummyClientStream(stream DummyMessage) returns (DummyMessage) {}
  // The streaming method that is used for both requests and responses.
  rpc DummyBidirectionalStreamStream(stream DummyMessage) returns (stream DummyMessage) {}
  // After this method is called, a specified gRPC error is returned.
  rpc SpecificError(SpecificErrorRequest) returns (EmptyMessage) {}
  // After this method is called, a random error is returned.
  rpc RandomError(EmptyMessage) returns (EmptyMessage) {}
  // After this method is called, a header is returned.
  rpc HeadersUnary(EmptyMessage) returns (HeadersMessage) {}
  // After this method is called, no response is returned.
  rpc NoResponseUnary(EmptyMessage) returns (EmptyMessage) {}
}

message HeadersMessage {
  message Values {
    repeated string values = 1;
  }
  map<string, Values> Metadata = 1;
}

message SpecificErrorRequest {
  uint32 code = 1;
  string reason = 2;
}

message EmptyMessage {}

message DummyMessage {
  message Sub {
    string f_string = 1;
  }
  enum Enum {
    ENUM_0 = 0;
    ENUM_1 = 1;
    ENUM_2 = 2;
  }
  string f_string = 1;
  repeated string f_strings = 2;
  int32 f_int32 = 3;
  repeated int32 f_int32s = 4;
  Enum f_enum = 5;
  repeated Enum f_enums = 6;
  Sub f_sub = 7;
  repeated Sub f_subs = 8;
  bool f_bool = 9;
  repeated bool f_bools = 10;
  int64 f_int64 = 11;
  repeated int64 f_int64s= 12;
  bytes f_bytes = 13;
  repeated bytes f_bytess = 14;
  float f_float = 15;
  repeated float f_floats = 16;
}

message IndexReply {
  message Endpoint {
    string path = 1;
    string description = 2;
  }
  string description = 1;
  repeated Endpoint endpoints = 2;
}

The underlying layer of the gRPC service is based on the HTTP/2 protocol, and the Path field is in the format of {Package name}.{Service name}/{Method name}. MSE cloud-native gateways can use the Path field to implement gRPC routing. For example, if you use gRPC to access the Index method of grpcbin, the value of the Path field for the HTTP/2 protocol is grpcbin.GRPCBin/Index.

Step 2: Use the cloud-native gateway to route gRPC requests

Add the ACK cluster as a service source of the cloud-native gateway and add the grpcbin service.

Add a service source

  1. Log on to the MSE console, and select a region in the top navigation bar.

  2. In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the name of the gateway.

  3. In the left-side navigation pane, choose Service Management > Sources.

  4. On the Sources page, click Add Source in the upper-left corner. In the Add Source panel, configure the parameters, and click OK.

    Parameter

    Description

    Source Type

    Select Container Service.

    ACK/ACK Serverless Cluster

    Select the cluster in which your backend service is deployed.

    Listen to Kubernetes Ingress

    If you turn on the switch, the cloud-native gateway automatically listens to the changes of Ingress resources and makes the listened configurations of domain names and routes of the Ingress resources take effect.

    If you turn off the switch, the cloud-native gateway no longer listens to the changes of Ingress resources and makes the listened configurations of domain names and routes of the Ingress resources become ineffective.

    Note

    The priorities of the domain names and routes that are manually configured in the MSE console are higher than the priorities of the listened domain names and routes of the Ingress resources.

Add a service

  1. Log on to the MSE console, and select a region in the top navigation bar.

  2. In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the name of the gateway.

  3. In the left-side navigation pane, choose Service Management > Services.

  4. On the Services page, click Create Service in the upper-left corner. In the Create Service panel, configure the parameters, and click OK.

    Parameter

    Description

    Service Source

    Select ACK Container.

    Namespace

    Select the namespace of the cluster.

    Services

    Select one or more services from the Select Service section.

Add a route from the gateway to the grpcbin service

  1. Log on to the MSE console, and select a region in the top navigation bar.

  2. In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the name of the gateway.

  3. In the left-side navigation pane, choose Routes > Route Settings.

  4. In the upper-left corner of the Routes page, click Create Route.

  5. In the Create Route panel, configure the parameters, and click Save and Online.

    1. In the Request Information wizard, configure the parameters and click The next step.

      Parameter

      Description

      Route Name

      Enter grpc.

      Associate Domain Name

      Select the default associated domain name * from the drop-down list.

      Match Rule

      Select Prefix Match from the Path matching condition drop-down list. Enter a path that starts with /grpcbin.GRPCBin.

      Note

      The path of the gRPC service is in the {Package name}.{Service name}/{Method name} format. You can configure the required route prefix.

    2. In the Target service wizard, select Single Service for Destination Service, select the destination service and service port number, and then click Save and Online.

Verify the result

Use BloomRPC to test the gRPC service availability. If a response is returned, the gRPC service is available.

You can also use other gRPC clients for testing.