Einer Cloud Tasks-Warteschlange eine HTTP-Zielaufgabe hinzufügen

In dieser Kurzanleitung erfahren Sie, wie Sie einer Cloud Tasks-Warteschlange mithilfe der Cloud Tasks API eine HTTP-Zielaufgabe hinzufügen.

Hinweise

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.
  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  5. Die Abrechnung für das Google Cloud-Projekt muss aktiviert sein.

  6. Enable the Cloud Tasks API:

    gcloud services enable tasks.googleapis.com
  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  11. Die Abrechnung für das Google Cloud-Projekt muss aktiviert sein.

  12. Enable the Cloud Tasks API:

    gcloud services enable tasks.googleapis.com
  13. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

Cloud Tasks-Warteschlange erstellen

Verwenden Sie den gcloud tasks queues create. zum Erstellen der Warteschlange.

  1. Erstellen Sie in Ihrem Terminal eine Warteschlange, die alle Vorgänge protokolliert.

    gcloud tasks queues create QUEUE_NAME \
        --log-sampling-ratio=1.0 \
        --location=REGION
    

    Ersetzen Sie Folgendes:

    • QUEUE_NAME: ein Name für Ihre Cloud Tasks-Aufgaben Wiedergabeliste
    • REGION: die Region, in der Sie Ihren Dienst oder Ihre App bereitgestellt haben
  2. Warten Sie, bis die Warteschlange initialisiert wurde, und prüfen Sie dann, ob sie erstellt wurde erfolgreich war.

    gcloud tasks queues describe QUEUE_NAME \
        --location=REGION
    

    Die Ausgabe sollte in etwa so aussehen:

     name: projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_NAME
     rateLimits:
       maxBurstSize: 100
       maxConcurrentDispatches: 1000
       maxDispatchesPerSecond: 500.0
     retryConfig:
       maxAttempts: 100
       maxBackoff: 3600s
       maxDoublings: 16
       minBackoff: 0.100s
     state: RUNNING
    

Aufgabe zur Cloud Tasks-Warteschlange hinzufügen

Verwenden Sie den Befehl gcloud tasks create-http-task, um eine Aufgabe zu erstellen, die auf einen HTTP-Endpunkt ausgerichtet ist, und fügen Sie die Aufgabe der Warteschlange hinzu.

  1. Erstellen Sie eine Aufgabe, fügen Sie sie der von Ihnen erstellten Warteschlange hinzu und übergeben Sie diese Aufgabe an einen HTTP-Endpunkt.

    gcloud tasks create-http-task \
        --queue=QUEUE_NAME \
        --url=URL_PATH \
        --method=GET \
        --location=REGION \
        --project=PROJECT_ID
    

    Ersetzen Sie URL_PATH durch den vollständigen URL-Pfad, der für -Anforderung wird gesendet. Beispiel: https://www.google.com

    Der Pfad muss mit http:// oder https:// beginnen.

  2. Prüfen Sie anhand der Protokolle, ob die Aufgabe erfolgreich ausgeführt wurde.

    gcloud logging read --limit=3
    

    Die Logs sollten in etwa so aussehen:

    jsonPayload:
    '@type': type.googleapis.com/google.cloud.tasks.logging.v1.TaskActivityLog
    task: projects/PROJECT_ID/locations/REGION/queues/QUEUE_NAME/tasks/TASK_ID
    taskCreationLog:
       scheduleTime: '2024-07-04T19:00:27.801837Z'
       status: OK
       targetAddress: GET https://www.google.com/
       targetType: HTTP
    

Bereinigen

Löschen Sie das Google Cloud-Projekt mit den Ressourcen, damit Ihrem Google Cloud-Konto die auf dieser Seite verwendeten Ressourcen nicht in Rechnung gestellt werden.

Delete a Google Cloud project:

gcloud projects delete PROJECT_ID

Alternativ können Sie die Cloud Tasks-Warteschlange löschen:

gcloud tasks queues delete QUEUE_NAME \
    --location=REGION

Nächste Schritte