DEV Community

Marant7
Marant7

Posted on

2

Deploy a Java application using Spring Boot on Google Cloud

Google Cloud offers many other features that you can configure, such as databases, storage, monitoring, and more.

Use Google App Engine, which is a serverless platform that makes it easy to deploy and automatically scale applications. Next, I'll show you how to do it.

Step 1: Set up your environment

  1. Install Google Cloud SDK: If you don't have it installed yet, follow the instructions here.

  2. Initialize Google Cloud SDK: Set up your Google Cloud environment by running:

gcloud init

  1. Install the App Engine plugin:

gcloud components install app-engine-java

Step 2: Create a Spring Boot Application

  1. Create a Spring Boot project:

o Project: Maven Project
o Language: Java
o Spring Boot: 2.5.x
o Project Metadata: Enter the name of your group, artifact, and other details.
o Dependencies: Add Spring Web.

Then, build the project and download the ZIP file. Unzip the file on your machine.

  1. Create the Spring Boot application: Open your project in your favorite IDE

  2. Create a simple controller: In the directory

src/main/java/com/example/demo (adjust according to your structure), create a file called HelloController.java with the following content:

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@GetMapping("/")
public String hello() {
    return "Hello, World!";
}
Enter fullscreen mode Exit fullscreen mode

}

  1. Make sure the application runs: Run your application with the mvn spring-boot:run command and verify that you can access http://localhost:8080 and see the "Hello, World!" message

Step 3: Prepare your app for App Engine

  1. Create the app.yaml file: In the root directory of your project, create a file called app.yaml with the following content: runtime: java11 instance_class: F1

This file tells App Engine to use the Java 11 runtime.

  1. Package your application: Build your application to generate the executable JAR file. Use the Java 11 runtime environment.

mvn clean package

Step 4: Deploy the app to Google App Engine

1.Deploy the application:

gcloud app deploy target/demo-0.0.1-SNAPSHOT.jar

Follow the instructions and confirm the deployment when prompted.

2.Abrir la aplicación:

gcloud app browse

Tu aplicación Spring Boot ahora debería estar desplegada en Google App Engine. Puedes actualizar el código y redeployar usando el mismo comando gcloud app deploy.

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay