Cloud Pub/Sub via Google Cloud SDK

This section explains how to create Cloud Pub/Sub topics and subscriptions, and how to send and receive messages using the gcloud utility in the Google Cloud SDK. To get started, update the Google Cloud SDK by running the following command:

gcloud components update

Run the following command to install Cloud Pub/Sub in the Google Cloud SDK. This command will also install additional components that are in the beta release:

gcloud components install beta

Run the following command to choose the right project to execute the commands against. If you just want to verify the project already configured in Google Cloud SDK, run the gcloud init command:

gcloud init

Run the following command to create a topic for the messages to be sent. This command creates a topic in the selected project, and this can be verified in the Cloud Console:

gcloud beta pubsub topics create exampleTopic

To get help about creating topics via gcloud, run the following command:

 gcloud help beta pubsub topics create

Run the following command to create a subscription for the exampleTopic created previously:

gcloud beta pubsub subscriptions create --topic exampleTopic exampleTopicSubscription

Run the following command to publish a message with data to Cloud Pub/Sub. The -- attribute flag is used to pass the data with the message:

gcloud beta pubsub topics publish exampleTopic --message "Sample Message" --attribute="EmployeeID=20,FirstName=Sarvesh,LastName=Thirukkumaran"

The preceding command should output a message ID, as shown here:

Run the following command to pull the message from Cloud Pub/Sub. The --auto-ack flag will remove the message from the Pub/Sub once it is pulled:

gcloud beta pubsub subscriptions pull --auto-ack exampleTopicSubscription

The output for the preceding command will look like the following image. The message text, message ID, and attributes are shown:

A topic can have more than one subscription. It is better to create one subscription per application for a topic so that it is easy to delete that subscription when the application is retired or hacked.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.181.252