
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 a previous lab, you modified the application to use Cloud SQL for database services. Cloud SQL provides a managed database service for applications that require robust relational database services. But when higher performance and transactions are critical to your application, you can use Spanner to provide high-performance, relational database services. Spanner is an enterprise-grade, globally distributed, strongly consistent database service built for the cloud specifically to combine the benefits of relational database structure with non-relational horizontal scale. This combination delivers high-performance transactions and strong consistency across rows, regions, and continents with enterprise-grade security.
In this lab, you update your application to integrate your Spanner instance with Spring Data. You will then test the changes locally in Cloud Shell, and then redeploy the backend service to App Engine.
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.
Run the command to create an App Engine app.
Open Editor
button on the toolbar of the Cloud Shell window.You can switch between Cloud Shell and the code editor by using Open Editor
and Open Terminal
icon as required.
In this task, you enable Spanner API so that you can create a Spanner database for your application.
In this task, you create a Spanner instance, a database, and a database table.
You create a Spanner instance, and then create a database on that instance for the demo application.
messages
database in the Spanner instance:The output indicates that the database is ready:
You create a table in the messages
database by creating a file that contains a DDL statement and then running the command.
guestbook-service
folder, create the db
folder:In the Cloud Shell code editor, create a file named spanner.ddl
in the ~/guestbook-service/db/
directory.
Add the following command to the spanner.ddl
file:
gcloud
to run the DDL command to create the table:You should see the guestbook_message table only after the spanner.ddl
file was processed successfully.
The database opens showing the schema details tab. You can click the Show Equivalent DDL button, the schema should match the schema you created in the spanner.ddl
file. Click Close.
In this task, you update the Guestbook Service's pom.xml
file to remove the Cloud SQL starter, and add the Spanner starter dependency.
Switch back to the tab running the Cloud Shell code editor.
In the editor, open ~/guestbook-service/pom.xml
.
Delete the Spring Data JPA
by removing these lines:
Cloud SQL Starter
by removing these lines:HSQL
by removing these lines:<dependencies>
section, immediately before the closing </dependencies>
tag:In this task, you add the Spanner instance and database configuration properties to application.properties
and application-cloud.properties
for the guestbook backend service application. You will also delete the Cloud SQL configuration properties.
There is no Spanner emulator, meaning both Dev and Prod will always need a real Spanner instance running. For this lab, we'll use the same Spanner instance.
~/guestbook-service/src/main/resources/application.properties
. Delete the two Cloud SQL Configuration lines and add the following Spanner configuration:~/guestbook-service/src/main/resources/application-cloud.properties
and remove the following Spring properties for Cloud SQL:In this task, you modify GuestbookMessage.java
to use the Spanner annotations.
You can use the @Table
annotation to map a Java class to a Spanner table. And you can use the @Column
annotation to map properties to table columns. You use the @Table
annotation to map to the guestbook_message
table that was created when you ran the DDL statement with gcloud CLI
.
The ID
property is specified as the primary key. In the class constructor, the ID
property is auto-populated with a random UUID. The UUIDv4 format is recommended over a monotonically increasing ID. This format helps Spanner avoid creating hotspots when it automatically shards the data.
The other class properties included match the table's schema in the DDL statement, except for imageUri
, which uses the @Column
annotation to map the table column name image_uri
to the property name imageUri
.
In the Cloud Shell code editor, open ~/guestbook-service/src/main/java/com/example/guestbook/GuestbookMessage.java
.
Replace the entire contents of this file with the following code:
In this task, you update the GuestbookMessageRepository.java
file to use String
as the ID type.
Spring Data Spanner implements many commonly used Spring Data patterns, such as creating simple methods that can be automatically translated to corresponding SQL queries.
One example is a simple method signature: List<GuestbookMessage> findByName(String name);
. The Spring framework enables querying the Spanner table with the SQL query SELECT * FROM guestbook_message WHERE name = ?
.
~/guestbook-service/src/main/java/com/example/guestbook/GuestbookMessageRepository.java
.import
statement after the existing import
directives:PagingAndSortingRepository GuestbookMessage
parameter from Long
to String
:GuestbookMessageRepository
public interface, immediately before the closing brace:The GuestbookMessageRepository.java
file should now look like the screenshot:
In this task, you run the updated guestbook backend service application in Cloud Shell in order to test that the application has been correctly configured to use Spanner for database services.
guestbook-service
directory:curl
to post a message:findByName()
search you added:gcloud
spanner command with a SQL query to validate that messages exist:guestbook_message
table to see the new entry. Click the Spanner Studio tab to see the new entry.In this task, you redeploy the updated guestbook backend service application to App Engine.
guestbook-service
directory:When the deployment completes, the output from Maven provides the URL of the updated backend service application:
A clickable URL link to your new backend service appears:
The response lists all the messages in the Spanner database.
gcloud app browse -s default
. Then click the link to browse to the guestbook frontend application.
You should now see an updated message list that includes the initial test message you sent from Cloud Shell and the new message you just posted confirming that the updated backend service application is using the new Spanner database.
In this lab, you created a Spanner instance and database. You used the data definition language (DDL) to create a Spanner table. You also used Spring to add support for Spanner to an application. Finally, you modified a Java application to use Spanner instead of Cloud SQL.
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