die API zur Verwendung eines API-Schlüssels konfiguriert haben. Das wird von Endpoints benötigt, um das Google Cloud -Projekt zu identifizieren, dem die aufrufende Anwendung zugeordnet ist. Weitere Informationen finden Sie unter Durch einen API-Schlüssel geschützte APIs freigeben.
Kontingent zur gRPC API-Konfigurationsdatei hinzufügen
Im Folgenden wird beschrieben, wie Sie die erforderlichen Einstellungen zum Einrichten von Kontingenten in Ihre gRPC API-Konfigurationsdatei einfügen. Der Einfachheit halber bezieht sich diese Seite auf die gRPC API-Konfigurationsdatei als api_config.yaml-Datei.
Sie fügen der Datei api_config.yaml die folgenden drei Abschnitte hinzu:
metrics: Ein benannter Messwert, der die Zahl der Anfragen an die API ermittelt. Der von Ihnen vergebene Name soll den Zähler beschreiben. Der Name könnte eine Kategorie sein, z. B. read-requests oder write-requests. Wenn Sie ein Kontingent für eine bestimmte Methode definieren, können Sie auch den Methodennamen einbeziehen, z. B. echo-api/echo_requests.
quota.limits: Stellt ein einzelnes durchsetzbares Limit für einen benannten Messwert dar.
Hier konfigurieren Sie die zulässige Zahl der Anfragen für einen von Ihnen definierten Messwert. Derzeit werden nur Limits pro Minute und Projekt unterstützt.
quota.metric_rules: metric_rule ordnet Messwerten Methoden zu (m:n-Beziehung).
Eine Anfrage an eine Methode weist für jeden der zugeordneten Messwerte einen Zähler zu.
Beim Verknüpfen einer Methode mit einem Messwert müssen immer Kosten für die Anfrage definiert werden. Sie können die Kosten für jede Methode einzeln konfigurieren. So können verschiedene Methoden denselben benannten Messwert zu unterschiedlichen Preisen nutzen. Wenn Sie keine komplexen Kontingentanforderungen haben, können Sie die Kosten für alle Messwerte auf 1 festlegen.
So konfigurieren Sie Kontingente für Ihre API:
Öffnen Sie die api_config.yaml-Datei Ihres Projekts in einem Texteditor.
Fügen Sie das Feld metrics nach dem Feld apis auf oberster Ebene der Datei ein (nicht eingerückt oder verschachtelt):
Ersetzen Sie YOUR_LIMIT_NAME durch einen Namen, der das Limit beschreibt.
Ersetzen Sie YOUR_METRIC_NAME durch einen zuvor definierten Messwertnamen (metric.name).
Das Feld unit muss "1/min/{project}" lauten. Dies ist die Kennzeichnung für das Limit "Pro Minute pro Projekt".
Das Feld values muss STANDARD enthalten.
Ersetzen Sie VALUE_FOR_THE_LIMIT durch eine Ganzzahl.
Dies ist die Anzahl von Anfragen, die eine mit dem Google Cloud Projekt eines Nutzers verknüpfte Anwendung in einer Minute senden kann.
Definieren Sie optional weitere Messwerte und Limits für jeden Messwert.
Fügen Sie eine Zeile metric_rules eingerückt unter quota nach dem Abschnitt limits hinzu.
Verknüpfen Sie im Abschnitt metric_rules einen zuvor definierten Messwert so mit einer Methode:
Ersetzen Sie YOUR_METRIC_NAME durch einen zuvor definierten Messwertnamen (metric.name).
Ersetzen Sie YOUR_METRIC_COST durch eine Ganzzahl. Der Anfragezähler des Messwerts wird bei jeder Anfrage um die Zahl erhöht, die Sie für die Kosten angeben.
Für das Feld selector können Sie einen der folgenden Werte angeben:
Mit selector: "*" verknüpfen Sie alle Methoden in allen APIs mit metric_cost.
Mit selector: YOUR_API_NAME.* verknüpfen Sie alle Methoden in einer API mit metric_cost.
Mit selector: YOUR_API_NAME.YOUR_METHOD_NAME verknüpfen Sie eine bestimmte Methode in einer API mit metric_cost.
Speichern Sie die Datei api_config.yaml.
Beispiele für die Kontingentkonfiguration
Die folgenden drei Beispiele zeigen, wie Sie in Ihrer API Kontingente konfigurieren.
Das folgende Beispiel zeigt, wie das Feld metric konfiguriert wird:
Damit das Kontingent wirksam wird, müssen Sie diese Schritte ausführen:
Stellen Sie die api_config.yaml-Datei für Service Management bereit, wodurch die Konfiguration in Endpoints aktualisiert wird. Ausführliche Schritte finden Sie unter Endpoints-Konfiguration bereitstellen.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\u003cp\u003eThis guide details the process of configuring quotas for a gRPC API, involving modifications to the \u003ccode\u003eapi_config.yaml\u003c/code\u003e file and deployment of both the API configuration and the Extensible Service Proxy (ESP).\u003c/p\u003e\n"],["\u003cp\u003eQuotas are defined by adding \u003ccode\u003emetrics\u003c/code\u003e, \u003ccode\u003equota.limits\u003c/code\u003e, and \u003ccode\u003equota.metric_rules\u003c/code\u003e to the \u003ccode\u003eapi_config.yaml\u003c/code\u003e file, where \u003ccode\u003emetrics\u003c/code\u003e defines named counters, \u003ccode\u003equota.limits\u003c/code\u003e sets per-minute, per-project limits, and \u003ccode\u003equota.metric_rules\u003c/code\u003e associates methods with metrics and assigns costs.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003emetrics\u003c/code\u003e section requires a name, display name, \u003ccode\u003eINT64\u003c/code\u003e value type, and \u003ccode\u003eDELTA\u003c/code\u003e metric kind for the request counter.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003equota.limits\u003c/code\u003e section defines the allowed number of requests per minute per project, using the unit \u003ccode\u003e"1/min/{project}"\u003c/code\u003e and a \u003ccode\u003eSTANDARD\u003c/code\u003e value for the limit.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003equota.metric_rules\u003c/code\u003e section links methods to metrics, allowing for different costs per method, and uses selectors to apply metric costs to specific methods or all methods.\u003c/p\u003e\n"]]],[],null,["# Configuring quotas\n\n[OpenAPI](/endpoints/docs/openapi/quotas-configure \"View this page for the Cloud Endpoints OpenAPI docs\") \\| gRPC\n\n\u003cbr /\u003e\n\n|\n| **Beta**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nThis page describes how to configure quotas for your API. At a high level, the\nsteps are:\n\n1. Add the information about the quota to your gRPC API configuration file.\n2. Deploy your gRPC API configuration file.\n3. Deploy the Extensible Service Proxy (ESP).\n\nFor an overview of the functionality provided by quotas, see\n[About quotas](/endpoints/docs/grpc/quotas-overview).\n\nPrerequisites\n-------------\n\nAs a starting point, this page assumes that you have:\n\n- [Configured Cloud Endpoints](/endpoints/docs/grpc/configure-endpoints).\n- [Deployed the Endpoints configuration](/endpoints/docs/grpc/deploy-endpoints-config).\n- [Deployed the API backend](/endpoints/docs/grpc/deploy-api-backend).\n- Configured your API to use an API key. This is needed for Endpoints to identify the Google Cloud project that the calling application is associated with. See [Sharing APIs protected by API key](/endpoints/docs/grpc/restricting-api-access-with-api-keys#sharing_apis_protected_by_api_key) for more information.\n\nAdding a quota to your gRPC API configuration file\n--------------------------------------------------\n\nThe following procedure describes adding the required settings to your gRPC API\nconfiguration file to set up quotas. For simplicity, this page refers to the\ngRPC API configuration file as the `api_config.yaml` file.\n\nYou add the following three sections to the `api_config.yaml` file:\n\n- `metrics`: A named metric that counts requests to your API. You provide a\n name that describes the counter. The name could be a category, such as\n `read-requests` or `write-requests`. Alternatively, if you are defining a\n quota for a specific method, you might want to include the method name, for\n example,`echo-api/echo_requests`.\n\n- `quota.limits`: Represents a single enforceable limit on a named metric.\n This is where you configure the allowed number of requests for a metric that\n you have defined. Currently, only per-minute, per-project limits are\n supported.\n\n- `quota.metric_rules`: A `metric_rule` maps methods to metrics (many-to-many).\n A request to a method allocates a counter for each of the mapped metrics.\n When you associate a method with a metric, you always specify a cost for the\n request. You can configure the cost of each method independently. This\n allows different methods to consume at different rates from the same named\n metric. If you don't have a complex quota requirement, you can configure the\n cost of every metric to 1.\n\nTo configure quotas on your API:\n\n1. Open your project's `api_config.yaml` file in a text editor.\n2. Add the `metrics` field at the top level of the file (not indented or\n nested), after the `apis` field.\\`\n\n ```\n metrics:\n - name: \"YOUR_METRIC_NAME\"\n display_name: \"YOUR_METRIC_DISPLAY_NAME\"\n value_type: INT64\n metric_kind: DELTA`\n ```\n - Replace \u003cvar translate=\"no\"\u003eYOUR_METRIC_NAME\u003c/var\u003e with a name that describes the API requests counter.\n - Replace \u003cvar translate=\"no\"\u003eYOUR_METRIC_DISPLAY_NAME\u003c/var\u003e with the text that is displayed on the **Endpoints** \\\u003e **Services** \\\u003e **Quotas** page to identify the metric.\n - The `value_type` field must be `INT64`.\n - The `metric_kind` field must be `DELTA`.\n3. Add a `quota` field at the same level as `metrics`, and add a `limits` field\n nested within the `quota` section.\n\n ```\n quota:\n limits:\n - name: \"YOUR_LIMIT_NAME\"\n metric: \"YOUR_METRIC_NAME\"\n unit: \"1/min/{project}\"\n values:\n STANDARD: VALUE_FOR_THE_LIMIT\n ```\n - Replace \u003cvar translate=\"no\"\u003eYOUR_LIMIT_NAME\u003c/var\u003e with a name that describes the limit.\n - Replace \u003cvar translate=\"no\"\u003eYOUR_METRIC_NAME\u003c/var\u003e with a previously defined `metric.name`.\n - The `unit` field must be `\"1/min/{project}\"`. This is the identifier for the per-minute per-project limit.\n - The `values` field must contain `STANDARD`.\n - Replace \u003cvar translate=\"no\"\u003eVALUE_FOR_THE_LIMIT\u003c/var\u003e with an integer value. This is the number of requests that an application associated with a consumer's Google Cloud project can make in a minute.\n4. Optionally, define additional metrics and limits for each metric.\n\n5. Add a `metric_rules` line indented under `quota`, after the `limits` section.\n Within the `metric_rules` section, associate a previously defined metric\n with a method, as follows:\n\n ```\n metric_rules:\n - metric_costs:\n YOUR_METRIC_NAME: YOUR_METRIC_COST\n selector: [METHODS]\n ```\n - Replace \u003cvar translate=\"no\"\u003eYOUR_METRIC_NAME\u003c/var\u003e with a previously defined `metric.name`.\n - Replace \u003cvar translate=\"no\"\u003eYOUR_METRIC_COST\u003c/var\u003e with an integer. For each request, the request counter for the metric is incremented by the number you specify for the cost.\n - For the `selector` field, you can specify one of the following:\n\n - To associate all methods in all APIs with the `metric_cost` use `selector: \"*\"`\n - To associate all methods within an API with the `metric_cost` use `selector: `\u003cvar translate=\"no\"\u003eYOUR_API_NAME\u003c/var\u003e`.*`\n - To associate a specific method within an API with the `metric_cost` use `selector: `\u003cvar translate=\"no\"\u003eYOUR_API_NAME\u003c/var\u003e`.`\u003cvar translate=\"no\"\u003eYOUR_METHOD_NAME\u003c/var\u003e\n6. Save the `api_config.yaml` file.\n\n### Quota configuration examples\n\nThe following three examples show how to configure quotas on your API.\n\nThe following example shows how configure the `metric` field: \n\n```carbon\nmetrics:\n # Define a metric for read requests.\n - name: \"read-requests\"\n display_name: \"Read requests\"\n value_type: INT64\n metric_kind: DELTA`\n```\n\nThe following example shows how to configure the `quota` and `limits` fields\nwithin the `quota` section: \n\n```carbon\nmetrics:\n # Define a metric for read requests.\n - name: \"read-requests\"\n display_name: \"Read requests\"\n value_type: INT64\n metric_kind: DELTA\nquota:\n limits:\n # Define the limit or the read-requests metric.\n - name: \"read-limit\"\n metric: \"read-requests\"\n unit: \"1/min/{project}\"\n values:\n STANDARD: 1000\n```\n\nThe following example shows how to configure the `metrics` line after the\n`limits` section: \n\n metrics:\n # Define a metric for read requests.\n - name: \"read-requests\"\n display_name: \"Read requests\"\n value_type: INT64\n metric_kind: DELTA\n quota:\n limits:\n # Define the limit or the read-requests metric.\n - name: \"read-limit\"\n metric: \"read-requests\"\n unit: \"1/min/{project}\"\n values:\n STANDARD: 1000\n metric_rules:\n - metric_costs:\n \"read-requests\": 1\n selector: *\n\nDeploying the `api_config.yaml` file and the ESP\n------------------------------------------------\n\nFor the quota to take effect, you must:\n\n1. Deploy the `api_config.yaml` file to Service Management, which updates the configuration in Endpoints. For detailed steps, see [Deploying the Endpoints configuration](/endpoints/docs/grpc/deploy-endpoints-config).\n2. Deploy the ESP. For detailed steps, see [Deploying the API backend](/endpoints/docs/grpc/deploy-api-backend)."]]