Edit on GitHub
Report issue
Page history

Author(s): @{[ username ]}  Published: {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: Run Spring Pet Clinic with Cloud SQL on Google App Engine Flexible Environment description: Learn how to deploy Spring Boot Pet Clinic application to Google App Engine flexible environment and use Cloud SQL. author: jabubake tags: App Engine, Cloud SQL, Spring Boot, Java date_published: 2017-05-12

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 Platform (GCP).


  1. Create a project in the Google Cloud Platform Console.
  2. Enable billing for your project.
  3. Install the Google Cloud SDK.


  1. Initialize the Cloud SDK, create an App Engine application, and authorize the Cloud SDK for using GCP APIs in your local environment:

    gcloud init
    gcloud app create
    gcloud auth application-default login
  2. 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.

  3. Note: 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

  1. Enable the Cloud SQL API.

  2. Create a Second Generation Cloud SQL (MySQL) instance and set the root user password following these instructions.

  3. Create the petclinic database.

    gcloud beta sql databases create petclinic --instance=INSTANCE_NAME
  4. Get the connectionName of the instance in the format project-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

  1. Update src/main/resources/application-mysql.properties, replacing INSTANCE_CONNECTION_NAME with the connectionName from the previous step:


    See updated file here.

  2. 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 reference pom.xml here.

  3. Restart the Spring Boot application using the mysql profile:

    ./mvnw -Drun.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.

Note: 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.

  1. Update src/main/resources/application-mysql.properties, replacing INSTANCE_CONNECTION_NAME with the connectionName from the previous step:


    See updated file here.

  2. Restart the Spring Boot application using the mysql profile:

    ./mvnw -Drun.profiles=mysql spring-boot:run

Testing PetClinic locally

  1. Access the application homepage http://localhost:8080 via your web browser and add some data.

  2. 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 GCP

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.

  1. App Engine flexible environment provides Maven plugins to make your build and deploy process extremely easy. Add appengine-maven-plugin to your pom.xml's build plugins section. The sample pom.xml already contains this plugin configuration.

  2. Create an app.yaml under src/main/appengine with the following contents. For more on configuring app.yaml, refer to this resource:

    runtime: java
    env: flex
      memory_gb: 2.3
      - url: /.*
        script: this field is required, but ignored

    Optionally, you can use the sample app.yaml.

  3. App Engine flexible environment monitors the health of your application using the /_ah/health endpoint. (Note: A 200 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 in src/main/resources/application.properties:


    Here is an updated application.properties.

  4. Run the following command to deploy your app:

    ./mvnw -DskipTests=true appengine:deploy
  5. Visit https://YOUR_PROJECT_ID.appspot.com to access the Pet Clinic application, now running on GCP. View the application logs using the Cloud Platform Console.

Next steps

See more by @{[ username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides


Request a Tutorial

Ask for community help


GCP Tutorials

Tutorials published by GCP


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.