Adicione uma tarefa de destino HTTP a uma fila do Cloud Tasks
Este início rápido mostra como adicionar uma tarefa de destino HTTP a uma fila do Cloud Tasks através da Cloud Tasks API.
Antes de começar
- 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.
-
Install the Google Cloud CLI.
-
Se estiver a usar um fornecedor de identidade (IdP) externo, tem primeiro de iniciar sessão na CLI gcloud com a sua identidade federada.
-
Para inicializar a CLI gcloud, execute o seguinte comando:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith 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_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Tasks API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable cloudtasks.googleapis.com
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/cloudtasks.admin, roles/logging.viewergcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
-
Install the Google Cloud CLI.
-
Se estiver a usar um fornecedor de identidade (IdP) externo, tem primeiro de iniciar sessão na CLI gcloud com a sua identidade federada.
-
Para inicializar a CLI gcloud, execute o seguinte comando:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith 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_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Tasks API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable cloudtasks.googleapis.com
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/cloudtasks.admin, roles/logging.viewergcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
No terminal, crie uma fila que registe todas as operações.
gcloud tasks queues create QUEUE_NAME \ --log-sampling-ratio=1.0 \ --location=REGIONSubstitua o seguinte:
QUEUE_NAME: um nome para a fila do Cloud TasksREGION: a região suportada onde implementou o seu serviço ou app
Aguarde a inicialização da fila e, em seguida, verifique se foi criada com êxito.
gcloud tasks queues describe QUEUE_NAME \ --location=REGIONO resultado deve ser semelhante ao seguinte:
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: RUNNINGCrie uma tarefa, adicione-a à fila que criou e entregue essa tarefa a um ponto final HTTP.
gcloud tasks create-http-task \ --queue=QUEUE_NAME \ --url=URL_PATH \ --method=GET \ --location=REGION \ --project=PROJECT_IDSubstitua
URL_PATHpelo caminho do URL completo para o qual o pedido vai ser enviado. Por exemplo:https://www.google.comO caminho tem de começar por
http://ouhttps://.Verifique se a tarefa foi executada com êxito lendo os registos.
gcloud logging read --limit=3Os registos devem ser semelhantes aos seguintes:
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
Crie uma fila do Cloud Tasks
Use o comando gcloud tasks queues create
para criar a sua fila.
Adicione uma tarefa à fila do Cloud Tasks
Use o comando
gcloud tasks create-http-task
para criar uma tarefa que segmenta um ponto final HTTP e adiciona a tarefa à sua fila.
Limpar
Para evitar incorrer em custos na sua Google Cloud conta pelos recursos usados nesta página, elimine o Google Cloud projeto com os recursos.
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Em alternativa, pode eliminar a fila do Cloud Tasks:
gcloud tasks queues delete QUEUE_NAME \
--location=REGION