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 Subscription
Java Development Kit
Apache Maven
Azure Cache for Redis – A running instance of Azure Redis Cache is required. Refer previous article Creating and Configuring Azure Redis Cache and use in Spring Boot Application to create Azure Redis Cache and Spring Boot application to store and retrieve data from cache storage
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
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
Step2: click on Create to create a new instance of Azure container registry
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
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
Once the deployment is completed, navigate to Container Registry and click on demolabs container registry instance to view the newly created registry
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
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
Step2: Click on Create to create a new instance of App Service
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
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
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
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
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
Step2: GET request to fetch the total price of XYZ_TV and ABC_Refrigerator products
Step3: DELETE request to delete the XYZ_TV product from cache product
The three endpoints are reachable using the Azure App Service provided endpoint URL.