🚀 Pilot a Spring Boot app on Google Cloud! With FAQs addressed!!!

Abirami Sukumaran
Google Cloud - Community
6 min readMay 25, 2023

--

Spring Boot on Google Cloud: Part 1!

Image with Spring Boot, Spring Cloud and Google Cloud logos

😏 So what is Spring Boot?

Spring Boot is an open source framework that makes it easy to create stand-alone, production-grade Spring-based Java applications. It provides a number of features that make it easy to get started with Spring, including:

  • Autoconfiguration: Spring Boot automatically configures many common Spring features, such as data access, security, and web services. This means that you can start writing code without having to worry about configuring Spring.
  • Starter dependencies: Spring Boot provides a number of starter dependencies that make it easy to add common Spring features to your application. For example, if you want to add support for JPA, you can add the spring-boot-starter-data-jpa dependency to your project.
  • Embedded servers: Spring Boot includes a number of embedded servers, such as Tomcat, Jetty, and Undertow. This means that you can run your application without having to deploy it to a web server.
  • Production-ready features: Spring Boot includes a number of production-ready features, such as metrics, health checks, and externalized configuration. This means that you can focus on developing your application without having to worry about the infrastructure.

In short, Spring Boot is a popular framework to build and ship production-ready, full-stack applications really quickly and is used by a number of large companies, such as Netflix, Airbnb, Spotify etc..

🤨 Spring or Spring Boot?

Majority of the time I speak about Spring Boot, I get asked if the knowledge of Spring and Spring framework is necessary for getting started with Spring Boot.

No, having prior knowledge of Spring is not mandatory for starting with Spring Boot. Spring Boot is built on top of the Spring framework and is designed to simplify the development of Spring applications. It provides a convention-over-configuration approach, which means that many of the common configurations and settings are pre-configured for you, allowing you to quickly start developing applications without diving deep into the underlying Spring framework.

While it is not necessary to have prior knowledge of Spring to get started with Spring Boot, having a basic understanding of the Spring framework can be beneficial. Spring Boot inherits many concepts and features from Spring, so familiarity with concepts such as dependency injection, inversion of control, and the core principles of Spring can help you understand and leverage the capabilities of Spring Boot more effectively.

You can start with Spring Boot and gradually learn the underlying Spring framework concepts as you progress.

🙃 Woah woah woah! That’s a whole lotta jargon!!!

“Dependency Injection? Inversion of what?” Here is an explanation as if to a 2-year-old (why will you explain DI to a 2yo?? let’s say 5 year old):

Let’s say you want to build a car 🚗. You need to buy all the parts, like the engine, the wheels, and the body. You can either go to the store and buy all the parts yourself, or you can have someone else buy the parts for you.

If you buy the parts yourself, you are in control of the process. You can choose which parts you want, and you can make sure that you get the right parts. However, it can be time-consuming and difficult to buy all the parts yourself.

If you have someone else buy the parts for you, you are giving up some control. You are trusting the other person to choose the right parts and to get them to you on time. However, it can be much easier to have someone else do the work for you.

Dependency injection is like having someone else buy the parts for you. It is a way of delegating the responsibility of creating objects to a framework. This can make your code easier to write and to maintain.

Inversion of control is a design principle that is used to achieve dependency injection. It means that the object that needs a dependency does not create it itself. Instead, the dependency is injected into the object by the framework.

🛠️ Or, just know that dependency injection (DI) simplifies the management of dependencies and promotes cleaner, more maintainable code by enabling loosely coupled and highly cohesive software components.

☁️ Enough said, I am Google Cloud READY!!!

With Google Cloud, you don’t need to install, initialize or set up anything. Just open your Chrome browser and go https://console.cloud.google.com/

Prerequisites on Google Cloud

Make sure you have a Google Cloud account with a project created and billing enabled, Cloud Shell activated

Open the Cloud Shell terminal and verify that it is pointing to the correct project and that you have the necessary permissions. If not, you can use the command below to set the correct project.

gcloud config set project <project-id>

Replace <project-id> with the ID of your project.

Bootstrap your first Spring Boot Project

To create a Spring Boot project, run the following command from the Cloud Shell terminal:

curl https://start.spring.io/starter.tgz -d dependencies=cloud-gcp,web,lombok,thymeleaf -d baseDir=first-spring-boot-app -d javaVersion=11 -d bootVersion=2.6.4 -d type=maven-project | tar -xzvf -

dependencies attribute lists the dependencies we want to kickstart the project with (for now just notice cloud-gcp for accessing Google Cloud services in your project and thymeleaf for recognizing your small html code we will include in the end of this blog).
first-spring-boot-app is the name of your project.
bootVersion is the version of Spring Boot, please make sure to update it if required at the time of your implementation.
type is the version of project build tool type, currently set to maven and you can change it as preferred.

The command above creates the Spring Boot project structure in your Cloud Shell machine. You can view the structure and components in your Cloud Shell Editor. Navigate to Cloud Shell terminal in another Google Cloud Console window and click on Open Editor button on the top right side.

You should see the project structure like this:

Spring Boot Project Structure

pom.xml contains all the dependencies for the project (dependencies you configured using this command are already added in your pom.xml.
src/main/java/com/example/demo has the source classes .java files.
resources contain the images, XML, text files and the static content the project uses that are maintained independently.
application.properties enable you to maintain the admin features to define profile specific properties of the application.

Time to build and run your project with maven!

Now, wait ✋… I don’t know Maven!!!

Maven is an automation tool that can be used to build Java and Spring Boot applications. It provides a number of features that can help you to automate the process of building, testing, and deploying your applications. It is a good choice for developers who are new to build automation or who are looking for a tool that is easy to learn.

From Google Cloud Shell, make sure you change directory to point to the newly created project folder “first-spring-boot-app” using the command:

cd first-spring-boot-app

Run the following commands to build your code and run locally

./mvnw package
./mvnw spring-boot:run

If the build is successful, it will start the application and it will be available on port 8080. You can access the application by clicking “Web preview” on your terminal which would open a new web browser tab.

Screenshot of the Cloud Shell Terminal with spring boot app started on 8080 and web preview highlighted

That’s it! You have now successfully gotten started with Maven for Spring Boot.

Create an index.html file in the /src/resources/static folder with the following content and run the build commands again:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Google Cloud and Spring Boot!</title>
</head>
<body>
<div>
<h1>Hello! welcome to Spring Boot on Google Cloud. That's all I am programmed to do at the moment!</h1>
</div>
</body>
</html>

You should see the below result on web preview this time:

Screenshot of web preview

Now that you are on Google Cloud, you might as well learn how to containerize and deploy your app on Google Cloud. Well. Not too soon. Let’s reserve that for our next post, shall we?

For now, take a deep breath and say “AUMMMMMM”! See ya soon in the next episode of Spring Boot on Google Cloud!

--

--

Abirami Sukumaran
Google Cloud - Community

Developer Advocate Google. With 16+ years in data and software dev leadership, I’m passionate about addressing real world opportunities with technology.