All Products
Search
Document Center

Alibaba Cloud Service Mesh:Use ASMCompressor to define compression configurations for calls between application services

Last Updated:Nov 30, 2023

Different programming languages and frameworks use different compression configurations when compression is performed. You can better maintain and manage compression configurations by adding compression filters that use consistent compression configurations to your applications. The compression configurations include the minimum compressed bytes, compression level, and which responses will be compressed by default. This topic describes how to use ASMCompressor to define compression configurations for calls between application services.

Prerequisites

Feature description

A waypoint proxy supports both Gzip and Brotli algorithms.

  • Gzip is a widely used compression algorithm that is supported by many web servers and browsers. The Gzip compression algorithm is known for its high compression ratio and fast compression speed. It is often used to compress static and dynamic content.

  • Brotli is an advanced compression algorithm that can provide a higher compression ratio and a faster decompression speed, and is widely used for web services. The Brotli compression algorithm is generally used to compress static content, such as HTML, Cascading Style Sheets (CSS), and JavaScript files.

Compared with Gzip, Brotli provides a higher compression ratio but may impact the compression speed. You can select a compression algorithm based on actual application scenarios and performance requirements.

Enable and verify Gzip compression

Step 1: Record the size of a response that is returned when compression is not enabled

  1. Obtain the IP address of the ingress gateway.

    1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

    2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose ASM Gateways > Ingress Gateway.

    3. On the Ingress Gateway page, obtain the service address.

  2. In the address bar of your browser, enter http://{IP address of the ASM gateway}/productpage to access the Bookinfo application.

  3. Turn on the developer mode in the browser and check the size of the response returned for the productpage request.

    As shown in the following figure, the size of the returned response is 5.5 KB.

    215BDB4C-576C-45ae-89ED-C378C13BB925.png

Step 2: Enable Gzip compression

  1. Create an ingressgateway-gzip.yaml file with the following content.

    For more information about the fields, see Description of ASMCompressor fields.

    Expand to view the ingressgateway-gzip.yaml

    apiVersion: istio.alibabacloud.com/v1beta1
    kind: ASMCompressor
    metadata:
      name: ingressgateway-gzip
      namespace: istio-system
    spec:
      compressor_library:
        gzip:
          compression_level: BEST_COMPRESSION
          compression_strategy: DEFAULT_STRATEGY
          memory_level: 9
          window_bits: 15
      isGateway: true
      portNumber: 80
      response_direction_config:
        common_config:
          content_type:
          - application/json
          - text/plain
          - text/html
          min_content_length: 100
        disable_on_etag_header: true
      workloadSelector:
        labels:
          istio: ingressgateway
  2. Run the following command to enable Gzip compression:

    kubectl apply -f ingressgateway-gzip.yaml

Step 3: View the results obtained after Gzip compression is enabled

  1. In the address bar of your browser, enter http://{IP address of the ASM gateway}/productpage to access the Bookinfo application.

  2. Turn on the developer mode of the browser and check the size of the response returned for the productpage request.

    As shown in the following figure, the size of the returned response changes from 5.5 KB to 1.8 KB, which indicates that the Gzip compression configuration takes effect.

    22.png

Enable and Verify Brotli compression

  1. Modify the virtual service named bookinfo.

    1. Add the following content to the spec.http field of the bookinfo.yaml file:

      - directResponse:
              body:
                string: >-
                  Hello, world! Hello, world! Hello, world! Hello, world! Hello,
                  world! Hello, world! Hello, world! Hello, world! Hello, world!
                  Hello, world!
                   Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world!
                    Hello, world! Hello, world! Hello, world! Hello, world!
              status: 200
            match:
              - uri:
                  prefix: /helloworld

      Expand to view the modified bookinfo.yaml file

      apiVersion: networking.istio.io/v1beta1
      kind: VirtualService
      metadata:
        name: bookinfo
      spec:
        gateways:
          - bookinfo-gateway
        hosts:
          - '*'
        http:
          - directResponse:
              body:
                string: >-
                  Hello, world! Hello, world! Hello, world! Hello, world! Hello,
                  world! Hello, world! Hello, world! Hello, world! Hello, world!
                  Hello, world!
                   Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world!
                    Hello, world! Hello, world! Hello, world! Hello, world!
              status: 200
            match:
              - uri:
                  prefix: /helloworld
          - match:
              - uri:
                  exact: /productpage
              - uri:
                  prefix: /static
              - uri:
                  exact: /login
              - uri:
                  exact: /logout
              - uri:
                  prefix: /api/v1/products
            route:
              - destination:
                  host: productpage
                  port:
                    number: 9080
      
    2. Run the following command to deploy the virtual service:

      kubectl apply -f bookinfo.yaml
  2. Enable Brotli compression.

    1. Use the following content to create an ingressgateway-brotli.yaml file.

      For more information about the fields, see Description of ASMCompressor fields.

      Expand to view the ingressgateway-brotli.yaml file

      apiVersion: istio.alibabacloud.com/v1beta1
      kind: ASMCompressor
      metadata:
        name: ingressgateway-brotli
        namespace: istio-system
      spec:
        compressor_library:
          brotli:
            quality: 5
            window_bits: 15
        isGateway: true
        portNumber: 80
        response_direction_config:
          common_config:
            content_type:
            - application/json
            - text/plain
            - text/html
            min_content_length: 100
          disable_on_etag_header: true
        workloadSelector:
          labels:
            istio: ingressgateway
    2. Run the following command to enable Brotli compression:

      kubectl apply -f ingressgateway-brotli.yaml
  3. Check whether Brotli compression takes effect.

    1. Run the following command to compress the response for the /helloworld request path by using Brotli compression and save the compressed content to the out.br file:

      curl -I -H "Accept-Encoding: br" http://{IP address of the ASM gateway}/helloworld > out.br
    2. Install Brotli.

      • macOS: Run the brew install brotli command in Brew to install Brotli.

      • Windows: Install Brotli on the brotli page.

    3. Run the following command in Brotli to decompress the out.br file:

      brotli -d out.br

      You can see the decompressed file and its content, which indicates that the Brotli compression configuration takes effect.