DEV Community

Marant7
Marant7

Posted on

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)