
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
In this series of labs, you take a demo microservices Java application built with the Spring framework and modify it to use an external database server. You adopt some of the best practices for tracing, configuration management, and integration with other services using integration patterns.
In this lab, you enhance your application to implement a message handling service with Pub/Sub. Pub/Sub publishes a message to a topic that can then be subscribed and processed by other services.
Pub/Sub is a fully managed, real-time messaging service that enables you to send and receive messages between independent applications. Pub/Sub brings the scalability, flexibility, and reliability of enterprise message-oriented middleware to the cloud. By providing many-to-many, asynchronous messaging that decouples senders and receivers, Pub/Sub enables secure and highly available communication between independently written applications. Pub/Sub delivers low-latency, durable messaging that helps developers quickly integrate systems hosted on the Google Cloud and externally.
In this lab, you learn how to perform the following tasks:
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.
Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Choose an account page.
On the Choose an account page, click Use Another Account. The Sign in page opens.
Paste the username that you copied from the Connection Details panel. Then copy and paste the password.
After a few moments, the Cloud console opens in this tab.
After you complete the initial sign-in steps, the project dashboard appears.
In this task, you clone the repository files that are used throughout this lab.
To begin the lab, click the Activate Cloud Shell button at the top of the Google Cloud Console and, if prompted, click Continue.
To activate the code editor, click the Open Editor
button on the toolbar of the Cloud Shell window.
Click Open in a new Window to open the code editor in a separate tab.
It may take a few minutes for provisioning to complete and the bucket to be created.
Now you're ready to go!
In this task, you create a Pub/Sub topic to which you will send a message.
gcloud
to create a Pub/Sub topic:In this task, you update the guestbook frontend application's pom.xml
file. You add Spring Cloud GCP starter for Pub/Sub in the dependency section.
~/guestbook-frontend/pom.xml
.<dependencies>
section, just before the closing </dependencies>
tag:In this task, you use the PubSubTemplate
bean in Spring Cloud GCP to publish a message to Pub/Sub. This bean is automatically configured and made available by the starter. You add PubSubTemplate
to FrontendController
.
guestbook-frontend/src/main/java/com/example/frontend/FrontendController.java
in the Cloud Shell code editor.import
directives:private
GuestbookMessagesClient client;
and @Value("${greeting:Hello}")
:// Post the message to the backend service
:The code for FrontendController.java
should now look similar to this:
In this task, you run the application in the Cloud Shell to test the new Pub/Sub message handling code.
guestbook-service
directory:The backend service application launches on port 8081.This takes a minute or two to complete. You should wait until you see that the GuestbookApplication is running.
guestbook-frontend
directory:cloud
profile:The frontend application tries to publish a message to the Pub/Sub topic. You will check if the publish was successful in the next task.
Before subscribing to a topic, you must create a subscription. Pub/Sub supports pull subscription and push subscription. With a pull subscription, the client can pull messages from the topic. With a push subscription, Pub/Sub can publish messages to a target webhook endpoint.
A topic can have multiple subscriptions. A subscription can have many subscribers. If you want to distribute different messages to different subscribers, then each subscriber needs to subscribe to its own subscription. If you want to publish the same messages to all the subscribers, then all the subscribers must subscribe to the same subscription.
Pub/Sub messages are delivered "at least once." Thus, you must deal with idempotence and you must deduplicate messages if you cannot process the same message more than once.
In this task, you create a Pub/Sub subscription. You then test it by pulling messages from the subscription, before and after posting a message from the frontend application.
The pull messages
command should report 0 items.
The message you posted earlier does not appear, because the message was published before the subscription was created.
The message appears. The message remains in the subscription until it's acknowledged.
In this task, you use the Spring PubSubTemplate
to listen to subscriptions.
This command generates a new Spring Boot project with the Pub/Sub starter preconfigured. The command also automatically downloads and unpacks the project into the message-processor
directory structure.
~/message-processor/pom.xml
to verify that the starter dependencies were automatically added.~/message-processor/src/main/java/com/example/demo/DemoApplication.java
in the Cloud Shell code editor.import
directives below the existing import
directives:DemoApplication
, just above the existing definition for the main method:We added the Web starter simply because it's much easier to put Spring Boot application into daemon mode, so that it doesn't exit immediately. There are other ways to create a Daemon, for example, using a CountDownLatch, or create a new Thread and set the daemon property to true. But since we are using the Web starter, make sure that the server port is running on a different port to avoid port conflicts.
The code for DemoApplication.java
should now look similar to this:
message-processor/src/main/resources/application.properties
:The new messages should be displayed in the Cloud Shell tab where the message processor is running, as in the following example:
In this lab, you enabled Pub/Sub and created a Pub/Sub topic. You used Spring to add Pub/Sub support to your application. You also modified an application to publish Pub/Sub messages, and created a Pub/Sub subscription. Finally, you modified an application to process messages from a Pub/Sub subscription.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one