All Products
Search
Document Center

Alibaba Cloud Service Mesh:Konfigurasikan kompresi HTTP dengan ASMCompressor

Last Updated:Jun 21, 2026

Saat layanan mikro berkomunikasi, muatan respons dapat mengonsumsi bandwidth signifikan, terutama untuk konten berbasis teks seperti JSON, HTML, dan CSS. ASMCompressor memungkinkan Anda menambahkan filter kompresi di lapisan proxy sehingga semua layanan berbagi konfigurasi kompresi yang konsisten, alih-alih menerapkan kompresi secara terpisah di setiap bahasa atau framework. Topik ini menjelaskan cara menggunakan ASMCompressor di ASM untuk menentukan pengaturan kompresi pada panggilan antar-layanan.

Prasyarat

Ikhtisar fitur

Proxy mendukung dua algoritma kompresi: Gzip dan Brotli.

  • Gzip adalah algoritma kompresi yang banyak digunakan dan didukung oleh banyak server web dan browser, dikenal karena rasio kompresi tinggi dan kecepatan kompresi cepat. Algoritma ini biasanya digunakan untuk mengompresi konten statis maupun dinamis.

  • Brotli adalah algoritma kompresi lanjutan yang memberikan rasio kompresi lebih tinggi dan kecepatan dekompresi lebih cepat, serta banyak digunakan di web. Brotli biasanya digunakan untuk mengompresi konten statis, seperti file HTML, CSS, dan JavaScript.

Brotli menawarkan rasio kompresi lebih tinggi daripada Gzip, tetapi proses kompresinya mungkin lebih lambat.

Aktifkan dan verifikasi kompresi Gzip

Langkah 1: Catat ukuran respons garis dasar

  1. Dapatkan alamat gerbang masuk.

    1. Masuk ke Konsol ASM. Di panel navigasi kiri, pilih Service Mesh > Mesh Management.

    2. Pada halaman Mesh Management, klik nama instans ASM. Di panel navigasi kiri, pilih ASM Gateways > Ingress Gateway.

    3. Pada halaman Ingress Gateway, peroleh Service address.

  2. Masukkan http://{ASM_GATEWAY_ADDRESS}/productpage di bilah alamat browser Anda untuk mengakses aplikasi Bookinfo. Ganti {ASM_GATEWAY_ADDRESS} dengan alamat yang telah Anda peroleh.

  3. Buka developer tools browser Anda dan periksa ukuran respons untuk permintaan productpage.

    Ukuran respons adalah 5,5 KB.

Langkah 2: Aktifkan kompresi Gzip

  1. Buat file bernama ingressgateway-gzip.yaml dengan konten berikut.

    Untuk deskripsi bidang-bidangnya, lihat ASMCompressor CRD reference.

    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
      request_direction_config:  # Nonaktifkan kompresi permintaan.
        common_config:
          enabled:
            default_value: false
            runtime_key: NOT_EXISTENT_KEY
      response_direction_config:  # Aktifkan kompresi respons.
        common_config:
          content_type:
          - application/json
          - text/plain
          - text/html
          min_content_length: 100
        disable_on_etag_header: true
      workloadSelector:
        labels:
          istio: ingressgateway
  2. Jalankan perintah berikut untuk mengaktifkan kompresi Gzip:

    kubectl apply -f ingressgateway-gzip.yaml

Langkah 3: Verifikasi kompresi Gzip

  1. Masukkan http://{ASM_GATEWAY_ADDRESS}/productpage di bilah alamat browser Anda untuk mengakses aplikasi Bookinfo.

  2. Buka developer tools browser Anda dan periksa ukuran respons untuk permintaan productpage.

    Ukuran respons turun dari 5,5 KB menjadi 1,8 KB. Hal ini mengonfirmasi bahwa kompresi Gzip telah diaktifkan.

