
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 use Spring Integration to create a message gateway interface. This interface abstracts from the underlying messaging system rather than using direct integration with Pub/Sub.
Using this approach, you can swap messaging middleware that works with on-premises applications for messaging middleware that works with cloud-based applications. This approach also makes it easy to migrate between messaging middleware.
In this lab, you use Spring Integration to add the message gateway interface and then refactor the code to use this interface rather than implementing direct integration with Pub/Sub.
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 source 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 set up the editor in a new tab with continued access to Cloud Shell.
Open Terminal
and then enter the following command to create an environment variable that contains the project ID for this lab:Now you're ready to go!
In this task, you add the Spring Cloud Integration starter to the frontend application so that you can refactor the code to use a messaging gateway interface instead of using direct integration with Pub/Sub.
Spring Integration core provides a framework for you to add a message gateway interface that can abstract from the underlying messaging system used.
~/guestbook-frontend/pom.xml
.<dependencies>
section, just before the closing </dependencies>
tag:<dependencies>
section, not in the dependencyManagement
section.
In this task, you create an OutboundGateway.java
file in the frontend application. The file contains a single method to send a text message.
OutboundGateway.java
in the ~/guestbook-frontend/src/main/java/com/example/frontend
directory.~/guestbook-frontend/src/main/java/com/example/frontend/OutboundGateway.java
.In this task, you modify the application to publish the message with the FrontendController.post
method. This method enables you to use OutboundGateway
to publish messages.
Whenever someone posts a new guestbook message, OutboundGateway
also sends it to a messaging system. At this point, the application does not know what messaging system is being used.
~/guestbook-frontend/src/main/java/com/example/frontend/FrontendController.java
.pubSubTemplate
with references to outboundGateway
:Replace these lines:
With these lines:
with this line:
FrontendController.java
should now look like the screenshot:
In this task, you configure a service activator to bind messagesOutputChannel
to use Pub/Sub.
In the outbound gateway, you specified messagesOutputChannel
as the default request channel. To define that channel to send the message to the Pub/Sub topic, you must create a new bean for that action in FrontendApplication.java
.
~/guestbook-frontend/src/main/java/com/example/frontend/FrontendApplication.java
.import
directives below the existing import
directives:FrontEndApplication
class definition:FrontendApplication.java
now looks like the following:
In this task, you run the application in Cloud Shell to test the new message gateway interface.
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:In this lab, you added Spring Integration Core to an application. You also created an outbound message gateway in your application and configured an application to publish messages through a gateway. Finally, you bound the output channel of a message gateway to Pub/Sub.
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