All Products
Search
Document Center

Alibaba Cloud Service Mesh:Gunakan ConcurrencyLimitingPolicy untuk mengimplementasikan pembatasan konkurensi permintaan

Last Updated:Jun 28, 2025

Anda dapat menggunakan fitur ConcurrencyLimitingPolicy dari suite penjadwalan lalu lintas ASM untuk membatasi jumlah permintaan bersamaan yang dikirim ke layanan (yaitu, jumlah permintaan yang sedang diproses), sehingga mencegah kelebihan beban layanan. Kebijakan ini mencatat jumlah permintaan yang sedang diproses dan menolak permintaan baru jika jumlah tersebut melebihi ambang batas tertentu. Topik ini menjelaskan cara menggunakan ConcurrencyLimitingPolicy untuk membatasi jumlah permintaan bersamaan.

Prasyarat

Langkah 1: Buat kebijakan pembatasan konkurensi menggunakan bidang ConcurrencyLimitingPolicy

  1. Gunakan kubectl untuk terhubung ke instance ASM. Untuk informasi lebih lanjut, lihat Gunakan kubectl pada control plane untuk mengakses sumber daya Istio.

  2. Buat file ConcurrencyLimitPolicy.yaml dengan konten berikut:

    apiVersion: istio.alibabacloud.com/v1
    kind: ConcurrencyLimitingPolicy
    metadata:
      name: concurrencylimit
      namespace: istio-system
    spec:
      concurrency_limiter:
        max_concurrency: 1
        parameters:
          max_inflight_duration: 60s
        selectors:
        - service: httpbin.default.svc.cluster.local

    Tabel berikut menjelaskan beberapa bidang. Untuk informasi lebih lanjut, lihat Deskripsi Bidang ConcurrencyLimitingPolicy.

    Bidang

    Deskripsi

    max_concurrency

    Jumlah maksimum permintaan bersamaan. Dalam contoh ini, bidang ini disetel ke 1, yang menunjukkan bahwa layanan hanya diizinkan memproses satu permintaan pada satu waktu.

    max_inflight_duration

    Periode timeout untuk pemrosesan permintaan. Karena kejadian tak terduga seperti restart pod dalam kluster, suite penjadwalan lalu lintas ASM mungkin gagal mencatat peristiwa penghentian permintaan. Untuk mencegah permintaan semacam itu memengaruhi penilaian algoritma pembatasan konkurensi, Anda perlu menentukan periode timeout untuk pemrosesan permintaan. Jika permintaan belum direspons sebelum periode timeout ini, sistem menganggap bahwa permintaan tersebut telah diproses. Anda dapat menyetel bidang ini dengan mengevaluasi waktu respons maksimum yang diharapkan dari sebuah permintaan. Dalam contoh ini, bidang ini disetel ke 60 detik.

    selectors

    Layanan tempat kebijakan pembatasan konkurensi diterapkan. Dalam contoh ini, layanan httpbin.default.svc.cluster.local digunakan, yang menunjukkan bahwa kebijakan pembatasan konkurensi diterapkan pada layanan httpbin.default.svc.cluster.local.

  3. Jalankan perintah berikut untuk mengaktifkan kebijakan pembatasan konkurensi:

    kubectl apply -f ConcurrencyLimitingPolicy.yaml 

    Keluaran yang Diharapkan:

    concurrencylimitingpolicy.istio.alibabacloud.com/concurrencylimit created

