Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

In this tutorial, we'll show how to deploy an application from our Bootstrap a Simple Application using Spring Boot tutorial to App Engine on Google Cloud Platform.

As part of this we'll:

  • Configure Google Cloud Platform Console and SDK
  • Use Cloud SQL to create a MySQL instance
  • Configure the application for Spring Cloud GCP
  • Deploy the application to App Engine and test it

2. Google Cloud Platform Configuration

We can use the GCP Console to get our local environment ready for GCP. We can find the installation process on the official website.

Let's create a project on GCP using the GCP Console:

gcloud init

Next, let's configure the project name:

gcloud config set project baeldung-spring-boot-bootstrap

Then we'll install the App Engine support and create an App Engine instance:

gcloud components install app-engine-java
gcloud app create

Our application will need to connect to a MySQL database within the Cloud SQL environment. As Cloud SQL doesn't provide a free tier we'll have to enable billing on the GCP account.

We can check available tiers easily:

gcloud sql tiers list

Before continuing, we should use the GCP Website to enable the Cloud SQL Admin API.

Now we can create a MySQL instance and database in Cloud SQL using the Cloud Console or the SDK CLI. During this process, we'll choose the region and provide an instance name and database name. It's important that the app and the database instance are in the same region.

Since we're going to deploy the app to europe-west2, let's do the same for the instance:

# create instance
gcloud sql instances create \
  baeldung-spring-boot-bootstrap-db \
    --tier=db-f1-micro \
# create database
gcloud sql databases create \
  baeldung_bootstrap_db \

3. Spring Cloud GCP Dependencies

Our application will need dependencies from the Spring Cloud GCP project for the cloud-native APIs. For this, let's use a Maven profile named cloud-gcp:


Then we add the App Engine Maven plugin:


4. Application Configuration

Now, let's define the configuration that allows the application to use the cloud-native resources like the database.

Spring Cloud GCP uses spring-cloud-bootstrap.properties to determine the application name:


We'll use a Spring Profile named gcp for this deployment and we'll need to configure the database connection. Therefore we create src/main/resources/application-gcp.properties:


5. Deployment

The Google App Engine provides two Java environments:

  • the Standard environment provides Jetty and JDK8 and the Flexible environment provides just JDK8 and
  • the Flexible environment is the best option for Spring Boot applications.

We require the gcp and mysql Spring profiles to be active, so we provide the SPRING_PROFILES_ACTIVE environmental variable to the application by adding it to the deployment configuration in src/main/appengine/app.yaml:

runtime: java
env: flex
  jdk: openjdk8
- url: /.*
  script: this field is required, but ignored
  instances: 1

Now, let's build and deploy the application using the appengine maven plugin:

mvn clean package appengine:deploy -P cloud-gcp

After deployment we can view or tail log files:

# view
gcloud app logs read

# tail
gcloud app logs tail

Now, let's verify that our application is working by adding a book:

http POST https://baeldung-spring-boot-bootstrap.appspot.com/api/books \
        title="The Player of Games" author="Iain M. Banks"

Expecting the following output:

HTTP/1.1 201 
    "author": "Iain M. Banks",
    "id": 1,
    "title": "The Player of Games"

6. Scaling the Application

The default scaling in App Engine is automatic.

It may be better to start with manual scaling until we understand the runtime behavior, and the associated budgets and costs involved. We can assign resources to the application and configure automatic scaling in app.yaml:

# Application Resources
  cpu: 2
  memory_gb: 2
  disk_size_gb: 10
  - name: ramdisk1
    volume_type: tmpfs
    size_gb: 0.5
# Automatic Scaling
  min_num_instances: 1 
  max_num_instances: 4 
  cool_down_period_sec: 180 
    target_utilization: 0.6

7. Conclusion

In this tutorial, we:

  • Configured Google Cloud Platform and the App Engine
  • Created a MySQL instance with Cloud SQL
  • Configured Spring Cloud GCP for using MySQL
  • Deployed our configured Spring Boot application, and
  • Tested and scaled the application

We can always refer to Google's extensive App Engine documentation for further details.

The complete source code of our examples here is, as always, over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Cloud footer banner
Comments are closed on this article!