Menambahkan tugas target HTTP ke antrean Cloud Tasks
Panduan memulai ini menunjukkan cara menambahkan tugas target HTTP ke antrean Cloud Tasks menggunakan Cloud Tasks API.
Sebelum memulai
- 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.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
gcloud init
-
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.
-
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Tasks API:
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.viewer
gcloud 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.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
gcloud init
-
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.
-
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Tasks API:
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.viewer
gcloud 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.
Di terminal, buat antrean yang mencatat semua operasi.
gcloud tasks queues create QUEUE_NAME \ --log-sampling-ratio=1.0 \ --location=REGION
Ganti kode berikut:
QUEUE_NAME
: nama untuk antrean Cloud Tasks AndaREGION
: region yang didukung tempat Anda men-deploy layanan atau aplikasi
Tunggu hingga antrean diinisialisasi, lalu verifikasi bahwa antrean berhasil dibuat.
gcloud tasks queues describe QUEUE_NAME \ --location=REGION
Outputnya akan mirip dengan berikut ini:
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
Buat tugas, tambahkan ke antrean yang Anda buat, dan kirimkan tugas tersebut ke endpoint HTTP.
gcloud tasks create-http-task \ --queue=QUEUE_NAME \ --url=URL_PATH \ --method=GET \ --location=REGION \ --project=PROJECT_ID
Ganti
URL_PATH
dengan jalur URL lengkap tempat permintaan akan dikirim. Contoh:https://www.google.com
Jalur harus diawali dengan
http://
atauhttps://
.Verifikasi bahwa tugas berhasil dieksekusi dengan membaca log.
gcloud logging read --limit=3
Log akan terlihat seperti berikut:
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
Membuat antrean Cloud Tasks
Gunakan perintah gcloud tasks queues create
untuk membuat antrean.
Menambahkan tugas ke antrean Cloud Tasks
Gunakan perintah
gcloud tasks create-http-task
untuk membuat tugas yang menargetkan endpoint HTTP dan menambahkan tugas ke
antrean Anda.
Pembersihan
Agar tidak menimbulkan biaya pada akun Google Cloud Anda untuk resource yang digunakan di halaman ini, hapus project Google Cloud yang berisi resource tersebut.
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Atau, Anda dapat menghapus antrean Cloud Tasks:
gcloud tasks queues delete QUEUE_NAME \
--location=REGION