Run Spring Pet Clinic with Cloud SQL on App Engine flexible environment
Contributed by Google employees.
This tutorial will walk you through getting the Spring Pet Clinic application up and running on App Engine flexible environment with Cloud SQL.
Spring Boot and Spring Pet Clinic
Spring Boot provides Java developers a quick, annotation driven way to deploy services using minimal code.
Spring Pet Clinic is a popular Spring application that will quickly demonstrate the power of Spring Boot.
App Engine flexible environment for Java
App Engine flexible environment provides the ability to run your Java applications in Docker containers on Google Compute Engine machines, with the ability to have them deployed, monitored and auto-scaled. We will be using the Java 8 runtime environment for this tutorial.
Cloud SQL
Cloud SQL for MySQL is a fully managed MySQL database service on Google Cloud.
Prerequisites
- Create a project in the Google Cloud Platform Console.
- Enable billing for your project.
- Install the Google Cloud SDK.
Prepare
Initialize the Cloud SDK, create an App Engine application, and authorize the Cloud SDK for using Google Cloud APIs in your local environment:
gcloud init gcloud app create gcloud auth application-default login
Clone and test the Pet Clinic application locally:
git clone https://github.com/spring-projects/spring-petclinic.git cd spring-petclinic ./mvnw spring-boot:run
Access http://localhost:8080 via your web browser to see the application homepage. By default, the application uses an in-memory HyperSQL database. You will now switch from HyperSQL to using Cloud SQL as your database.
spring-petclinic provides a copy of the source code with the required changes already completed to run Spring PetClinic using Cloud SQL.
Set up Cloud SQL
Enable the Cloud SQL API.
Create a Second Generation Cloud SQL (MySQL) instance and set the root user password following these instructions.
Create the
petclinic
database.gcloud beta sql databases create petclinic --instance=INSTANCE_NAME
Get the
connectionName
of the instance in the formatproject-id:zone-id:instance-id
by running the following command:gcloud beta sql instances describe INSTANCE_NAME
Using Cloud SQL as your database
Once Cloud SQL is setup and initialized, you can configure your application to use Cloud SQL as the primary database either using Spring Datasource or the snapshot version of Spring Integration for Cloud SQL. The following sections demonstrate both options.
Using Spring Datasource
Update
src/main/resources/application-mysql.properties
, replacing INSTANCE_CONNECTION_NAME with theconnectionName
from the previous step:database=mysql spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://google/petclinic?cloudSqlInstance=INSTANCE_CONNECTION_NAME&socketFactory=com.google.cloud.sql.mysql.SocketFactory spring.datasource.username=root spring.datasource.password=my-smart-password
See updated file here.
Update
pom.xml
to include Cloud SQL MySQL Socket Factory. The socket library allows you to connect to your Cloud SQL instance for local testing and deployment. See referencepom.xml
here.Restart the Spring Boot application using the
mysql
profile:./mvnw -Dspring-boot.run.profiles=mysql spring-boot:run
Using Spring Cloud integration for Cloud SQL
You can also now use the Spring Cloud SQL starter to configure Cloud SQL in your application.
This is currently a SNAPSHOT release, add the dependency from Spring SNAPSHOT repository.
You also need to add the Spring Boot JDBC
to your pom.xml
as shown here.
This dependency enables your application to use the JDBC API to connect to the database and execute SQL queries.
Update
src/main/resources/application-mysql.properties
, replacing INSTANCE_CONNECTION_NAME with theconnectionName
from the previous step:database=mysql spring.cloud.gcp.sql.instanceName=INSTANCE_NAME spring.cloud.gcp.sql.databaseName=petclinic spring.cloud.gcp.sql.password=my-smart-password
See updated file here.
Restart the Spring Boot application using the
mysql
profile:./mvnw -Drun.profiles=mysql spring-boot:run
Testing PetClinic locally
Access the application homepage http://localhost:8080 via your web browser and add some data.
You can verify the data exists in Cloud SQL by running queries agains the
petclinic
database using the Cloud Shell.
Deploying to App Engine flexible environment on Google Cloud
Now that you've tested the application locally, you can deploy the application to the App Engine flexible environment. Once deployed, it will be accessible at https://YOUR_PROJECT_ID.appspot.com.
App Engine flexible environment provides Maven plugins to make your build and deploy process extremely easy. Add
appengine-maven-plugin
to yourpom.xml
'sbuild
plugins section. The samplepom.xml
already contains this plugin configuration.Create an
app.yaml
undersrc/main/appengine
with the following contents. For more on configuringapp.yaml
, refer to this resource:runtime: java env: flex resources: memory_gb: 2.3 handlers: - url: /.* script: this field is required, but ignored
Optionally, you can use the sample
app.yaml
.App Engine flexible environment monitors the health of your application using the
/_ah/health
endpoint. (A200
or404
status is interpreted as the application being healthy.) Because Spring Boot automatically provides a health check endpoint, we can hook that up as our health check endpoint. Update the following fields insrc/main/resources/application.properties
:management.contextPath=/_ah spring.profiles.active=mysql
Here is an updated
application.properties
.Run the following command to deploy your app:
./mvnw -DskipTests=true appengine:deploy
Visit
https://YOUR_PROJECT_ID.appspot.com
to access the Pet Clinic application, now running on Google Cloud. View the application logs using the Cloud Console.
Next steps
- Build your own Spring application.
- Deploy the application to Google Kubernetes Engine.
- Try out other Java samples on Google Cloud.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.