All Products
Search
Document Center

:Shape traffic to gRPC servers

Last Updated:Jun 05, 2023

This topic describes how to shape traffic to gRPC servers based on the gRPC version and gRPC API in the Service Mesh (ASM) console.

Shape traffic to gRPC servers based on the gRPC version

A gRPC service is deployed on each of the Java, Go, Node.js, and Python gRPC servers. The following example shows how to route requests from gRPC clients to the gRPC service that is deployed on the Java gRPC server.Version

  1. Log on to the ASM console.

  2. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  3. On the Mesh Management page, find the ASM instance that you want to configure. Click the name of the ASM instance or click Manage in the Actions column.

  4. In the Control Plane section, click the DestinationRule tab and then Create.

  5. In the Create panel, select the required namespace from the Namespaces drop-down list. Copy the following content to the code editor. Then, click OK.

    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
      namespace: grpc-best
      name: grpc-server-dr
    spec:
      host: grpc-server-svc
      subsets:
        - name: v1
          labels:
            version: v1
        - name: v2
          labels:
            version: v2
        - name: v3
          labels:
            version: v3
        - name: v4
          labels:
            version: v4
  6. In the Control Plane section, click the VirtualService tab and then Create.

  7. In the Create panel, select the required namespace from the Namespaces drop-down list. Copy the following content to the code editor. Then, click OK.

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      namespace: grpc-best
      name: grpc-server-vs
    spec:
      hosts:
        - "*"
      gateways:
        - grpc-gateway
      http:
        - match:
            - port: 9996
          route:
            - destination:
                host: grpc-server-svc
                subset: v1
              weight: 100

    Run the following command to check whether all the requests are routed to the Java gRPC service:

    for i in {1..100}; do
      docker exec -e GRPC_SERVER="${INGRESS_IP}" -it "$client_node_container" node mesh_client.js >> mesh_result
    done
    sort mesh_result | grep -v "^[[:space:]]*$"| uniq -c | sort -nrk1

    Expected output:

     100 TalkOneAnswerMore:JAVA
     100 TalkMoreAnswerOne:JAVA
     100 TalkBidirectional:JAVA
     100 Talk:JAVA

Shape traffic to gRPC servers by using the gRPC API operations

You can use the gRPC API operations to shape traffic to gRPC servers in a fine-grained way. The gRPC API operations can be built for the communication models. For more information, see Implement the communication models of gRPC. Four gRPC API operations and four gRPC services in the following programming languages are available: Java, Go, Node.js, and Python. The following example shows how to set a routing rule to route the requests of a gRPC API operation to the gRPC server that uses the same language as the operation.API

  1. Log on to the ASM console.

  2. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  3. On the Mesh Management page, find the ASM instance that you want to configure. Click the name of the ASM instance or click Manage in the Actions column.

  4. In the Control Plane section, click the VirtualService tab and then Create.

  5. In the Create panel, select the required namespace from the Namespaces drop-down list. Copy the following content to the code editor. Then, click OK.

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      namespace: grpc-best
      name: grpc-server-vs
    spec:
      hosts:
        - "*"
      gateways:
        - grpc-gateway
      http:
        - match:
            - port: 9996
            - uri:
                exact: /org.feuyeux.grpc.LandingService/talk
          route:
            - destination:
                host: grpc-server-svc
                subset: v1
              weight: 100
        - match:
            - port: 9996
            - uri:
                exact: /org.feuyeux.grpc.LandingService/talkOneAnswerMore
          route:
            - destination:
                host: grpc-server-svc
                subset: v2
              weight: 100
        - match:
            - port: 9996
            - uri:
                exact: /org.feuyeux.grpc.LandingService/talkMoreAnswerOne
          route:
            - destination:
                host: grpc-server-svc
                subset: v3
              weight: 100
        - match:
            - port: 9996
            - uri:
                exact: /org.feuyeux.grpc.LandingService/talkBidirectional
          route:
            - destination:
                host: grpc-server-svc
                subset: v4
              weight: 100

    Run the following command to check whether the requests of each gRPC API operation are directed to the gRPC server that uses the same language as the operation:

    for i in {1..100}; do
      docker exec -e GRPC_SERVER="${INGRESS_IP}" -it "$client_node_container" node mesh_client.js >> mesh_result
    done
    sort mesh_result | grep -v "^[[:space:]]*$"| uniq -c | sort -nrk1

    Expected output:

     100 TalkOneAnswerMore:GOLANG
     100 TalkMoreAnswerOne:NODEJS
     100 TalkBidirectional:PYTHON
     100 Talk:JAVA