Langkah 4: Verifikasi kompresi Brotli

  1. Modifikasi VirtualService bookinfo.

    1. Tambahkan konten berikut di bawah spec.http dalam file bookinfo.yaml.

      - 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

      Konten YAML yang telah diperbarui adalah sebagai berikut:

      Modified bookinfo.yaml

      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. Jalankan perintah berikut untuk menerapkan VirtualService:

      kubectl apply -f bookinfo.yaml
  2. Aktifkan kompresi Brotli.

    1. Buat file bernama ingressgateway-brotli.yaml dengan konten berikut.

      Untuk deskripsi bidang-bidangnya, lihat ASMCompressor CRD reference.

      ingressgateway-brotli.yaml

      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
        request_direction_config:  # Nonaktifkan kompresi permintaan.
          common_config:
            enabled:
              default_value: false
              runtime_key: NOT_EXISTENT_KEY
        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. Jalankan perintah berikut untuk mengaktifkan kompresi Brotli:

      kubectl apply -f ingressgateway-brotli.yaml
  3. Verifikasi bahwa kompresi Brotli telah diaktifkan.

    1. Jalankan perintah berikut untuk menyimpan respons dari path /helloworld ke file out.br:

      curl -I  -H "Accept-Encoding: br" http://{ASM_GATEWAY_ADDRESS}/helloworld > out.br
    2. Instal Brotli.

      • macOS: Jalankan perintah brew install brotli untuk menginstal.

      • Di Windows, lihat petunjuk instalasi di brotli.

    3. Jalankan perintah brotli untuk mendekompresi file out.br:

      brotli -d out.br

      Pembuatan file out yang telah didekompresi dengan konten yang benar mengonfirmasi bahwa kompresi Brotli telah diaktifkan.

Langkah 5: Verifikasi konfigurasi per-route

  1. Setelah konfigurasi pada Langkah 4, kompresi diaktifkan untuk respons di semua path. Anda dapat mengatur IP gateway sebagai variabel lingkungan dan menjalankan perintah berikut:

    export ASM_GATEWAY_IP=<your_gateway_ip>
    curl ${ASM_GATEWAY_IP}/helloworld > helloworld_no_compress.html
    curl -H "Accept-Encoding: br" ${ASM_GATEWAY_IP}/helloworld > helloworld_compress.html
    curl ${ASM_GATEWAY_IP}/productpage > productpage_no_compress.html
    curl -H "Accept-Encoding: br" ${ASM_GATEWAY_IP}/productpage > productpage_compress.html
  2. Jalankan perintah ls untuk membandingkan ukuran respons untuk kedua path ini sebelum dan sesudah kompresi:

    ls -lh
    total 40
    -rw-r--r--@ 1 user  staff    37B  5 28 18:16 helloworld_compress.html
    -rw-r--r--@ 1 user  staff   324B  5 28 18:15 helloworld_no_compress.html
    -rw-r--r--@ 1 user  staff   1,2K  5 28 18:17 productpage_compress.html
    -rw-r--r--@ 1 user  staff   5,2K  5 28 18:16 productpage_no_compress.html
  3. Di VirtualService, tambahkan nama ke route helloworld. File bookinfo.yaml yang telah dimodifikasi adalah sebagai berikut:

    Modified YAML

    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
          name: helloworld-route    # Tentukan nama route agar filter kompresi dapat mencocokkannya.
        - 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
  4. Tambahkan konfigurasi per-route ke ASMCompressor dan nonaktifkan kompresi pada route helloworld. ASMCompressor yang telah dimodifikasi adalah sebagai berikut:

    apiVersion: istio.alibabacloud.com/v1beta1
    kind: ASMCompressor
    metadata:
      name: ingressgateway-gzip
      namespace: istio-system
    spec:
      compressor_library:
        brotli:
          quality: 5
          window_bits: 15
      isGateway: true
      portNumber: 80
      request_direction_config:  # Nonaktifkan kompresi permintaan.
        common_config:
          enabled:
            default_value: false
            runtime_key: NOT_EXISTENT_KEY
      response_direction_config:
        common_config:
          content_type:
          - application/json
          - text/plain
          - text/html
          min_content_length: 100
        disable_on_etag_header: true
      per_route_configs:  # Tambahkan konfigurasi per-route.
      - disabled: true
        route_match:
          vhost:
            route:
              name: helloworld-route  # Masukkan nama route yang dikonfigurasi di VirtualService di sini.
      workloadSelector:
        labels:
          istio: ingressgateway
  5. Jalankan perintah berikut untuk memverifikasi bahwa kompresi dinonaktifkan untuk respons dari path /helloworld:

    curl -H "Accept-Encoding: br" ${ASM_GATEWAY_IP}/helloworld > helloworld_route_disabled.html
  6. Jalankan kembali perintah ls untuk memeriksa ukuran respons saat ini dari path /helloworld.

    ls -lh
    total 48
    -rw-r--r--@ 1 user  staff    37B  5 28 18:16 helloworld_compress.html
    -rw-r--r--@ 1 user  staff   324B  5 28 18:15 helloworld_no_compress.html
    -rw-r--r--@ 1 user  staff   324B  5 28 19:29 helloworld_route_disabled.html
    -rw-r--r--@ 1 user  staff   1,3K  5 28 19:30 productpage_compress.html
    -rw-r--r--@ 1 user  staff   5,2K  5 28 18:16 productpage_no_compress.html

    Hal ini mengonfirmasi bahwa pengaturan kompresi tingkat route berfungsi sebagaimana mestinya.