Deploying a Spring Boot application on Google Cloud App Engine
In this section, we will learn how to deploy a Spring Boot application in Google Cloud App Engine.
1. A little bit of Background
GCP App Engine
Spring Boot
2. Creating a simple spring boot web application
First, open the Spring initializr https://start.spring.io/
Then, Provide the Group and Artifact name. We have provided Group name com.knf.dev.demo and Artifact spring-boot-gcp-helloworld. Here I selected the Maven project - language Java 11 - Spring Boot 2.7.9 and add Spring web dependency.
Then, import the project on your favourite IDE.
Final Project directory:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<version>1</version>
<projectId>GCLOUD_CONFIG</projectId>
</configuration>
</plugin>
Complete pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-boot-gcp-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-gcp-helloworld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<version>1</version>
<projectId>GCLOUD_CONFIG</projectId>
</configuration>
</plugin>
</plugins>
</build>
</project>
app.yaml
runtime: java11
instance_class: F1
Create Hello Controller
package com.knf.dev.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String getMessage()
{
return "Hello World!";
}
}
- Spring @RestController annotation is used to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON response.
- @GetMapping annotation for mapping HTTP GET requests onto specific handler methods.
Application.java
package com.knf.dev.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3. Create a New Repository and Upload Files on GitHub
First, sign in to Github https://github.com/
Then, create a new repository "spring-boot-gcp-helloworld".
Then, upload the source code from your local machine to the newly created Github repo.
4. Create a GCP Project
5. Deploy the application to App Engine
git clone https://github.com/knowledgefactory4u/spring-boot-gcp-helloworld.git
When creating an application in App Engine, we need to choose a region where the application will be running. The list of regions can be shown by executing the command:
gcloud app regions list
We will choose region us-west4 and create our AppEngine app:
gcloud app create --region us-west4
gcloud app create --region us-west4
Change the directory to spring-boot-gcp-helloworld.
cd spring-boot-gcp-helloworld
mvn -DskipTests package appengine:deploy