Langkah 2: Verifikasi bahwa kebijakan pembatasan konkurensi berlaku

  1. Jalankan perintah berikut untuk membuka baris perintah shell untuk layanan sleep:

    kubectl exec -it deploy/sleep -- sh
  2. Jalankan perintah berikut untuk mengirim permintaan dari backend yang membutuhkan waktu 30 detik untuk selesai, lalu kirim permintaan kedua dalam waktu 30 detik.

    curl httpbin:8000/delay/30 -I &
    curl httpbin:8000 -I

    Keluaran yang Diharapkan:

    HTTP/1.1 429 Too Many Requests
    date: Fri, 26 Jul 2024 13:50:55 GMT
    server: envoy
    x-envoy-upstream-service-time: 1
    transfer-encoding: chunked
    
    ~ $ HTTP/1.1 200 OK
    server: envoy
    date: Fri, 26 Jul 2024 13:51:05 GMT
    content-type: application/json
    content-length: 269
    access-control-allow-origin: *
    access-control-allow-credentials: true
    x-envoy-upstream-service-time: 10006
    
    [1]+  Done                       curl httpbin:8000/delay/30 -I

    Keluaran menunjukkan bahwa respons dengan kode status HTTP 429 dikembalikan untuk permintaan kedua. Ini membuktikan bahwa kebijakan pembatasan konkurensi berlaku.

Referensi

Anda dapat mengidentifikasi apakah ConcurrencyLimitingPolicy berlaku di Grafana. Pastikan bahwa instance Prometheus untuk Grafana telah dikonfigurasi dengan suite penjadwalan lalu lintas ASM.

Anda dapat mengimpor konten berikut ke Grafana untuk membuat dasbor untuk ConcurrencyLimitingPolicy.

Klik untuk Melihat Detail

