Spring Boot applications are a cornerstone for building robust, Java-based microservices. When it comes to deploying these applications in a scalable, serverless, and fully managed environment, Google Cloud Run stands out as an excellent choice. It eliminates the complexities of server provisioning, scaling, and infrastructure management, allowing developers to focus solely on their code.

This comprehensive guide will walk you through the process of deploying your Spring Boot application to Google Cloud Run, from initial setup to final verification.

GitHub Repository for Sample Application:
You can find the source code for a sample application at: https://github.com/lalamanil/IntegratingAccidentPredictionModelOfVideo.git

Prerequisites:

Before you begin, ensure you have the following:

  • A Google Cloud account: With active billing enabled.
  • Google Cloud SDK (gcloud CLI): Installed and configured on your local machine.
  • A Spring Boot application: Ready for deployment (e.g., the sample application from the GitHub link above).

Step 1: Verify Google Cloud CLI Installation and Project Configuration

First, let’s confirm your gcloud installation and set up your project.

  1. Verify gcloud version:
    Ensure the Google Cloud CLI is correctly installed and accessible by checking its version.

  2. Check/Set Active Project:
    Confirm the currently active Google Cloud project. If you need to switch or set a project, use the appropriate gcloud config set project <YOUR_PROJECT_ID> command.

  3. Set Default Compute Region and Zone (Recommended):
    While optional, setting a default region and zone simplifies subsequent commands. For example, you can set us-central1 as your region and us-central1-a as your zone.

Step 2: Enable Required APIs

Before deployment, it’s crucial to ensure that all necessary Google Cloud APIs are enabled in your project.

  1. List Enabled Services:
    Check the services currently enabled in your project.

  2. Enable Missing Services:
    If any of the following APIs are not enabled, enable them using the gcloud services enable command:

    • artifactregistry.googleapis.com
    • cloudbuild.googleapis.com
    • run.googleapis.com
    • compute.googleapis.com

Step 3: Create an Artifact Registry Repository

Google Artifact Registry is the preferred service for storing your Docker images securely and efficiently.

  1. List Existing Repositories:
    Check if you have any existing Artifact Registry repositories.

  2. Create a New Docker Repository:
    Create a new Docker repository in your desired location (e.g., us-central1) with a descriptive name like springboot-docker-repo.

  3. Verify Repository Creation:
    Confirm that your new repository has been successfully created.

Step 4: Create a Multi-Stage Dockerfile for Your Spring Boot Application

In the root directory of your Spring Boot project, create a Dockerfile. A multi-stage Dockerfile is highly recommended for Spring Boot applications as it helps create smaller, more secure production images. This typically involves a build stage (e.g., using Maven or Gradle) and a lighter runtime stage.

Step 5: Build and Push the Docker Image Using Cloud Build

Instead of building Docker images locally, leverage Google Cloud Build to automate the process directly within Google Cloud, pushing the resulting image to Artifact Registry.

  1. Navigate to Project Root:
    Ensure your terminal is in the root directory of your Spring Boot application where the Dockerfile resides.

  2. Check for Existing Images (Optional):
    You can list images in your Artifact Registry repository to confirm if any are already present.

  3. Build and Push the Image:
    Use the gcloud builds submit command. This command will:

    • Initiate a build process in Google Cloud using your Dockerfile.
    • Tag the resulting Docker image with the specified Artifact Registry path and a version (e.g., us-central1-docker.pkg.dev/<YOUR_PROJECT_ID>/springboot-docker-repo/cloudrunspringbootdeploy:v1).
    • Automatically push the tagged image to your designated Artifact Registry repository.
  4. Verify Image in Artifact Registry:
    After the build completes, confirm that the Docker image is successfully created and pushed to your Artifact Registry repository.

Step 6: Deploy to Cloud Run

With your Docker image safely stored, you can now deploy your Spring Boot application to Google Cloud Run.

  1. Deploy the Container:
    Use the gcloud run deploy command. Specify a service name (e.g., springboot-service), the image from your Artifact Registry, the deployment region, and allow unauthenticated access if it’s a public service.

  2. Monitor Deployment:
    Observe the deployment process in your terminal or the Google Cloud Console. Cloud Run will provision the necessary resources and make your application accessible.

Verification

To ensure your Spring Boot application is running correctly on Cloud Run:

  1. Access the Endpoint:
    Cloud Run provides a unique URL for your deployed service. If your Spring Boot application has a health check endpoint (e.g., /healthCheck), you can access this URL in your browser followed by the endpoint path to verify it’s responding.

  2. Check Cloud Run Logs:
    Navigate to the Cloud Run service in the Google Cloud Console and review the logs. This will provide insights into your application’s startup and runtime behavior, confirming successful execution and request handling.

By following these steps, you will have successfully deployed your Spring Boot microservice to Google Cloud Run, enjoying the benefits of a fully managed, scalable, and serverless environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed