Deploying Spring Boot Application in Azure App Service

Deploying Spring Boot Application in Azure App Service

This article is a continuation to the previous article Creating and Configuring Azure Redis Cache and use in Spring Boot Application . In the previous article, Azure Redis Cache service is created in Azure portal and a Spring Boot application is created to store/retrieve/delete products information from Azure Redis Cache storage using REST APIs.

In this article, the main focus will be on how to deploy the Spring Boot service to Azure App Service and access the application’s REST APIs using Azure provided public endpoint URL.

Prerequisite

Azure App Service

Azure App Service is a Microsoft provided PaaS (Platform as a Service) which helps developers to host their applications with ease. The applications can be web application or backend services or mobile application which are developed using multiple programming languages and frameworks. Supported programming languages includes ASP.NET, Node.js, PHP, Java, Python, Ruby, Static HTML.

App Service offers the deployed application with scalability, compliance and security using features like load balancing, autoscaling, security and auto management. Application monitoring can also be achieved using Application Insights.

Azure App Service, enables the build and deployment of application with ease by leveraging the DevOps capabilities. The DevOps capabilities such as continuous integration and deployment from Azure DevOps pipelines, GitHub, Container Registries etc.

App Service costs only the amount of compute resource that has been utilized. This compute resources utilization is determined by App Service Plan on which the deployed application runs on.

Usecase

usecase.jpg

Figure.1. Spring Boot application integration with Azure Cache for Redis

Creating Azure Container Registry in Portal

Step1: Search for Container Registry from the Search window in Azure Portal

1.jpg

Step2: click on Create to create a new instance of Azure container registry

2.jpg

Step3: Provide the necessary data such as

  • Subscription

  • Resource Group: Select from existing or create new resource group

  • Registry Name: Name for the new container registry

  • Location: Location in which the registry is desired to be created

  • Availability zones: Availability zone provides resiliency and high availability to a container registry in a specific region

  • SKU: Choose from Standard/Basic/Premium option

3.jpg

The rest of all the sections such as Network, Encryption and Tags are left to default values in this article.

Step4: Click on Review + Create option to validate the deployment. Once the validation is passed, click on Create to initiate the deployment

4.jpg

Once the deployment is completed, navigate to Container Registry and click on demolabs container registry instance to view the newly created registry

5.jpg

Creating Docker Image

Docker image for the Spring Boot service is created and the same will be pushed to Azure Container registry. From ACR, the image be deployed to the Azure App Service.

Step1: Install Docker in the build server to create a docker image for the Spring Boot application. In this article, Docker for Windows is installed.

Step2: Create a Dockerfile for the Spring Boot application

FROM openjdk:8-jre-slim
EXPOSE 8080
COPY target/demo-0.0.1-SNAPSHOT.jar demo-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/demo-0.0.1-SNAPSHOT.jar"]

Step3: Docker build using the below command

docker build -f Dockerfile -t demolabs.azurecr.io/demo-labs:v1 .

Step4: To push the created docker image to the newly created Azure container registry (demolabs.azurecr.io), need to login to the docker server using the login credentials

docker login <login server> --username <username> --password <password>

Details such as login server, username and password can be obtained from Access keys section from Azure container registry

Step5: Push the docker image to Azure container registry using the below command

docker push demolabs.azurecr.io/demo-labs:v1

Once the docker image has been pushed, the same can be validated from portal in Repositories section of Azure Container registry service

6.jpg

Creating Azure App Service and Deploy Spring Boot Application to Azure App Service in Azure Portal

Step1: Search for App Services from the Azure portal search window

7.jpg

Step2: Click on Create to create a new instance of App Service

8.jpg

Step3: In Basics section, provide the necessary details as below

  • Subscription

  • Resource Group: Create new or select existing resource group.

  • Name: Name for the Azure App Service instance.

  • Publish: Method to Publish Code/Docker. In this article, Docker is used.

  • Operating System: Linux/Windows.

  • Region: Region in which the Azure App Service instance will be created.

  • App Service Plan: App Service plan pricing tier determines the location, features, cost and compute resources associated with your app.

  • SKU and Size: In this article, Basic B1 size is chosen for creation

9.jpg

Step4: In Docker section, provide docker related information for the Azure to pull the docker image and deploy to the Azure App Service instance

  • Options: Single Container/Docker Compose

  • Image Source: Select the registry in which Spring Boot application’s docker image is available. Available options are Azure Container Registry/Docker Hub/Private Registry. In this article, Azure Container Registry is selected

  • Registry: Name of the registry

  • Image: Docker image to be deployed

  • Tag: Version of the image to be deployed

10.jpg

Sections such as Monitoring and Tags are left to default values.

Step5: Click on Review + Create option to validate the deployment. Once the validation is passed, click on Create to initiate the deployment

Step6: Once deployment is successful, navigate to the newly created Azure App service instance

13.jpg

To enable a robust deployment of the application, CI/CD pipelines can be configured using Azure DevOps pipelines. This can be configured from Source section under Deployment Center in Azure App service

17.jpg

Test the Deployment

To check the deployed Spring Boot service endpoints, POSTMAN tool is used.

Step1: In POSTMAN, three endpoints for POST, GET, DELETE is tested

POST request to add the XYZ_TV and ABC_Refrigerator to the cache storage

14.jpg

Step2: GET request to fetch the total price of XYZ_TV and ABC_Refrigerator products

15.jpg

Step3: DELETE request to delete the XYZ_TV product from cache product

16.jpg

The three endpoints are reachable using the Azure App Service provided endpoint URL.

References