
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 will take a demo microservices Java application built with the Spring framework and modify it to use an external database server. You will adopt some of the best practices for tracing, configuration management, and integration with other services using integration patterns.
In this lab, you will also reconfigure the demo application to use an external database. The demo application is initially configured to use an embedded HSQL database. You will use Spring Boot to modify the application to use a cloud database.
Rather than building and maintaining your own MySQL instance in the cloud, you can use managed services as much as possible to reduce operation overhead and increase reliability. Google Cloud has a managed MySQL and PostgreSQL service called Cloud SQL.
In this lab, you will create and configure a Cloud SQL instance and reconfigure the application to use Cloud SQL.
Cloud SQL is a fully managed database service that makes it easy to set up, maintain, manage, and administer your relational PostgreSQL and MySQL databases in the cloud. Cloud SQL offers high performance, scalability, and convenience. Hosted on Google Cloud, Cloud SQL provides a database infrastructure for applications running anywhere.
In this lab, you will 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.
Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
In this task, you clone the source repository files that are used throughout this lab.
In this task, you provision a new Cloud SQL instance, create a database on that instance, and then create a database table with a schema that can be used by the demo application to store messages.
You enable Cloud SQL Administration API and verify that Cloud SQL is preprovisioned.
No instances are listed yet.
You create a new Cloud SQL instance.
Provisioning the Cloud SQL instance will take a couple of minutes to complete.
The output should indicate that the Cloud SQL instance creation was done.
You create a database to be used by the demo application.
messages
database in the MySQL instance:By default, Cloud SQL is not accessible through public IP addresses. You can connect to Cloud SQL in the following ways:
gcloud
to connect through a CLI client.You create the database schema to be used by the demo application to store messages.
gcloud
CLI to connect to the database:This command temporarily allowlists the IP address for the connection.
The root password is empty by default.
The prompt changes to mysql>
to indicate that you are now working in the MySQL command-line environment.
gcloud sql connect
command line automatically and temporarily allowlists your incoming connection.
It takes a minute or two for the allowlisting process to complete before the MySQL administration client can connect.
This command lists all of the databases on the Cloud SQL instance, which should include the messages database that you configured in previous steps.
Output:
messages
database:You return to the standard Cloud Shell user prompt.
In this task, you add the Spring Cloud GCP Cloud SQL starter to your project so that you can use Spring to connect to your Cloud SQL database.
From a Java application, you can integrate with a Cloud SQL instance by using the standard method, where you use the JDBC driver. However, configuring the JDBC driver for use with Cloud SQL can be more complicated than connecting to a standard MySQL server because of the additional security that Google Cloud puts in place. Using the Spring Cloud GCP Cloud SQL starter simplifies this task.
The Spring Cloud GCP project provides configurations that you can use to automatically configure your Spring Boot applications to consume Google Cloud services, including Cloud SQL.
You update the guestbook service's pom.xml
file to import the Spring Cloud GCP BOM and also the Spring Cloud GCP Cloud SQL starter. This process involves adding the milestone repository to use the latest Spring release candidates.
guestbook-service/pom.xml
file in the Cloud Shell code editor or in the shell editor of your choice.spring-cloud-gcp-starter-sql-mysql
just before the </dependencies>
closing tag:For local testing, you can continue to use a local database or an embedded database. The demo application is initially configured to use an embedded HSQL database.
To continue to use the demo application for local runs, you disable the Cloud SQL starter in the default application profile by updating the application.properties
file.
guestbook-service/src/main/resources/application.properties
.In this task, you create an application profile that contains the properties that are required by the Spring Boot Cloud SQL starter to connect to your Cloud SQL database.
When deploying the demo application into the cloud, you want to use the production-managed Cloud SQL instance.
You create an application profile called cloud
profile. The cloud
profile leaves the Cloud SQL starter that is defined in the Spring configuration profile enabled. And it includes properties used by the Cloud SQL starter to provide the connection details for your Cloud SQL instance and database.
This command format filters out the connectionName
property from the description of the guestbook Cloud SQL object. The entire string that is returned is the instance's connection name.
The string looks like the following example:
application-cloud.properties
file in the guestbook-service/src/main/resources
directory.guestbook-service/src/main/resources/application-cloud.properties
and add the following properties:YOUR_INSTANCE_CONNECTION_NAME
placeholder with the full connection name string returned in step 1 of this task.If you worked in the Cloud Shell code editor, your screen should look like the following screenshot.
You use the spring.datasource.*
configuration properties to configure the JDBC connection pool, as you do with other Spring Boot applications.
guestbook-service/src/main/resources/application-cloud.properties
that should still be open in the Cloud Shell code editor to specify the connection pool size:You relaunch the backend service for the demo application in Cloud Shell, using the new cloud profile that configures the service to use Cloud SQL instead of the embedded HSQL database.
guestbook-service
directory:The output should look similar to the following.
Output:
curl
:Press Enter at the Enter password prompt.
Query the guestbook_message
table in the messages database:
The guestbook_messages
table now contains a record of the test message that you sent using curl
in a previous step.
The output should look similar to the following.
Output:
In this lab, you have created a Cloud SQL instance, database, and table. You then used Spring to add Cloud SQL support to your application. You also configured an application profile for Cloud SQL, before finally verifying that an application is using 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