Deploying a Spring Boot Web Application on Azure App Service

Hello everyone, Hope you are doing well. In this tutorial, you will learn to how to deploy the Spring Boot application to Azure App Services using Azure Portal.



A little bit of Background

Azure App Service

Azure App Service is an HTTP-based service for hosting web applications, REST APIs, and mobile backends. You can develop in your favourite language, be it .NET, .NET Core, Java, Ruby, Node.js, PHP, or Python. Applications run and scale with ease on both Windows and Linux-based environments. More Info - https://docs.microsoft.com/en-us/azure/app-service/overview

Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can "just run". More Info - https://spring.io/projects/spring-boot

GitHub Actions

GitHub Actions helps you automate your software development workflows from within GitHub. You can deploy workflows in the same place where you store code and collaborate on pull requests and issues. 
In GitHub Actions, a workflow is an automated process that you set up in your GitHub repository. You can build, test, package, release, or deploy any project on GitHub with a workflow. More Info - https://docs.microsoft.com/en-us/azure/developer/github/github-actions


Steps to Deploy Spring Boot Web Application to Azure App Service


Step 1: 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 and Artifact helloworldHere I selected the Maven project - language Java - Spring Boot 2.7.1 and add Spring web dependency. The single spring-boot-starter-web dependency transitively pulls in all dependencies cognate to web development.



Then, click on the Generate button. When we click on the Generate button, it starts packing the project in a .zip(helloworld.zip) file and downloads the project. 
Then, Extract the Zip file.



Step 2: Import the project on your favourite IDE, I am using Eclipse IDE

Import the project File -> Import -> Maven ->Existing Maven Project -> Next -> Browse -> Select the project -> Finish 

When the project imports prosperously, we can optically discern the project directory in the Package Explorer. The following image shows the project directory:



Add a RestController to be able to test, 

GreetingController.java

@RestController
public class GreetingController {

@GetMapping("greeting")
public String greeting() {

return "Hello, world! Greeting from Azure";
}
}


Then, test the application locally 

Step 1: mvn clean install

Step 2: Run the Spring Boot application - mvn spring-boot:run


The application is running fine locally and I can access it using localhost:8080



Step 3: Create a New Repo and Upload Files on GitHub

First, sign in to Github https://github.com/ 

Then, create a new repository, for example, "springboot-greeting-service".



Then, upload the source code from your local machine to the newly created Github repo.




Step 4: Sign in to Azure Portal and create a resource group

Sign in to Azure portal https://portal.azure.com/#home and find "Resource groups" like below.



Then, create a resource group like the one below.


Here the resource group name is "Knowledgefactory". Create a resource group by clicking the "Review + create " button.



Step 5: Create a Web App and Deploy the code from GitHub Repo

Find "App Services",




Then, create a "Web App" like the one below.


Enter necessary information like Resource group, App name, Runtime stack, Java web server stack, OS, Region etc... like above. Then click on the "Deployment" button  

Enable GitHub Actions to continuously deploy your app and then enter GitHub account information like above. Here the Repository which I selected is "springboot-greeting-service" which we already created on GitHub.

Next click on the "Review + create" button and you will be taken to "Review + create", as shown in the image below.

Next, click on the "Create" button.


Then, You can see "Deployment is in progress" like the below image.


Once deployment is completed you can see "Your deployment is complete".

Next, Click on "Go to resource" button and you will be taken to the App service Overview page, as shown in the image below. 


Copy the public URL which is marked on the above image and use postman to test the service.

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String