Google Cloud Pub/Sub - Setup and Tryout
In this article, we will learn to set up the Google Cloud Pub/Sub for an event-driven microservice application to publish and subscribe to the messages.
Join the DZone community and get the full member experience.Join For Free
Before starting this article, please check out my previous article about Google Cloud Pub/Sub and How it works? and learn the real-time use cases of event-driven microservice applications with Google Cloud Pub/Sub.
In the last article, we have seen the Shoe-Mart application architecture, which publishes messages to the topics and those messages are received by Subscribers through subscriptions. This article teaches how to set up the Google Cloud Pub/Sub to publish and subscribe to the messages by following the use case.
In the above picture, The Order Service publishes a message to the topic "Order", The Package and Notification Service Subscribers have received a message through the subscriptions "Order-Packaging" and "Order-Notification". Let's create a Topic and Subscription in the Google Cloud Pub/Sub, then we will learn to publish and subscribe messages from Pub/Sub using a demo application.
Google Cloud Pub/Sub Setup
Google offers Free Trial and Free Tier Cloud Platform access up to $300 credits with 90 days. To try out Google Cloud features, the free tier access would be a good offer for everyone. After creating an account in Google Cloud Platform, Create/Select a project and Click the Pub/Sub link from the left-hand side menu navigation.
After clicking the Pub/Sub, the page will navigate you to the "Topics" page. Here we can create a new topic by clicking the button "Create Topic".
After clicking the "Create Topic" button, a pop-up opens up to create a new topic. On this pop-up create a new topic with default settings (uncheck add a default subscription).
After creating a topic, now it's time to create a subscription. If The topic has no subscriptions and messages published will be lost. Click the "Subscriptions" menu from the left-hand side navigation menu bar under Pub/Sub.
On the "Subscriptions" page, click the "Create Subscription" button to create a new subscription for the topic to receive published messages.
Create a new subscription "order-packaging" with default settings and delivery type is PULL. While creating a new subscription choose the topic "order".
Create another subscription named "order-notification" to receive published messages to the two subscribers.
After successfully created two subscriptions, now it's time to publish a message to the topic "order" and receive messages from the topic "order" through the subscriptions "order-packaging" and "order-notification".
A service account represents a Google Cloud service identity, such as code running on Compute Engine VMs, App Engine apps, or systems running outside Google. In the article, we are going to run our apps to publish and subscribe messages from outside Google. So, to run the demo project create two service accounts with Pub/Sub Publish and Subscribe permission respectively. To create service accounts follow the below steps.
Step 1: Go to the IAM & Admin and Service Accounts menu by clicking the left-hand side navigation menu.
Step 2: Click the "Create Service Account" button on the Service Accounts page.
Step 3: Provide a Service Account Name and click "Create and Continue".
Step 4: Provide the Pub/Sub Publisher role on the Grant this service account access to the project section and click the "Continue" button.
Step 5: Leave the Grant users to access this service account section and click the "Done" button.
Step 6: The new Service Account was created successfully and click that newly created service account on the Service Accounts list page.
Step 7: After clicking the Service Account, Click the Key button to create a new Key.
Step 8: Click the "Create new key" link from the ADD KEY menu button.
Step 9: Choose key type as "JSON" and click the "Create" button to download the Service Account JSON file.
Step 10: Follow the same steps above to create another service account file with the "Pub/Sub Subscriber" role. Store the JSON files in your computer at a secured location.
Try Out With Spring Boot App
The Order Service, Package Service, and Notification Service applications were developed in Spring Boot and available in the GitHub repository. Check out the applications from GitHub and follow the below steps to publish and subscribe messages between the applications by using Google Cloud Pub/Sub Topic and Subscription.
Project Name: pub-sub/set-up-and-try-out
After checkout the project, open the application.yml file from the order service application. Replace the Google Project ID and Publisher Service Account file path on your machine.
spring: application: name: orderservice cloud: gcp: project-id: <GCP-PROJECT-ID> credentials: location: file:<PUB/SUB-PUBLISHER-ROLE-SERVICE-ACCOUNT-KEY-JSON-PATH> server: port: 8081
Open the application.yml file from the package and notification service applications. Replace the Google Project ID and Subscriber Service Account file path on your machine.
spring: application: name: packageservice cloud: gcp: project-id: <GCP-PROJECT-ID> credentials: location: file: <PUB/SUB-SUBSCRIBER-ROLE-SERVICE-ACCOUNT-KEY-JSON-PATH> server: port: 8082
spring: application: name: notificationservice cloud: gcp: project-id: <GCP-PROJECT-ID> credentials: location: file: <PUB/SUB-SUBSCRIBER-ROLE-SERVICE-ACCOUNT-KEY-JSON-PATH> server: port: 8083
Prerequisites are given below to run the Spring Boot applications.
1. Open JDK 1.8
2. Gradle 7
Build all services by executing the below Gradle command.
gradle clean build
After builds are successful, use the below commands to run the applications one by one.
java -jar build\libs\orderservice-1.0.jar java -jar build\libs\packageservice-1.0.jar java -jar build\libs\notificationservice-1.0.jar
Use the below CURL command to publish a message in order service and the message will be received in package service and notification service.
curl --location --request POST 'http://localhost:8081/publish?orderId=WP102925'
The CURL command response is given below
The Order Id WP102925 is published successfully
The output of the packaging service and notification service is as follows.
2021-08-01 22:15:57.887 INFO 9276 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path '' 2021-08-01 22:15:57.934 INFO 9276 --- [ main] c.t.g.p.p.PackageserviceApplication : Started PackageserviceApplication in 13.453 seconds (JVM running for 15.22) Message Arrived! The Order Id is: WP102925
2021-08-01 22:19:23.774 INFO 37808 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8083 (http) with context path '' 2021-08-01 22:19:23.814 INFO 37808 --- [ main] c.t.g.p.n.NotificationserviceApplication : Started NotificationserviceApplication in 13.869 seconds (JVM running for 15.706) Message Arrived! The Order Id is: WP102925
Watch the below video to create a topic and subscriptions in GCP, and service accounts, check out the GitHub project to build and run the services, and publish/subscribe messages from Pub/Sub.
Thanks for reading this article. Happy Architecture! Happy Coding!
Opinions expressed by DZone contributors are their own.