Microservices Example
Clone the demo application with the commands below:
cd ~/
git clone https://github.com/GoogleCloudPlatform/training-data-analyst.git
ln -s ~/training-data-analyst/courses/java-microservices/spring-cloud-gcp-guestbook ~/spring-cloud-gcp-guestbook
Run the Backend Locally
cp -a ~/spring-cloud-gcp-guestbook/1-bootstrap/guestbook-service ~/guestbook-service
cd ~/guestbook-service
./mvnw -q spring-boot:run -Dserver.port=8081
Test the application as shown below:
curl http://localhost:8081/guestbookMessages
Add a new record with the command below:
curl -XPOST -H "content-type: application/json" \
-d '{"name": "Ray", "message": "Hello"}' \
http://localhost:8081/guestbookMessages
List all records:
curl http://localhost:8081/guestbookMessages
Run the Frontend
cp -a ~/spring-cloud-gcp-guestbook/1-bootstrap/guestbook-frontend ~/guestbook-frontend
cd ~/guestbook-frontend
./mvnw -q spring-boot:run
Test the Application
In this task, you will test the demo application. The demo application is a simple Java application composed of a microservices backend and a frontend that consumes it. You will extend this simple application in later labs to leverage various Google Cloud services. You will eventually deploy it to the cloud, using both App Engine and Google Kubernetes Engine.
Access the frontend application through the Cloud Shell web preview. To access and use the frontend application through the web preview, perform the following steps:
Click Web Preview. Select Preview on port 8080 to access the application on port 8080.

A new browser tab displays the connection to the frontend application.
You can view the frontend, add messages, and view messages.
Use Cloud Shell to test the backend service
Open another Cloud Shell and run the command below:
curl -s http://localhost:8081/guestbookMessages
To use jq to parse the returned JSON text, execute the following command.
For example, the following command prints out only the messages:
curl -s http://localhost:8081/guestbookMessages \
| jq -r '._embedded.guestbookMessages[] | {name: .name, message: .message}'
Connecting to the Database
Clone the project with the commands below:
git clone https://github.com/GoogleCloudPlatform/training-data-analyst.git
ln -s ~/training-data-analyst/courses/java-microservices/spring-cloud-gcp-guestbook ~/spring-cloud-gcp-guestbook
Copy the relevant folders to your home directory:
cp -a ~/spring-cloud-gcp-guestbook/1-bootstrap/guestbook-service ~/guestbook-service
cp -a ~/spring-cloud-gcp-guestbook/1-bootstrap/guestbook-frontend ~/guestbook-frontend
Create a Cloud SQL instance, database, and table
Now, you will create a Cloud SQL instance of a MySQL database and a table, and then connect your application to that cloud instance.
To get this done, first you have to enable SQL services using the command below:
gcloud services enable sqladmin.googleapis.com
Confirm that it is enabled using the command below:
gcloud services list | grep sqladmin
You still don't have any Cloud SQL instances at this point. To check that, run the command below:
gcloud sql instances list
Now, create a new SQL instance using the command:
gcloud sql instances create guestbook --region=us-central1
This will take a few minutes to provision a MySQL instance.
Now, let us create a database using the command below:
gcloud sql databases create messages --instance guestbook
####Connect to Cloud SQL and Create the Schema
By default, Cloud SQL is not accessible through public IP addresses. You can connect to Cloud SQL in the following ways:
- Use a local Cloud SQL proxy.
- Use
gcloudto connect through a CLI client. - From the Java application, use the MySQL JDBC driver with an SSL socket factory for a secure connection.
You will create the database schema to be used by the demo application to store messages.
Now, let us use the gcloud CLI to connect to the database:
gcloud sql connect guestbook
This will show the prompt below:
Allowlisting your IP for incoming connection for 5 minutes...done. Connecting to database with SQL user [root].Enter password:
The root password is empty by default, so just hit 'Return' on the keyboard when the prompt shows up.
Now you are in the MySQL prompt and can run all the SQL queries.
Run the SQL commands below:
USE messages;
CREATE TABLE guestbook_message (
id BIGINT NOT NULL AUTO_INCREMENT,
name CHAR(128) NOT NULL,
message CHAR(255),
image_uri CHAR(255),
PRIMARY KEY (id)
);
Connecting to Cloud SQL through a Java Application Using Spring
Add the Spring Cloud GCP Cloud SQL starter.
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 will 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.
Here are the steps:
Edit guestbook-service/pom.xml and add this dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
</dependency>
Disable Cloud SQL in the default profile
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.
- In the Cloud Shell code editor, open
guestbook-service/src/main/resources/application.properties. - Add this setting:
spring.cloud.gcp.sql.enabled=false
Configure an application profile to use Cloud SQL
When deploying the demo application into the cloud, you want to use the production-managed Cloud SQL instance.
You will create an application profile called the 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.
- In Cloud Shell, find the instance connection name:
gcloud sql instances describe guestbook --format='value(connectionName)'This command format filters out theconnectionNameproperty from the description of theguestbookCloud SQL object. The entire string that is returned is the instance's connection name. - In the Cloud Shell code editor, create an
application-cloud.propertiesfile in theguestbook-service/src/main/resourcesdirectory. - In the Cloud Shell code editor, open
guestbook-service/src/main/resources/application-cloud.propertiesand add the following properties:
spring.cloud.gcp.sql.enabled=true
spring.cloud.gcp.sql.database-name=messages
spring.cloud.gcp.sql.instance-connection-name=YOUR_INSTANCE_CONNECTION_NAME
- Replace the
YOUR_INSTANCE_CONNECTION_NAMEplaceholder with the full connection name string returned in step 1 in this task.
Configure the connection pool
You use the spring.datasource.* configuration properties to configure the JDBC connection pool, as you do with other Spring Boot applications.
- Add the following property to
guestbook-service/src/main/resources/application-cloud.properties, which should still be open in the Cloud Shell code editor, to specify the connection pool size:
spring.datasource.hikari.maximum-pool-size=5
Test the backend service running on Cloud SQL
You will 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.
- In Cloud Shell, change to the
guestbook-servicedirectory:cd ~/guestbook-service - Run a test with the default profile and make sure there are no failures:
./mvnw test
- Start the Guestbook Service with the
cloudprofile:
./mvnw spring-boot:run \ -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=cloud"
- During the application startup, validate that you see Cloud SQL-related connection logs.
Testing steps
In a new Cloud Shell tab, make a few calls using curl:
curl -XPOST -H "content-type: application/json" \
-d '{"name": "Ray", "message": "Hello CloudSQL"}' \
http://localhost:8081/guestbookMessages
List messages using curl http://localhost:8081/guestbookMessages.
Using a client, connect to Cloud SQL: gcloud sql connect guestbook
Query using statements like:
USE messages;
SELECT * FROM guestbook_message;