{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": {
          "type": "grafana",
          "uid": "-- Grafana --"
        },
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 0,
  "id": 41,
  "links": [],
  "liveNow": false,
  "panels": [
    {
      "datasource": {
        "type": "prometheus",
        "uid": "${datasource}"
      },
      "description": "Sinyal berasal dari eksekusi berkala query: (sum(rate(concurrency_limiter_counter_total{policy_name=\"concurrencylimit\",component_id=\"root.0\",decision_type=\"DECISION_TYPE_ACCEPTED\"}[30s])) / sum(rate(concurrency_limiter_counter_total{policy_name=\"concurrencylimit\",component_id=\"root.0\"}[30s]))) * 100",
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisCenteredZero": false,
            "axisColorMode": "text",
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 10,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          },
          "unit": ""
        },
        "overrides": []
      },
      "gridPos": {
        "h": 10,
        "w": 24,
        "x": 0,
        "y": 0
      },
      "id": 1,
      "interval": "10s",
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom",
          "showLegend": true
        },
        "tooltip": {
          "mode": "single",
          "sort": "none"
        }
      },
      "pluginVersion": "v10.1.0",
      "targets": [
        {
          "datasource": {
            "type": "prometheus",
            "uid": "${datasource}"
          },
          "editorMode": "code",
          "expr": "(sum by (policy_name) (rate(concurrency_limiter_counter_total{component_id=\"root.0\",decision_type=\"DECISION_TYPE_ACCEPTED\"}[30s])) / sum by (policy_name) (rate(concurrency_limiter_counter_total{component_id=\"root.0\"}[30s]))) * 10",
          "intervalFactor": 1,
          "legendFormat": "accepted percentage",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Accept Percentage",
      "type": "timeseries"
    },
    {
      "datasource": {
        "type": "prometheus",
        "uid": "${datasource}"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "noValue": "No data",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue",
                "value": null
              }
            ]
          },
          "unit": "short"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 10,
        "w": 8,
        "x": 0,
        "y": 10
      },
      "id": 2,
      "options": {
        "colorMode": "value",
        "graphMode": "area",
        "justifyMode": "center",
        "orientation": "horizontal",
        "reduceOptions": {
          "calcs": [
            "lastNotNull"
          ],
          "fields": "",
          "values": false
        },
        "textMode": "auto"
      },
      "pluginVersion": "10.0.9",
      "targets": [
        {
          "datasource": {
            "type": "prometheus",
            "uid": "${datasource}"
          },
          "editorMode": "code",
          "expr": "sum by (policy_name) (increase(concurrency_limiter_counter_total{component_id=\"root.0\"}[$__range]))",
          "instant": false,
          "intervalFactor": 1,
          "legendFormat": "{{ policy_name }}",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Total Requests",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "prometheus",
        "uid": "${datasource}"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "noValue": "No data",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              }
            ]
          },
          "unit": "short"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 10,
        "w": 8,
        "x": 8,
        "y": 10
      },
      "id": 3,
      "options": {
        "colorMode": "value",
        "graphMode": "area",
        "justifyMode": "center",
        "orientation": "horizontal",
        "reduceOptions": {
          "calcs": [
            "lastNotNull"
          ],
          "fields": "",
          "values": false
        },
        "textMode": "auto"
      },
      "pluginVersion": "10.0.9",
      "targets": [
        {
          "datasource": {
            "type": "prometheus",
            "uid": "${datasource}"
          },
          "editorMode": "code",
          "expr": "sum by (policy_name)(increase(concurrency_limiter_counter_total{component_id=\"root.0\", decision_type=\"DECISION_TYPE_ACCEPTED\"}[$__range]))",
          "instant": false,
          "intervalFactor": 1,
          "legendFormat": "{{ policy_name }}",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Total Accepted Requests",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "prometheus",
        "uid": "${datasource}"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "mappings": [],
          "noValue": "No rejected requests",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "red",
                "value": null
              }
            ]
          },
          "unit": "short"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 10,
        "w": 8,
        "x": 16,
        "y": 10
      },
      "id": 4,
      "options": {
        "colorMode": "value",
        "graphMode": "area",
        "justifyMode": "center",
        "orientation": "horizontal",
        "reduceOptions": {
          "calcs": [
            "lastNotNull"
          ],
          "fields": "",
          "values": false
        },
        "textMode": "auto"
      },
      "pluginVersion": "10.0.9",
      "targets": [
        {
          "datasource": {
            "type": "prometheus",
            "uid": "${datasource}"
          },
          "editorMode": "code",
          "expr": "sum by (policy_name)(increase(concurrency_limiter_counter_total{component_id=\"root.0\", decision_type=\"DECISION_TYPE_REJECTED\"}[$__range]))",
          "instant": false,
          "intervalFactor": 1,
          "legendFormat": "{{ instance }} - {{ policy_name }}",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Total Rejected Requests",
      "type": "stat"
    },
    {
      "datasource": {
        "type": "prometheus",
        "uid": "${datasource}"
      },
      "description": "",
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisCenteredZero": false,
            "axisColorMode": "text",
            "axisLabel": "Decisions",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 10,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          },
          "unit": "reqps"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 10,
        "w": 24,
        "x": 0,
        "y": 20
      },
      "id": 5,
      "interval": "10s",
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom",
          "showLegend": true
        },
        "tooltip": {
          "mode": "single",
          "sort": "none"
        }
      },
      "pluginVersion": "v10.1.0",
      "targets": [
        {
          "datasource": {
            "type": "prometheus",
            "uid": "${datasource}"
          },
          "editorMode": "code",
          "expr": "sum by(decision_type, policy_name) (rate(concurrency_limiter_counter_total{component_id=\"root.0\"}[$__rate_interval]))",
          "intervalFactor": 1,
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Concurrency Limiter Decision Overview",
      "type": "timeseries"
    }
  ],
  "refresh": "",
  "schemaVersion": 38,
  "style": "dark",
  "tags": [],
  "templating": {
    "list": [
      {
        "hide": 0,
        "includeAll": false,
        "label": "Data Source",
        "multi": false,
        "name": "datasource",
        "options": [],
        "query": "prometheus",
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "type": "datasource"
      }
    ]
  },
  "time": {
    "from": "now-5m",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "browser",
  "title": "Policy Summary - concurrencylimit",
  "version": 1,
  "weekStart": ""
}

Dasbor yang dihasilkan adalah sebagai berikut.

image