Using Spring Boot with Google App Engine in 4 steps

Nils Weiser
3 min readJun 3, 2020

--

The combination of Google Cloud and Spring Boot allows you to quickly and easily build and host scalable applications without worrying about infrastructure.

App Engine, CloudSQL, Spring Boot

Prerequisites

1. Create a SpringBoot App

Go to https://start.spring.io/ and create a new application (For those who are already familiar with this tool, there is also the possibility to select the dependencies that we will add manually later).

Spring Initializer

Then unpack the zip and go straight to your pom.xml file and add those dependencies and plugins.

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

And add the App Engine maven plugin.

<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.0.0</version>
</plugin>

2. GCP setup

In the following steps we will setup a new GCP project and create a CloudSQL environment and a PostgreSQL instance.

First of all we create a new Project, and then set it in the console.

$ gcloud config set project <PROJECT-ID>

After we have set our project we create an App Engine instance.

$ gcloud components install app-engine-java
$ gcloud app create

Choose your region and wait until the app is created, important is to remember the zone you have chosen, for me its europe-west2.

after that we create a Cloud SQL instance and add a database to it.

Important: The app and the Instance have to be in the same region.

$ gcloud sql instances create codify-sb-gae --tier=db-f1-micro --region=europe-west2 --database-version=POSTGRES_11
$ gcloud sql databases create codify_sb_db --instance=codify-sb-gae

While we wait for the instance and database to be created we can enable the Cloud SQL Admin API.

3. Bootstrap Spring Boot application

Let’s go back to our application and add a few things, first we have to make changes to the application.properties file as following.

spring.cloud.appId=spring-boot-gae-279112

spring.cloud.gcp.sql.instance-connection-name=spring-boot-gae-279112:europe-west2:codify-sb-gae
spring.cloud.gcp.sql.database-name=codify_sb_db
spring.cloud.gcp.logging.enabled=true

Next we add an simple movie entity to our project, first create a new package then add those files, we use Lombok to reduce the huge overflow we would have otherwise.

Movie.java
MovieRepository.java
MovieController.java
MovieService.java

In order for our application to communicate with our database we need to make additional changes to the application.properties file and add those few lines (exchange the password and username with yours).

spring.datasource.username=postgres
spring.datasource.password=

spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true

spring.jpa.hibernate.ddl-auto=create

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

4. Deploy and test

after those changes we are ready to deploy our project and test it.

mvn clean package appengine:deploy...
[INFO] GCLOUD: Deployed service [default] to [https://spring-boot-gae-279112.appspot.com]
[INFO] GCLOUD:
[INFO] GCLOUD: You can stream logs from the command line by running:
[INFO] GCLOUD: $ gcloud app logs tail -s default
[INFO] GCLOUD:
[INFO] GCLOUD: To view your application in the web browser run:
[INFO] GCLOUD: $ gcloud app browse
[INFO] -------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -------------------------------------------------------------
[INFO] Total time: 08:32 min
[INFO] Finished at: 2020-06-03T11:00:50+02:00
[INFO] -------------------------------------------------------------

add some data with curl

curl --header "Content-Type: application/json"  --request POST  --data '{"name":"xyz","genre":"xyz"}'  https://spring-boot-gae-279112.appspot.com/movie/savecurl --header "Content-Type: application/json"  --request POST  --data '{"name":"Resident Evil","genre":"Horror/Action"}'  https://spring-boot-gae-279112.appspot.com/movie/save
Get data from server

Voilà! the data is present at the Server.

You can find the complete source code on my Gitlab account here.

FINISH

What we’ve learned is:

  • Create an simple Spring Boot app with Lombok
  • Create a CloudSQL instance and add an DB to it
  • Create an App Engine
  • Deploy the Spring Boot application and connect it with Cloud SQL

--

--