Microsoft Azure For Java Developers D.
Microsoft Azure For Java Developers D.
Abhishek Mishra
Microsoft Azure for Java Developers: Deploying Java Applications through Azure
WebApp, Azure Kubernetes Service, Azure Functions, and Azure Spring Cloud
Abhishek Mishra
Navi Mumbai, India
Acknowledgments���������������������������������������������������������������������������������������������������xv
Introduction�����������������������������������������������������������������������������������������������������������xvii
v
Table of Contents
Part II: Integrating Java Applications with Popular Azure Services����������� 113
Chapter 6: Integrating with an Azure Storage Account���������������������������������������� 115
Structure����������������������������������������������������������������������������������������������������������������������������������� 115
Objectives��������������������������������������������������������������������������������������������������������������������������������� 116
Introduction to Azure Storage��������������������������������������������������������������������������������������������������� 116
Create an Azure Storage Account��������������������������������������������������������������������������������������������� 118
Working with Azure Storage Blob��������������������������������������������������������������������������������������������� 121
Working with Azure Storage Queue������������������������������������������������������������������������������������������ 127
Working with Table Storage������������������������������������������������������������������������������������������������������ 130
Summary���������������������������������������������������������������������������������������������������������������������������������� 136
vii
Table of Contents
viii
Table of Contents
Chapter 12: Authentication and Authorization Using Azure Active Directory������ 253
Structure����������������������������������������������������������������������������������������������������������������������������������� 253
Objectives��������������������������������������������������������������������������������������������������������������������������������� 254
Introduction to Azure Active Directory�������������������������������������������������������������������������������������� 254
Configure Azure Active Directory for Authentication and Authorization������������������������������������ 255
Create a User in Azure Active Directory������������������������������������������������������������������������������� 255
Register an Application in Azure Active Directory��������������������������������������������������������������� 258
Assign the User to the Application Role������������������������������������������������������������������������������� 267
Configure a Spring Boot Application����������������������������������������������������������������������������������������� 271
Authentication Scenarios for Azure AD������������������������������������������������������������������������������������� 276
Web Browser to Web Application����������������������������������������������������������������������������������������� 276
Single Page Application������������������������������������������������������������������������������������������������������� 277
Native Application to Web API���������������������������������������������������������������������������������������������� 277
Web Application to Web API������������������������������������������������������������������������������������������������� 277
Server Application to Web API��������������������������������������������������������������������������������������������� 278
Summary���������������������������������������������������������������������������������������������������������������������������������� 278
ix
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 351
x
About the Author
Abhishek Mishra is a Principal Cloud Architect at a leading
organization and has more than 17 years of experience in
building and architecting software solutions for large and
complex enterprises across the globe. He has deep expertise
in enabling digital transformation for his customers
using the cloud and artificial intelligence. He speaks at
conferences on Azure and has authored four books on Azure
prior to writing this new book.
xi
About the Technical Reviewer
Haixia Cheng is a software engineer for Java on Azure
at Microsoft. She has three years of experience building
products and supporting Java enterprise customers
in embracing the cloud paradigm. Haixia has helped
companies migrate (modernize) largely monolithic Java EE
applications to Virtual Machines and Kubernetes on Azure.
xiii
Acknowledgments
I would like to thank Apress for giving me the opportunity to work on this book. Also
thanks to the technical reviewer and the editor and the entire Apress team for supporting
me in this journey.
xv
Introduction
This book helps you build and run Java-based applications on Azure. You will get an
understanding of available services on Azure that support Java-based applications
and learn with practical demonstrations how to create these services and run the Java
applications on these services. The book shows how to deploy your Java applications in
Azure WebApp, Azure Kubernetes Service, Azure Functions, and Azure Spring Cloud.
Also covered is integration with components such as Graph API, Azure Storage, Azure
Redis Cache, and Azure SQL.
This book is for Java developers planning to build Azure-based Java applications
and deploy them on Azure. Developers should be aware of the preliminary cloud
fundamentals to help them understand the Java capability available on Azure. The
developer need not be an expert in Azure to grasp the book's content and start building
Java-based applications using the capability available on Azure. However, developers
should have a good understanding of the Java programming language and frameworks.
The book starts with a brief discussion of cloud computing and an introduction to
Java support on Azure. You’ll then learn how to deploy Java applications using each of
the deployment models, and you’ll see examples of integrating with Azure services that
are of particular interest to Java programmers. Security is an important aspect, and this
book shows how to enable authentication and authorization for your Java applications
using Azure Active Directory.
Implementing a DevOps strategy is essential in today’s market when building any
application. Examples in this book show how to build continuous integration and
continuous deployment pipelines to build and deploy Java applications on Azure. The
book focuses on the best practices one should follow while designing and implementing
Java applications on Azure. The book also elaborates on monitoring and debugging Java
applications running on Azure using Application Insights and Azure Monitor.
xvii
PART I
Structure
In this chapter, we will discuss the following aspects of Azure for Java developers:
• Introduction to Azure
Objectives
After studying this chapter, you should be able to get the following learnings:
3
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_1
Chapter 1 Getting Started with Java Development for Azure
4
Chapter 1 Getting Started with Java Development for Azure
underlying hosting infrastructure as the cloud vendor will manage it. You need to deploy
your application in the cloud service provided by the cloud vendor. This approach will
save you both capital and operational expenditure. You need not purchase any server
to host your application and need not worry about keeping the server up and running.
You need to rent the hosting environment on demand from the cloud vendor and
host your application. The cloud vendor will maintain the infrastructure, and you can
decommission the infrastructure whenever you do not need it. And not just the hosting
environment, you can rent other services like databases, storage, network, and many
more such services as per your application requirements.
Note Cloud computing is all about renting cloud hosting services from a cloud
vendor like Microsoft Azure, Amazon Web Services, or Google Cloud Platform.
5
Chapter 1 Getting Started with Java Development for Azure
Note In the case of Infrastructure as a Service, you get greater control over
the underlying hosting environment. However, this cloud hosting approach is
expensive.
Note In the case of Platform as a Service, you have no control or minimal control
over the underlying hosting environment. However, this cloud hosting approach is
cheaper than the Infrastructure-as-a-Service approach.
6
Chapter 1 Getting Started with Java Development for Azure
Note In Software as a Service, you have no control over the cloud infrastructure,
hosting environment, and application code. You configure the data and access for
the application.
7
Chapter 1 Getting Started with Java Development for Azure
• You can provision and de-provision the cloud services with a single
click and in minutes. You are not locked to the infrastructure if you do
not need it anymore.
8
Chapter 1 Getting Started with Java Development for Azure
• You can manage the customer latency issues for your application.
You can host a copy of your application in a data center near your
customers. Cloud-based load balancers are available that operate at
a global level and can load-balance your applications hosted across
multiple regions. These load balancers would route the customer
request to a data center near the customer.
9
Chapter 1 Getting Started with Java Development for Azure
Introduction to Azure
Azure is a cloud offering from Microsoft. It was first announced at Microsoft’s
Professional Developers Conference in 2008. Microsoft made its cloud platform
generally available in February 2010 and launched it as Windows Azure, and in the
year 2014, renamed it Microsoft Azure. Today, Microsoft Azure has a wide range of 200
plus cloud-based services that would help you build and host cutting-edge solutions
on Azure.
Your application running on Azure can be a simple customer-facing website, a
robust data platform, a complex Machine Learning–based intelligent application, or any
other new-generation solution. Azure supports a wide range of software languages and
frameworks like .NET Core, Java, Python, and many more. Microsoft Azure is a popular
cloud choice for leading private organizations and governments across the globe. You
can build highly available, reliable, fault-tolerant, and secured solutions on Azure with
ease. Your solution on Azure can be based on the Infrastructure-as-a-Service model or
Platform-as-a-Service model, or Software-as-a-Service model.
Azure provides a global infrastructure comprising more than 200 physical data
centers spread across the globe. These data centers are linked using a highly available
and secured interconnected network owned by Microsoft. These data centers are
grouped into zones, and a geographic region consists of a group of zones. You can host
your Azure solution across data centers in a zone, making the solution locally redundant;
or data centers across multiple zones, making the solution zone redundant; or data
centers across multiple regions, making the solution geo-redundant.
Azure data centers are spread across the globe, and your solution can be hosted
anywhere near your customer base. You can build excellent data backup and recovery
strategies for your workloads running on Azure as you can back up your data and
solution across data centers in multiple zones or regions. Even if the primary zone or
region running your application goes down, your solution running on Azure can be
restored and made available in no time from a secondary zone or region where you have
a backup for your solution.
Azure provides a wide range of services spread across multiple categories like
Compute, Data, Storage, Networking, and many more. Table 1-1 depicts some of the
Azure services that are used frequently by cloud practitioners.
10
Chapter 1 Getting Started with Java Development for Azure
You can provision, delete, or interact with your Azure resources using Azure portal
that provides a graphically intuitive user experience or command-line utilities like Azure
CLI or Azure PowerShell or REST APIs exposed by these services. You can also build
Infrastructure-as-Code solutions using Terraform, Azure ARM templates, Chef, or other
popular Infrastructure-as-Code offerings.
Azure resources or services that you create are arranged inside a resource group.
A resource group is a logical container for all your resources, and resource groups are
grouped inside subscriptions. Subscriptions define the pricing model for the Azure
resources you are using. You need to purchase an Azure subscription so that you can
start creating your resources. Subscriptions are further grouped into management
groups. You can define management and governance strategies for your resources at
either the management group level, subscription level, or resource group level. You can
have multiple management groups inside a management group. However, you cannot
have nested subscriptions or resource groups. Figure 1-2 demonstrates how we can
group resources inside the resource groups and subscriptions.
11
Chapter 1 Getting Started with Java Development for Azure
12
Chapter 1 Getting Started with Java Development for Azure
in Azure Kubernetes Service. You may also choose to run your application on Azure
Spring Cloud. Azure Spring Cloud is a Spring Cloud offering on Azure. You can build
robust serverless applications using Java-based Azure Functions, Azure Logic Apps, and
other serverless offerings on Azure. In a nutshell, you have a wide range of possibilities
available on Azure for your Java applications.
You can integrate your Java applications with other popular Azure services like Azure
Active Directory, Azure Redis Cache, Azure SQL, Azure Cosmos DB, Azure Cognitive
Services, Azure Logic Apps, and many more. These services would help you build a
cloud-native solution without needing any on-premises components for caching,
databases, content delivery, authentication, authorization, or other concerns for your
application.
All popular Java editors like Eclipse, IntelliJ, and Visual Studio Code support
deploying to Azure seamlessly. You can also package and deploy your Java application
to Azure using Maven or Gradle. You can leverage popular DevOps tools like Azure
DevOps or Jenkins to build Continuous Integration and Continuous Deployment of your
application on Azure.
You can monitor your Java application running on Azure using Azure Monitor and
Application Insights. You can ingest your application logs to Log Analytics Workspace
and query the logs when you need them.
You can integrate your Java application with Azure Active Directory and enable
authentication and authorization using OAuth for your Java application. Azure Active
Directory is a widely accepted identity management solution on Azure. You can build
robust enterprise-grade identity management strategies and enable popular identity
features like single sign-on or multifactor authentication for your Java application using
Azure Active Directory.
Summary
In this chapter, we learned the basics of cloud computing. We explored the different
cloud hosting types like Infrastructure as a Service, Platform as a Service, and Software as
13
Chapter 1 Getting Started with Java Development for Azure
a Service. We then explored Microsoft Azure and the Java support provided by Microsoft
Azure. In the next chapter, we will learn the basics of Azure WebApp and then build a
Spring Boot Java application, package it with Maven, and deploy it to Azure WebApp
using Maven-based Azure Plugins.
The following are the key takeaways from this chapter:
• You can host your application on the cloud either using the
Infrastructure-as-a-Service (IaaS) model or Platform-as-a-Service
(PaaS) model, or Software-as-a-Service (SaaS) model.
14
CHAPTER 2
Structure
In this chapter, we will discuss the following aspects of Java for Azure WebApp:
• Azure WebApp
• App Service Plan
Objectives
After studying this chapter, you should be able to get the following learnings:
15
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_2
Chapter 2 Java for Azure WebApp
Azure WebApp
Azure WebApp is a Platform-as-a-Service offering on Azure. You can build HTTP-based
applications and services and host them on the Azure WebApp. You need not worry
about creating and managing the underlying hosting infrastructure, and the Azure
platform will take care of this for you. You need to provision the service and host your
application. You can build web applications, REST APIs, and mobile back end using
any supported languages like .NET, Java, Node.js, Python, PHP, and Ruby and run it on
Azure WebApp. Both the Windows- and Linux-based environments are supported on
Azure WebApp. You can also containerize your application using Docker and run it on
Azure WebApp.
Azure WebApp is highly available and scalable. You can either configure your
WebApp to scale automatically or scale the WebApp manually. It has a rich continuous
integration and continuous deployment support and integrates easily with Azure
DevOps, Jenkins, Bitbucket, GitHub, and even any local Git repository. It also supports
deploying an application using FTP.
You can have multiple deployment slots for the Azure WebApp. Your application
would run on a production deployment slot, and you can create a staging deployment
slot for an Azure WebApp and host a newly developed version of your application.
The majority of the application traffic can be diverted to the production slot, and part
of the application traffic can be diverted to the staging slot. Eventually, you can swap
the content in the staging slot to the production slot and redirect all the traffic to the
production slot once you have enough confidence in the newly developed application.
You can automate this process using Infrastructure-as-Code and DevOps solutions.
Note You can scale the Azure WebApp horizontally by provisional additional
identical instances for the WebApp or scale it vertically by changing the App
Service Plan. Scaling vertically is referred to as the scale-up process, and scaling
horizontally is referred to as the scale-out process.
16
Chapter 2 Java for Azure WebApp
• Shared Compute
• Dedicated Compute
• Isolated
Shared Compute
Shared Compute offers Free or Shared plans, and the underlying hosting infrastructure
would get shared by the WebApp running on App Service Plans for multiple customers.
This tier does not offer scaling features, and compute isolation is guaranteed for your
application. You can use this tier for development and testing purposes only.
Dedicated Compute
With Dedicated Compute, you have a dedicated underlying hosting infrastructure for all
your WebApps running on your App Service Plan. It can be basic, premium, or standard
tier. The premium tier offers the maximum number of computing resources and scaling
instances followed by the standard and basic tiers. Compute isolation is guaranteed for
your application.
17
Chapter 2 Java for Azure WebApp
Isolated
The Isolated plan provides a dedicated Virtual Network for your Azure. This tier
guarantees network isolation along with compute isolation.
https://start.spring.io/
Select the Project as Maven Project, Language as Java, and Spring Boot version. The
Spring Boot version keeps getting updated very frequently, and you can use the version
based on your preferences or the latest one available. Provide the Project Metadata as in
Figure 2-1. Select Packaging as JAR and Java version as 11.
18
Chapter 2 Java for Azure WebApp
We need to add the Spring Web dependency to our application. Click on the ADD
DEPENDENCIES button shown in Figure 2-2. We will create a REST API in our sample
application, and the Spring Web dependency will help us create it.
19
Chapter 2 Java for Azure WebApp
20
Chapter 2 Java for Azure WebApp
Click on the GENERATE button as in Figure 2-4. This action will generate a sample
Java Spring Boot application that will get downloaded locally.
Open the application in Visual Studio Code. Go to the AppApplication.java file. You
will find it inside the java\com\sample\app folder as in Figure 2-5.
21
Chapter 2 Java for Azure WebApp
Replace the code in the AppApplication.java file with the code in Listing 2-2. We are
annotating the AppApplication class as REST Controller and adding a REST GET API
named hello. When we invoke this REST API, we will get the Hello World string as the
response.
package com.sample.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class AppApplication {
22
Chapter 2 Java for Azure WebApp
@GetMapping("/hello")
public String hello() {
return String.format("Hello World !!");
}
Now let us open a new terminal window and build the application using Maven
(Figure 2-6).
Let us execute the following command to build the application as in Listing 2-3.
Make sure you navigate to the directory in the terminal where the pom.xml file is present.
23
Chapter 2 Java for Azure WebApp
mvn install
Once the build is successful, a directory named target with the runnable JAR file
gets created. Navigate to the target directory and execute the following command in the
target directory as in Listing 2-4.
Once the application boots up successfully, browse the following URL in the browser
as in Listing 2-5.
http://localhost:8080/hello
You can see the response from the hello REST API as in Figure 2-7.
We can deploy this application to Azure. Now let us provision an Azure WebApp on
which we can deploy this application.
24
Chapter 2 Java for Azure WebApp
https://portal.azure.com
Click Create a resource as in Figure 2-8. This action will navigate you to Azure
Marketplace, where you can choose a service to create.
Select Web App as in Figure 2-9. If you do not see the WebApp in the list of the
popular products, search for it using the search bar.
25
Chapter 2 Java for Azure WebApp
Provide the subscription, resource group, and name of the WebApp as in Figure 2-10.
26
Chapter 2 Java for Azure WebApp
Scroll down and provide the code publishing details, region, and the App Service
Plan as in Figure 2-11. Click Review + create. This action will navigate you to the
validation page.
27
Chapter 2 Java for Azure WebApp
Click Create as in Figure 2-12. This action will spin up your Azure WebApp.
28
Chapter 2 Java for Azure WebApp
29
Chapter 2 Java for Azure WebApp
mvn com.microsoft.azure:azure-webapp-maven-plugin:2.2.1:config
You will get prompted to enter the choice to select the WebApp we created earlier.
You can choose to create a new WebApp as well as in Figure 2-13.
30
Chapter 2 Java for Azure WebApp
Once this command completes successfully, the pom.xml file will get modified, and
the WebApp details will get added to it as in Listing 2-8. Use the latest package and plug-
in versions available when you are performing this demo.
31
Chapter 2 Java for Azure WebApp
<groupId>com.sample</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app</name>
<description>Demo project for Spring Boot on Azure WebApp</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-webapp-maven-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<schemaVersion>v2</schemaVersion>
<subscriptionId>a3759752-01d3-4b0e-ad35-71bd2faa2980</
subscriptionId>
<resourceGroup>rg-demowebapp</resourceGroup>
<appName>mydemowebapp28</appName>
32
Chapter 2 Java for Azure WebApp
<pricingTier>S1</pricingTier>
<region>eastus</region>
<appServicePlanName>ASP-rgdemowebapp-8802</appServicePlanName>
<appServicePlanResourceGroup>rg-demowebapp</
appServicePlanResourceGroup>
<runtime>
<os>Windows</os>
<javaVersion>Java 11</javaVersion>
<webContainer>Java SE</webContainer>
</runtime>
<deployment>
<resources>
<resource>
<directory>${project.basedir}/target</directory>
<includes>
<include>*.jar</include>
</includes>
</resource>
</resources>
</deployment>
</configuration>
</plugin>
</plugins>
</build>
</project>
Now run the following command to deploy your application as in Listing 2-9.
Once the application gets deployed successfully, you can browse the WebApp URL as
in Figure 2-15 and see the output. You can go to the Azure portal, open the App Service
Overview section. You will get the URL to navigate in that section.
33
Chapter 2 Java for Azure WebApp
34
Chapter 2 Java for Azure WebApp
Then you can switch to another plan and click Apply as in Figure 2-17.
You can click See additional options to explore more plans as in Figure 2-18.
35
Chapter 2 Java for Azure WebApp
To scale your WebApp horizontally, you need to go to the WebApp in the Azure
portal and click Scale-out (App Service plan) as in Figure 2-19. You can scale either
manually by specifying the number of instances you need for your application or
automatically based on custom metrics and rules like CPU utilization, memory
utilization, and other compute resources performance needs.
36
Chapter 2 Java for Azure WebApp
Summary
In this chapter, we learned the details of Azure WebApp and then explored what App
Service Plan is and other features of Azure WebApp. We provisioned an Azure WebApp
and then deployed a Java Spring Boot application to Azure WebApp using Maven plug-
in. We also learned about scaling Azure WebApp at a high level. In the next chapter,
we will learn the basics of Azure Functions, and then we will build a Java-based Azure
Function.
The following are the key takeaways from this chapter:
• You need not worry about creating and managing the underlying
hosting infrastructure, and the Azure platform will take care of this
for you.
37
Chapter 2 Java for Azure WebApp
• Shared Compute
• Dedicated Compute
• Isolated
• You can use the Maven plug-in to deploy a Java Spring Boot
application to Azure WebApp.
38
CHAPTER 3
Java-Based Azure
Functions
Azure Functions are serverless offerings on Azure. They are best suited to run code that
can run for a short duration and perform business logic. Azure Functions can be used as
a background worker for your application and perform a background job. They can also
perform simple to highly complex business logic and be consumed like an API. Azure
Functions are powerful and can deploy modern applications based on complex
architecture like microservices. Azure Functions can be used to host the services in the
microservices architecture.
In the previous chapter, we learned the basics of Azure WebApp. We then developed
a Java Spring Boot application and deployed it on Azure WebApp using the Maven plug-
in. In this chapter, we will learn the details of Azure Functions, and then we will build a
Java-based Azure Function.
Structure
In this chapter, we will discuss the following aspects of Java for Azure WebApp:
• Hosting plans
39
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_3
Chapter 3 Java-Based Azure Functions
Objectives
After studying this chapter, you should be able to get the following learnings:
• Azure Functions
• Durable Functions
40
Chapter 3 Java-Based Azure Functions
when the Azure Functions execute, and you are not charged when they are idle. The
underlying infrastructure on which the Azure Function runs and the scaling aspects are
managed by the underlying Azure platform. Azure Functions support running code that
executes for a short time interval. However, you can choose an appropriate hosting plan
and run your application for a longer duration. Table 3-1 demonstrates the programing
language support based on the Function runtime you choose. At present, while
authoring this book, there are four runtime versions supported.
C# .NET 4.8 .NET Core 2.1 .NET Core 3.1, .NET 5 .NET 6
JavaScript Node.js 6 Node.js 10, 8 Node.js 14, 12, 10 Node.js 14, 16
F# .NET 4.8 .NET Core 2.1 .NET Core 3.1 .NET 6
Java No Support Java 8 Java 11, 8 Java 11, 8
PowerShell No Support PowerShell Core 6 PowerShell 7, Core 6 PowerShell 7
Python No Support Python 3.7, 3.6 Python 3.9, 3.8, 3.7, 3.6 Python 3.9, 3.8
Typescript No Support Supported Supported Supported
Azure Functions are based on top of Azure WebJobs. However, Azure WebJobs run
as a background worker and always shares the App Service Plan for the Azure WebApp.
Azure Functions have a hosting plan of their own. You can also host multiple Azure
Functions on an App Service Plan.
Azure Functions execute whenever they get triggered by the supported Triggers.
They complete the execution and get into an idle state. Azure Functions again wake up
and spring into action only when triggered.
41
Chapter 3 Java-Based Azure Functions
• You can build an n-tier application using Azure Functions. You can
break the business and data access logic into smaller chunks and
host each of these chunks in an Azure Function.
• You can use Azure Functions and Azure Event Grid in event-driven
scenarios where these functions can get triggered and perform a task.
Hosting Plans
Hosting plans decide the underlying infrastructure on which Azure Functions run. They
decide the scaling needs, execution interval, virtual network integration, and many other
important aspects for your Azure Functions. You can choose your hosting plan for the
Azure Function based on your computing requirements like memory usage, CPU usage,
and many more. Also, you need to consider the scaling needs, execution time-out, and
virtual network integration as other important aspects while deciding the hosting plan
you may need. The following are the hosting plans supported by Azure Functions.
42
Chapter 3 Java-Based Azure Functions
Consumption Plan
Consumption Plan is a pure serverless plan. You do not have any control over the hosting
and scaling infrastructure. Based on the incoming traffic, it adds additional instances in
the runtime, and when the load decreases, it shuts down the extra instances. This plan
is cost-effective as you get billed only when the Function is executing, and you do not
incur any bill when the Function is idle and is not performing any task. The Functions
can execute for five minutes by default, and you can set them to execute for ten minutes
at maximum.
The Function gets into sleep or an idle state when it is not executing. Whenever it
gets triggered again, it springs into action and starts executing. However, the Function
does not start executing as soon as triggered. There is a delay as the Function needs to
wake up from the idle state and get ready to serve the request. This delay is called as cold
start phenomenon that we get while using this plan.
Premium Plan
Premium Plan makes sure that an instance of an Azure Function is always warmed
up and ready to serve the request. This will avoid the cold start phenomenon. Like
Consumption Plan, you do not have any control over how the Function scales. However,
you have greater control over the infrastructure on which the Azure Function is running.
You can choose a SKU for the Plan like EP1, EP2, or EP3 and choose the computing
needs for the Function. You can configure the Azure Function to run in a Virtual
Network. Using the Premium Plan, Azure Functions can execute for 30 minutes by
default, and you can configure it to never time out.
Dedicated Plan
The Dedicated Plan is the same as the WebApp App Service Plan. Azure Functions can
run continuously and never get into an idle state when using this plan. This plan would
make your Function run just like a WebApp and is not a serverless plan. You can choose
an App Service Plan SKU based on your CPU usage, memory, and other computing
requirements, and you can either configure auto-scale or scale manually and have full
control over how the Function scales.
43
Chapter 3 Java-Based Azure Functions
Kubernetes Plan
This plan runs Azure Functions inside Azure Kubernetes Service and helps run Azure
Functions continuously inside the Kubernetes cluster.
44
Chapter 3 Java-Based Azure Functions
Figure 3-1. Triggers and Bindings in Azure FunctionsThe following are Triggers
supported for Azure Functions Runtime 1.x:
• Blob storage
• Azure Cosmos DB
• Event Grid
• Event Hubs
• IoT Hub
• Queue storage
• Service Bus
• Timer
The following are Triggers supported for Azure Functions Runtime 2.x and more:
• Blob storage
• Azure Cosmos DB
• Dapr
• Event Grid
• Event Hubs
45
Chapter 3 Java-Based Azure Functions
• IoT Hub
• Kafka
• Queue storage
• RabbitMQ
• Service Bus
• Timer
The following are the Bindings supported along with Input and Output directions
supported for Azure Functions Runtime 1.x:
• SendGrid (Output)
• Twilio (Output)
The following are the Bindings supported along with Input and Output directions
supported for Azure Functions Runtime 2.x and above:
• Blob storage (Input, Output)
46
Chapter 3 Java-Based Azure Functions
• SendGrid (Output)
• Twilio (Output)
• Kafka (Output)
• RabbitMQ (Output)
You cannot create Kafka and RabbitMQ Triggers using Consumption Plan. Dapper
Triggers are applicable for Azure Kubernetes Service.
47
Chapter 3 Java-Based Azure Functions
Provide subscription details, resource group details, the name for the Storage
Account, and location. Click Review + create as in Figure 3-5.
Click Create as in Figure 3-6. This will provision a Storage Account for you.
49
Chapter 3 Java-Based Azure Functions
Once the Storage Account gets created, navigate to the Storage Account, go to the
Access Keys section as in Figure 3-7, and copy the connection string. Later, we will use the
connection string to connect to the Storage Account from the Azure Function.
50
Chapter 3 Java-Based Azure Functions
Now let us start creating Azure Function using the Maven plug-in. As a prerequisite,
you should have the following utilities already installed:
• Java 11
• Azure CLI
Launch Visual Studio Code, open the folder where you plan to work, and open a new
command prompt based on Terminal. Execute the following command as in Listing 3-1
in the Terminal. This will create an Azure Function Code for you locally. We will modify
the code further.
51
Chapter 3 Java-Based Azure Functions
When prompted, provide the group ID, artifact ID, package name, and version for
your Java function and in the end provide your selection confirmation by hitting Y as in
Figure 3-8.
The Function gets created with HTTP Trigger Binding that would invoke the
Function code from the browser. Let us add an output binding to the Storage Queue.
Open the Function.java file as in Figure 3-9.
52
Chapter 3 Java-Based Azure Functions
Replace the existing code with the following code as in Listing 3-2. We have
added a Queue output binding that integrates with the Storage Account provided
with the connection string name AzureWebJobsStorage. We will later set the value of
AzureWebJobsStorage in the Azure Function App Configuration. You can see that we
are using declarative configuration using the attribute QueueOutput to connect to the
Storage Queue without needing to write much code. The queuename parameter value
specifies the Queue name in the Storage Account. The line of code msg.setValue("Name
received : "+name); adds the message to the Queue.
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.OutputBinding;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.annotation.QueueOutput;
53
Chapter 3 Java-Based Azure Functions
import java.util.Optional;
/**
* Azure Functions with HTTP Trigger.
*/
public class Function {
/**
* This function listens at endpoint "/api/HttpExample". Two ways to
invoke it using "curl" command in bash:
* 1. curl -d "HTTP Body" {your host}/api/HttpExample
* 2. curl "{your host}/api/HttpExample?name=HTTP%20Query"
*/
@FunctionName("HttpExample")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
@QueueOutput(
name = "msg",
queueName = "outqueue",
connection = "AzureWebJobsStorage")
OutputBinding<String> msg,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
} else {
return request.createResponseBuilder(HttpStatus.OK).body
("Hello, " + name).build();
}
}
}
We have added a QueueOutput binding to our Function. We need to edit the test
class as well. Open the FunctionTest.java class and replace the following code as in
Listing 3-3. We are replacing the Function().run method call to include the Output
Binding we created in the parameter.
package com.myfunc;
import com.microsoft.azure.functions.*;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.util.*;
import java.util.logging.Logger;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
/**
* Unit test for Function class.
*/
public class FunctionTest {
/**
* Unit test for HttpTriggerJava method.
*/
@Test
public void testHttpTriggerJava() throws Exception {
// Setup
@SuppressWarnings("unchecked")
55
Chapter 3 Java-Based Azure Functions
doAnswer(new Answer<HttpResponseMessage.Builder>() {
@Override
public HttpResponseMessage.Builder answer(InvocationOnMock
invocation) {
HttpStatus status = (HttpStatus) invocation.
getArguments()[0];
return new HttpResponseMessageMock.HttpResponse
MessageBuilderMock().status(status);
}
}).when(req).createResponseBuilder(any(HttpStatus.class));
// Invoke
final OutputBinding<String> msg = (OutputBinding<String>)
mock(OutputBinding.class);
final HttpResponseMessage ret = new Function().run(req, msg,
context);
// Verify
assertEquals(ret.getStatus(), HttpStatus.OK);
}
}
Now let us modify the pom.xml file. You may choose to replace the resource group
name, Function App name, and Function location with a value of your choice as in
Listing 3-4. We need to make sure that the Function App name is unique and no other
56
Chapter 3 Java-Based Azure Functions
Azure Function exists with this name. For example, you can use myfunc-demo-28. If this
name is not taken up by anyone in the Azure infrastructure, then you will not get any
error, and you can use it.
<groupId>com.myfunc</groupId>
<artifactId>funcdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>
<azure.functions.maven.plugin.version>1.14.1</azure.functions.
maven.plugin.version>
<azure.functions.java.library.version>1.4.2</azure.functions.java.
library.version>
<functionAppName>myfunc-demo-28</functionAppName>
</properties>
<dependencies>
<dependency>
<groupId>com.microsoft.azure.functions</groupId>
<artifactId>azure-functions-java-library</artifactId>
<version>${azure.functions.java.library.version}</version>
</dependency>
57
Chapter 3 Java-Based Azure Functions
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>${azure.functions.maven.plugin.version}</version>
<configuration>
<!-- function app name -->
<appName>${functionAppName}</appName>
<!-- function app resource group -->
<resourceGroup>rg-myjavafunc-demo</resourceGroup>
<!-- function app service plan name -->
58
Chapter 3 Java-Based Azure Functions
<appServicePlanName>java-functions-app-service-plan
</appServicePlanName>
<!-- function app region-->
<!-- refers https://github.com/microsoft/azure-
maven-plugins/wiki/Azure-Functions:-Configuration-
Details#supported-regions for all valid values -->
<region>westus</region>
<!-- function pricingTier, default to be consumption if
not specified -->
<!-- refers https://github.com/microsoft/azure-
maven-plugins/wiki/Azure-Functions:-Configuration-
Details#supported-pricing-tiers for all valid
values -->
<!-- <pricingTier></pricingTier> -->
<!-- Whether to disable application insights, default
is false -->
<!-- refers https://github.com/microsoft/azure-maven-
plugins/wiki/Azure-Functions:-Configuration-Details for
all valid configurations for application insights-->
<!-- <disableAppInsights></disableAppInsights> -->
<runtime>
<!-- runtime os, could be windows, linux or docker-->
<os>windows</os>
<javaVersion>11</javaVersion>
</runtime>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~3</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
59
Chapter 3 Java-Based Azure Functions
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
<!--Remove obj folder generated by .NET SDK in maven clean-->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<filesets>
<fileset>
<directory>obj</directory>
</fileset>
</filesets>
</configuration>
</plugin>
</plugins>
</build>
</project>
Let us build and package the Azure Function code using the following Maven
command as in Listing 3-5.
Log in to Azure using the following Azure CLI command as in Listing 3-6
az login
60
Chapter 3 Java-Based Azure Functions
mvn azure-functions:deploy
Note With WSL, the default authentication type is DEVICE_CODE for Azure
login. We need to open a browser, input the device code, and log in. The default
authentication type for CMD is Azure CLI. No extra login is needed if you are
already logged in.
If you run into a name conflict error, you must change functionAppName
in pom.xml with a unique name and run mvn clean package and mvn azure-
functions:deploy again.
Once the build completes, copy the Function URL as in Figure 3-10. We will use this
to invoke the Azure Function from the browser later.
Navigate to the Azure Function that we created. You can find it inside the resource
group provided in the POM file. Go to the Configuration section. You will find the Edit
button. Replace the value with the Storage Account connection string that we created
earlier. Hit the OK button to save the value. Click Save as in Figure 3-11 to save the
application settings.
61
Chapter 3 Java-Based Azure Functions
Now invoke the Function URL with a query string parameter name in the browser as
in Listing 3-9.
https://myfunc-demo-28.azurewebsites.net/api/httpexample?name=Abhishek
Go to the Storage Account and see the queue item added as in Figure 3-12.
62
Chapter 3 Java-Based Azure Functions
Summary
In this chapter, we learned the details of Azure Functions, explored the hosting plans
available for Azure Functions, and then learned the concept of Triggers and Bindings.
We then developed an Azure Function that gets triggered using an HTTP Trigger and
puts a message in the Storage Queue using Output Queue Binding. We then deployed
the Function to Azure using the Maven plug-in. In the next chapter, we will learn how to
containerize a Java application and run it on Azure Kubernetes Service.
The following are the key takeaways from this chapter:
63
Chapter 3 Java-Based Azure Functions
• Triggers wake Azure Functions from an idle state and start their
execution. Bindings help the Azure Function exchange data with
other Azure or non-Azure services. You create Triggers and Bindings
declaratively without needing to write much code.
64
CHAPTER 4
Containerizing Java
Applications with Azure
Kubernetes Service
Containers are modern application hosting options today. You can build your
application, containerize your application along with the hosting environment and
application dependencies, and then keep the containerized image in a centralized
registry. You can pull the containerized image in the target environments and run it as
containers with ease. You can build the image once and run it in many environments
without setting up any hosting or application dependencies. An application may
consist of multiple containers. For example, you can have a container for the user
interface, a container for the business layer, and another container for data access. All
these containers in the application need to communicate with each other securely.
The containers should be always up and running to serve the client request. You
should manage all these containers with ease and address architectural concerns
like availability, reliability, scalability, and many more. Kubernetes is a container
orchestration platform that would help you orchestrate and manage these containers. It
would address all cross-cutting concerns that we discussed.
In the previous chapter, we learned the basics of Azure Functions. We then created
a Java-based Azure Function and deployed it on Azure. In this chapter, we will learn the
details of Azure Kubernetes Service, and then we will containerize a Java application and
run it on Azure Kubernetes Service.
65
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_4
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Structure
In this chapter, we will discuss the following aspects of containerizing Java applications
with Azure Kubernetes Service:
• Introduction to containers
Objectives
After studying this chapter, you should be able to get the following learnings:
Introduction to Containers
We build an application and make it ready for production use. To host the application,
we purchase a hosting server and an operating system and install the hosting software
and all necessary dependencies to run. Once the hosting environment is ready, along
with the application dependencies, we host the application on the server. We end up
spending a lot of time and effort in procuring a hosting server and making it ready for
the application to run on it. We sometimes get into the complexities of configuring
the hosting environment and the dependencies that further add to the delay. Suppose
you have multiple environments in your enterprise. In that case, you will have to
spend the same amount of effort to get the application up and running across multiple
environments like test, acceptance, or production.
You may choose to host your application on Virtual Machines. You can purchase
a powerful server and run multiple Virtual Machines on the server. In the case of
Virtual Machines, you purchase a server, install an operating system on the server
referred to as the host operating system, and then install a virtualization software like
Hyper-V. The virtualization software virtualizes the underlying server hardware like the
CPU, RAM, disks, and other infrastructure and runs multiple Virtual Machines on the
66
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
server. These Virtual Machines run in isolation and have their share of the virtualized
hardware infrastructure. Each virtual machine has an operating system referred to
as the guest operating system. You need to install the hosting software like Tomcat or
IIS, or node.js and application dependencies on these Virtual Machines and then host
your applications on these Virtual Machines. Here in the case of the virtual machine,
you need to spend effort in preparing the hosting environment and making the virtual
machine ready to host the application. This application hosting approach is almost
the same as hosting your application on a physical server. However, the advantage you
get here is that you share the underlying hardware infrastructure and run multiple
applications in isolation in the Virtual Machines. This approach saves you cost.
Containers are a smarter way of hosting your application. You can build your
application, containerize it, and keep it in a container registry along with all necessary
application dependencies. The container registry stores the container images. Azure
Container Registry and Docker Hub are examples of container registry. You may choose
to allow everyone to use your images without needing any authentication. You will have
to store your container images in a public container registry like the Docker Hub for such
scenarios. You may use a private container registry like Azure Container Registry and
provide your container images to authenticated users who have access to the container
registry.
Once you have the container image in the container registry, you can get a server
with an operating system and install the container runtime software like containerd
or Docker. And then pull the container image and run it on the server. You need not
configure the hosting environment or the application dependencies. The container
image has the application dependencies and the hosting software along with the
application. Once you pull the container image and run it as a container, your
application is up and running. The container runtime virtualizes the underlying
operating system and runs the containers as operating system threads. Figure 4-1
demonstrates the difference between Virtual Machines and containers.
67
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
68
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
the production environment. Think of a scenario when the data access container goes
down. In that case, the entire application will go down as the business layer container
will not connect with the database without the data access layer. Also, what will happen
if there is a sudden surge in the incoming request for the application. We may have to
add more containers to handle the incoming requests dynamically. These containers
should be able to communicate securely. The container and application health should
be monitored for performance degradation and failures. You must take care of all these
concerns.
There can be thousands of containers in an application to manage. To make your life
easy, we have container orchestrators like Kubernetes, Docker Swarm, and many more.
These orchestrators manage these containers and ensure that the containers are highly
available, reliable, scalable, fault-tolerant, and secured. They address all the concerns
we discussed for the containers in an application. For example, if a container dies, the
orchestrator will spin up another container in no time to replace the container that died.
Kubernetes is an open-source container orchestrator offering from Google. It
consists of a master node or the control plane and multiple worker nodes. Your
containers run inside the pods in the worker nodes. Virtual Machines are worker nodes
in most cases. If you are planning to scale at a very high rate, then you can use serverless
nodes that are Azure Container Instances. Serverless nodes can scale automatically
based on incoming traffic and spin out much faster as compared to Virtual Machines.
The control plane controls and manages the worker nodes. It decides which worker
node to run the pods and closely monitors the pods and the worker nodes. You can run
identical containers called replicas as a replica set. For example, you can have multiple
pods for the data access tier in a replica set. If one of the replicas in a replica set goes
down, other replicas are still available to serve the request until another replica spins
up. You can configure your Kubernetes cluster to scale the pods or the worker nodes
automatically when the incoming load increases.
Setting up the Kubernetes cluster is cumbersome. You may have to spend a lot
of effort and time to get your Kubernetes cluster up and running. The control plane
is the most complex part of the Kubernetes cluster. Cloud providers like Amazon,
Microsoft, and Google help you spin out the Kubernetes cluster in minutes. They provide
Kubernetes as a service on their cloud and abstract all necessary complexities while
spinning up the Kubernetes cluster and managing the control plane for you. You have
no control over the control plane. The underlying cloud platform manages the control
plane for you, and you can manage the worker nodes and the application container
69
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Note Load balancer service exposes your application on HTTP port, while node
port exposes your application on a nonstandard port from 30000 to 32767.
The application containers running inside pods should be exposed to the end users
to access the application. Even you may have scenarios where a pod should be exposed
only inside the cluster for another pod to consume. Such scenarios are handled using
the Kubernetes services running inside the cluster. A cluster IP service allows pods to
communicate internally. The load balancer service and the node port service allow the
pods to be exposed to the end users. Let us go back to the three-tier application example
that we discussed earlier. We need to expose the user interface tier to the end user. The
user interface tier should communicate with the business logic tier, and the business
logic tier should communicate with the data access tier. The business logic tier and the
data access tier should not be exposed to the external users, and its access should be
limited inside the cluster. To address this scenario, we can have a load balancer service
in front of the user interface tier so that the user interface tier gets exposed to the end
user. You can have a cluster IP service in front of the business logic tier and the data
access tier to limit its communication within the cluster.
As a developer, you containerize the application and push it to the container registry.
You then spin up the Kubernetes cluster and build a deployment YAML file for the
Kubernetes cluster. The deployment YAML file will have all necessary details like the
number of replicas, services, containers to run on the cluster, and many more details
needed for the application containers to run on Kubernetes worker nodes. You then
pass on this deployment file to the control plane. The control plane then schedules the
pods on the worker nodes based on the information you have provided in the YAML
file. The pods will pull the container images from the container registry specified in the
deployment file and then start them as containers.
70
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Click on the Containers tab. You will get all Container-related offerings here, and you
can see them in Figure 4-3. Click Container Registry.
71
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Provide subscription, resource group, name, SKU, and location for the container
registry. Click Review + create as shown in Figure 4-4.
72
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Click Create as shown in Figure 4-5. This will spin up an Azure Container Registry
for you.
73
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Once the container registry gets created, go to the Azure portal and navigate to the
container registry. Click Access keys as shown in Figure 4-6. Enable Admin user. Copy
the login server, username, and password. We will use these credentials to push the
container images to the container registry later.
74
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Go to the Containers tab and click Kubernetes Service as shown in Figure 4-8.
75
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
76
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Scroll down and provide cluster name, region, and other necessary details as shown
in Figure 4-10.
77
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Scroll down and provide the number of nodes in the cluster as 1 as shown in
Figure 4-11. The higher the number of nodes, the higher the cost for the cluster.
78
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Go to the Integrations tab. Select the Azure Container Registry that we created
earlier. This step would help us integrate Azure Container Registry with the Kubernetes
cluster, and the pods running inside the nodes can pull the container images from the
container registry seamlessly. You can enable container monitoring that will help you
get the performance metrics and logs for the cluster. Click Review + create as shown in
Figure 4-12.
79
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Click Create as shown in Figure 4-13. This will spin up the Kubernetes cluster.
80
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
https://start.spring.io/
Let us provide the group name, artifact ID, and other necessary details for your Java
application as shown in Figure 4-14.
81
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Add the Spring Web dependencies as shown in Figure 4-15. Generate the Java
application project and download it.
82
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
In the Java application, modify the Java file with the main method shown in Listing 4-2.
We need to add a service accessible using the hello route. The service returns the value
Hello World !!.
package com.aksdemo.aksdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class AksdemoApplication {
@GetMapping("/hello")
public String hello() {
return String.format("Hello World !!");
}
Now let us build the application using the command shown in Listing 4-3.
Once the Maven build is successful, we can containerize the application. The JAR
file will get generated inside the target folder. The target folder gets created once the
build is successful. Let us create the Docker file using the following code listing. The file
should be named Dockerfile without any extension. Make sure you create the Docker
file as shown in Listing 4-4 in the directory where the application pom.xml file is there.
83
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
The Docker file uses the base image as openjdk:11. It will copy the application JAR file to
the /usr/app folder. Make sure that you replace the JAR name in the Docker file listed as
follows with the name of the JAR file that gets generated for you in the target folder. It will
then expose port 8080 and define the application JAR file as the entry point so that the
JAR will start up whenever the container boots up.
FROM openjdk:11
COPY target/aksdemo-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app
EXPOSE 8080
ENTRYPOINT ["java","-jar","aksdemo-0.0.1-SNAPSHOT.jar"]
Now let us run the command shown in Listing 4-5 to containerize the application.
You should run this command where the Docker file is present.
Now let us verify if the container image gets created as shown in Figure 4-16 by
running the command shown in Listing 4-6.
docker image ls
84
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Let us start the container and check if we can access the application. You can start
the application using the command shown in Listing 4-7. We are mapping the system
port 8080 on your laptop to port 8080 of the container where the application is running.
Once the Docker container gets started, browse the URL shown in Listing 4-8 as in
Figure 4-17.
http://localhost:8080/hello
Now let us push the container image to the Azure Container Registry that we created
earlier. Run the command shown in Listing 4-9 to log in to the Azure Container Registry.
You will be prompted for credentials that you will get in the Access keys section of the
Azure Container Registry.
85
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Run the command shown in Listing 4-10 to tag the container image with the Azure
Container Registry name. Replace [ACRName] with the name of your Azure Container
Registry.
Listing 4-10. Tag the Docker image with the Azure Container Registry
Push the container image to the container registry shown in Listing 4-11. Replace
[ACRName] with the name of your Azure Container Registry.
Listing 4-11. Push the Docker image to the Azure Container Registry
Once the image gets pushed, you can see the image in the Azure Container Registry
in the Repositories section as shown in Figure 4-18.
86
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
87
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
We need to fetch the credentials locally on the cloud shell and authenticate with the
Azure Kubernetes Service. Run the command shown in Listing 4-12 to get authenticated.
Now let us open the nano editor and create the deploy.yaml file. In this YAML, we
specify the container image repository details, the number of replicas, the load balancer
service for exposing the pod to the Internet, and the port details where the image
would run on the pod and the load balancer service. In a nutshell, we are defining the
Kubernetes deployment configuration with three replicas and a load balancer service
that will expose the application to the Internet. Run the command shown in Listing 4-13
to open the nano editor.
nano deploy.yaml
Paste the YAML shown in Listing 4-14 in the deploy.yaml. Save the YAML and close
the nano editor.
Note You need to provide the image path in Listing 4-14 based on the Azure
Container Registry you are using and the name of the container image that you
created.
88
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: mydemoapp
labels:
app: mydemoapp
spec:
selector:
matchLabels:
app: mydemoapp
replicas: 3
template:
metadata:
labels:
app: mydemoapp
spec:
containers:
- name: mydemoapp
image: acrdemo28.azurecr.io/aksdemo:demo
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 200m
memory: 200Mi
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: mydemoappservice
spec:
89
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
type: LoadBalancer
ports:
- port: 8080
selector:
app: mydemoapp
Run the command shown in Listing 4-15 to deploy the application container image
in the Azure Kubernetes Service.
We have specified three replicas, so we should have three pods running in the
cluster. Run the command shown in Listing 4-16 to list the running pods as shown in
Figure 4-20.
Run the command shown in Listing 4-17 to inspect the load balancer service. Copy
the external IP address as shown in Figure 4-21 for the load balancer service. We need
this IP address to browse the application running on the cluster.
90
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
Browse the external IP address and you can see the application output as shown in
Figure 4-22.
Summary
In this chapter, we learned the details of containers and Kubernetes. We then learned
what Azure Kubernetes Service is. We then developed a Java Spring Boot application,
containerized it in a Docker image, and pushed it to the Azure Container Registry.
We pulled the container image and ran it on the Azure Kubernetes Service cluster. We
explored basic Docker and kubectl commands that will help us interact with the Docker
engine and the Kubernetes cluster. In the next chapter, we will build a Java application
and run it on Azure Spring Cloud.
The following are the key takeaways from this chapter:
91
Chapter 4 Containerizing Java Applications with Azure Kubernetes Service
92
CHAPTER 5
Running Java
Applications on Azure
Spring Cloud
You are building a Spring Boot–based microservices application that you will host on
Azure using the Platform-as-a-Service model. You need to take care of concerns like
keeping application configurations for different environments, integrating with Platform-
as-a-Service (PaaS) databases on Azure, and leveraging Azure Redis for caching needs.
You may have to integrate application insights for logging and monitoring and configure
automatic scaling for your services and many more. You also need to integrate your
application hosting environment with a Git repository, Azure DevOps pipelines, or
Jenkins. And you need to achieve all these requirements without making any code
changes and use configurations as much as possible. Azure Spring Cloud will help you
here with ease.
In the previous chapter, we learned the basics of Azure Kubernetes Service. We then
created a Java-based application, containerized it, and deployed it on Azure Kubernetes
Service. In this chapter, we will learn the details of Azure Spring Cloud, and then we will
provision an Azure Spring Cloud service and deploy the application in the service.
Structure
In this chapter, we will discuss the following aspects of deploying Java applications on
Azure Spring Cloud:
93
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_5
Chapter 5 Running Java Applications on Azure Spring Cloud
Objectives
After studying this chapter, you should be able to get the following learnings:
94
Chapter 5 Running Java Applications on Azure Spring Cloud
for end-to-end application life cycle management. You can address enterprise-level
concerns like monitoring, auditing, security, scalability, availability, and many more
with ease.
Click Create as in Figure 5-2. You will get navigated to screen where you can create
Azure Spring Cloud.
95
Chapter 5 Running Java Applications on Azure Spring Cloud
Provide resource group name, subscription name, spring cloud name, location, and
tier. Click Review + create as shown in Figure 5-3.
96
Chapter 5 Running Java Applications on Azure Spring Cloud
Click Create as shown in Figure 5-4. This action will provision Azure Spring Cloud.
97
Chapter 5 Running Java Applications on Azure Spring Cloud
Once the Azure Spring Cloud service gets created, navigate to the service in the
portal. We need to create an App instance to deploy the Spring Boot application. Go to
the Apps section and click Create App as shown in Figure 5-5.
98
Chapter 5 Running Java Applications on Azure Spring Cloud
Provide the instance name, runtime version as Java 11, instance count as 1, and other
compute details. Click Create as shown in Figure 5-6. The app instance will get created.
99
Chapter 5 Running Java Applications on Azure Spring Cloud
Click on the App we created as shown in Figure 5-7. You will get navigated inside the
App we created.
100
Chapter 5 Running Java Applications on Azure Spring Cloud
We need to access the app using a public URL. Click Assign endpoint as shown in
Figure 5-8.
101
Chapter 5 Running Java Applications on Azure Spring Cloud
Once the URL gets assigned, click on the URL as shown in Figure 5-9.
102
Chapter 5 Running Java Applications on Azure Spring Cloud
The URL will serve you the default app running on the Spring Cloud service as
shown in Figure 5-10.
103
Chapter 5 Running Java Applications on Azure Spring Cloud
https://start.spring.io/
104
Chapter 5 Running Java Applications on Azure Spring Cloud
Provide group, artifact, name, and other basic details for the project. Select 11 as Java
version and JAR as packaging as shown in Figure 5-11.
Add the Spring Web and Spring Data Reactive Redis dependencies and generate the
project as shown in Figure 5-12.
105
Chapter 5 Running Java Applications on Azure Spring Cloud
We need to integrate Azure Redis Cache with the Azure Spring Cloud application
instance we created. Go to the application instance and then navigate to the Service
bindings section. Click Create service binding as shown in Figure 5-13.
Provide necessary details for the service binding. We need to give a name for the
service binding, select the Azure Redis Cache that we need to bind, and provide the
106
Chapter 5 Running Java Applications on Azure Spring Cloud
access key. You can create a Redis Cache by following the steps mentioned in Chapter 9.
Make sure that you select Use SSL as False as shown in Figure 5-14. The dependency
we are using in the application code to work with Azure Redis Cache does not support
SSL. To keep things simple, we are using this dependency. However, other dependencies
are available for Redis Cache that will work with SSL.
107
Chapter 5 Running Java Applications on Azure Spring Cloud
Modify the Java file having the main function as in Listing 5-2. We have added the
hello method where we are writing the value Hello World !!!! to the Azure Redis Cache
using the key helloworld. And we are rereading the value and returning from the API call.
package com.springcloud.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootApplication
@RestController
public class DemoApplication {
@Autowired
private StringRedisTemplate template;
@RequestMapping("/hello")
public String hello() {
ValueOperations<String, String> ops = this.template.opsForValue();
108
Chapter 5 Running Java Applications on Azure Spring Cloud
Execute the Maven command shown in Listing 5-3 to build and package the JAR file.
Execute the Maven command shown in Listing 5-4 to configure the POM file with
Azure Spring Cloud configuration. We will generate Java 8 version POM file that we can
change later to Java 11.
mvn com.microsoft.azure:azure-spring-cloud-maven-plugin:1.7.0:config
Select your subscription when prompted. Provide the name of the app as the one we
created earlier. Ensure that you provide the necessary input to make it a public app when
prompted as shown in Figure 5-15.
Figure 5-15. Update the POM file to include Azure Spring Cloud Plugin
The POM file will get updated with azure-spring-cloud-maven-plugin along with its
dependencies. Make sure that you have Java 11 in the plug-in configuration.
Execute the Maven command shown in Listing 5-5 to deploy the JAR file to the Azure
Spring Cloud. Browser will prompt you authenticate and sign in to Azure.
109
Chapter 5 Running Java Applications on Azure Spring Cloud
mvn azure-spring-cloud:deploy
Make sure that you have enabled non-SSL access for the Azure Redis Cache as shown
in Figure 5-16.
Once the deployment is complete, browse the app URL for the hello API, and you
can see the value stored in Azure Redis Cache in response as shown in Figure 5-17.
110
Chapter 5 Running Java Applications on Azure Spring Cloud
Summary
In this chapter, we learned the details of Azure Spring Cloud. We learned how to
integrate other Azure services with application running on Azure Spring Cloud using
service bindings and also explored how to use Maven commands to deploy application
jars to Azure Spring Cloud. We then developed a Java Spring Boot application and
deployed it on Azure Spring Cloud. In the next chapter, we will learn how to work with
Azure Storage programmatically from Java applications.
The following are the key takeaways from this chapter:
• Azure Spring Cloud helps you host Spring Boot applications on Azure
without making code changes.
• Using service bindings, you can integrate with other Azure services
without making code changes.
111
PART II
Integrating Java
Applications with Popular
Azure Services
CHAPTER 6
Structure
In this chapter, we will discuss the following aspects of the Azure Storage service:
• Introduction to Azure Storage
115
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_6
Chapter 6 Integrating with an Azure Storage Account
Objectives
After studying this chapter, you should be able to do the following:
• Azure Containers (Blobs) help you store images, videos, files, and
other binary large object data. You can keep big unstructured data
like Virtual Machine disks.
• Azure Files provide a managed file share to keep your files and access
using SMB, NFS, or HTTP protocol.
Data replication is an important design aspect for data stores on the cloud. This
mechanism helps you recover data during a disaster or an outage and keeps your
business running. Azure Storage supports the following data replications or redundancy
mechanisms:
• Locally Redundant Storage (LRS) helps you store three copies of your
data in the same data center. If the data center goes down, you will
not be able to access the data.
116
Chapter 6 Integrating with an Azure Storage Account
You can choose either a standard tier or a premium tier to store the data on Azure
Storage. The Standard tier stores your data in a standard hard disk, and the premium tier
stores your data in an SSD disk. The premium tier is more performant than the standard
tier. The following are the types of Storage Accounts available on Azure:
• Blob Storage helps you store block blobs and append blobs.
It supports only the standard tier and all replication options
except ZRS.
• File Storage provides you with file service. It supports the premium
tier and LRS and ZRS replication options.
• Block Blob helps you store block blobs and append blobs. It supports
the premium tier and LRS and ZRS replication options.
117
Chapter 6 Integrating with an Azure Storage Account
Note Block Blobs store binary and text data. Page Blobs help you store large
files like virtual hard disks. Append Blobs are the same as Block Blobs but highly
performant append operations.
Search for Storage Account in the Marketplace as shown in Figure 6-2. Then
click Create.
118
Chapter 6 Integrating with an Azure Storage Account
You will now be on the Create a storage account dialog shown in Figure 6-3.
Provide subscription details, resource group name, storage account name, region, and
performance tier and click Review + create.
119
Chapter 6 Integrating with an Azure Storage Account
Click Create as shown in Figure 6-4. This would spin up the Storage Account.
120
Chapter 6 Integrating with an Azure Storage Account
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.13.0</version>
121
Chapter 6 Integrating with an Azure Storage Account
</dependency>
Go to the Storage Account in the Azure portal and get the connection string for the
Storage Account in the Access keys section as shown in Figure 6-5.
Now go to the main method of the Java class and add the following code to create
a blob container. You need to create a BlobServiceClient object to help you work with
the blob containers inside the Storage Account. You need to list all available containers
using the listBlobContainers function and check if your container exists. Create the
container using function createBlobContainer if it does not exist, else get the reference to
the existing container using function getBlobContainerClient.
122
Chapter 6 Integrating with an Azure Storage Account
//If the container does not exist then create the container
// If the container exists then get reference for the existing container
BlobContainerClient container;
if(!containerExists) {
// Create container
container = blobClient.createBlobContainer(containerName);
}
else {
// Get reference to the existing container
container = blobClient.getBlobContainerClient(containerName);
}
Now let us create a blob with some data inside the container. Use the getBlobClient
function to create a reference for the blob you are planning to upload, and then use the
upload method to upload the blob with content Hello Blob !!!!. The code is demonstrated
in Listing 6-3.
123
Chapter 6 Integrating with an Azure Storage Account
Now let us iterate all the blobs in our container and get the data in each blob. Use the
listBlobs function to get all the blobs inside the container. Iterate through all the blobs
and use the getBlobClient function to reference the blob. Use the download function to
get the blob in an output stream as in Listing 6-4.
Listing 6-5 is the complete code performing operations on the blob container.
import com.azure.storage.blob.*;
import com.azure.storage.blob.models.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
124
Chapter 6 Integrating with an Azure Storage Account
//If the container does not exist then create the container
// If the container exists then get reference for the existing
container
BlobContainerClient container;
if(!containerExists) {
// Create container
container = blobClient.createBlobContainer(containerName);
}
else {
// Get reference to the existing container
container = blobClient.getBlobContainerClient(containerName);
}
125
Chapter 6 Integrating with an Azure Storage Account
blob.upload(dataStream,data.length(),true);
You can also see the container created and the blob in the Azure portal as shown in
Figure 6-6 once you execute the project.
126
Chapter 6 Integrating with an Azure Storage Account
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-queue</artifactId>
<version>12.0.1</version>
</dependency>
127
Chapter 6 Integrating with an Azure Storage Account
We have already copied the connection string from the Azure portal. Let us go to
the main method and add the following code to create the queue. We are getting the
QueueCient object and creating a queue using the create function as in Listing 6-7.
//Create Queue
queueClient.create();
We are adding messages to the queue using sendMessage function as in Listing 6-8.
We are reading the first message from the queue using the peekMessage function as
in Listing 6-9.
We are reading all the messages from the queue and deleting the read messages
using the receiveMessages function as in Listing 6-10.
128
Chapter 6 Integrating with an Azure Storage Account
//Get all messages from the queue and remove them from the queue
var messages = queueClient.receiveMessages(queueClient.getProperties().
getApproximateMessagesCount());
for(QueueMessageItem msg:messages){
System.out.println("Added Message : "+msg.getMessageText());
}
Listing 6-11 shows the complete code to perform queue operations. You can check
the item that got added in the Storage Queue. Go to the storage account in the Azure
portal and click Storage browser and then click Queue.
import com.azure.storage.queue.*;
import com.azure.storage.queue.models.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
//Create Queue
queueClient.create();
129
Chapter 6 Integrating with an Azure Storage Account
//Get all messages from the queue and remove them from the queue
var messages = queueClient.receiveMessages(queueClient.
getProperties().getApproximateMessagesCount());
for(QueueMessageItem msg:messages){
System.out.println("Added Message : "+msg.getMessageText());
}
}
}
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-data-tables</artifactId>
<version>12.1.5</version>
</dependency>
We have already copied the connection string from the Azure portal. Let us go to
the main method and add the following code to create the table. We are getting the
TableServiceClient object and creating a queue using the createTableIfNotExists function
as in Listing 6-13. The table will only be created if it does not exist.
130
Chapter 6 Integrating with an Azure Storage Account
String tableName="students";
String connectionString="{Provide Connection String}";
// Create table
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
TableClient tableClient = tableServiceClient.createTableIfNotExists(t
ableName);
We created the TableEntity object using the row key and the partition key for the
data. We then populated the properties for the TableEntity object with the data we need
to insert into the table as in Listing 6-14.
tableClient.createEntity(entity);
tableClient.createEntity(entity);
131
Chapter 6 Integrating with an Azure Storage Account
We can form a filter query and fetch the data stored in the table as in Listing 6-15.
The listEntities function fetches the data from the table.
Listing 6-16 shows the complete code for the table operation.
import com.azure.data.tables.*;
import com.azure.data.tables.models.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
132
Chapter 6 Integrating with an Azure Storage Account
.connectionString(connectionString)
.buildClient();
TableClient tableClient = tableServiceClient.createTableIfNotExists
(tableName);
tableClient.createEntity(entity);
tableClient.createEntity(entity);
133
Chapter 6 Integrating with an Azure Storage Account
"\t" + tableEntity.getProperty("Marks"));
});
}
}
Let us check the table that got added in the Storage Account. Go to the storage
account in the Azure portal and click Storage browser and then click Tables as shown in
Figure 6-7.
You can see the table that we created as shown in Figure 6-8.
134
Chapter 6 Integrating with an Azure Storage Account
Click on the table we created. You can see the data we added in the table as shown in
Figure 6-9.
135
Chapter 6 Integrating with an Azure Storage Account
S
ummary
In this chapter, we learned the details of Azure Storage and explored the concept of
Blob, Queue, Table, and File. We learned how to create an Azure Storage using the
Azure portal. We then developed a Java code and then performed operations on Queue,
Table, and Blob. In the next chapter, we will learn how to work with Azure SQL database
programmatically from Java applications.
The following are the key takeaways from this chapter:
• Azure Containers (Blobs) help you store images, videos, files, and
other binary large object data.
• Azure Files provide a managed file share to keep your files and access
using SMB, NFS, or HTTP protocol.
• Azure Queues provide a queue-based messaging store for your
application.
• Azure Tables help you store semi-structured NoSQL data.
136
CHAPTER 7
Structure
In this chapter, we will discuss the following aspects of the Azure Storage service:
Objectives
After studying this chapter, you should be able to get the following learnings:
137
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_7
Chapter 7 Azure SQL from Java Applications
Note DTU stands for Database Transaction Unit and is a number based on the
combination of CPU, Memory, and Data I/O performance specification.
Azure SQL database does not support Microsoft SQL Server features like linked
server, SQL agent, SQL data sync, SQL Auditing, Service Broker, and many more. You
may face challenges in migrating an on-premises SQL Server database as is to Azure if
138
Chapter 7 Azure SQL from Java Applications
you are using such features. However, you can use Azure SQL Managed Instance in such
cases that supports almost all the on-premises SQL Server Enterprise Edition capabilities
on Azure. Azure SQL Managed Instance is also a Platform-as-a-Service relational
database offering on Azure. You can get native Virtual Network Integration with Azure
SQL Managed Instance that is not supported by Azure SQL Database. However, features
like SQL Server Analysis Services (SSAS) and SQL Server Reporting Services (SSRS) are
not supported by Azure SQL Managed Instance and Azure SQL Database.
You will get navigated to Azure Marketplace. Click Databases and then click SQL
Database as in Figure 7-2.
139
Chapter 7 Azure SQL from Java Applications
Let us provide the subscription details, resource group name, and name of the
database. We need to create a database server on which the database will reside. You
may choose to reuse an existing database or create a new database. Let us create a new
one. Click Create new as in Figure 7-3.
140
Chapter 7 Azure SQL from Java Applications
Provide the name of the SQL server and the authentication details, and then click OK
as in Figure 7-4. You will get navigated back to the basic tab.
141
Chapter 7 Azure SQL from Java Applications
Scroll down and select the right compute and storage requirement based on
your need. You will get billed accordingly. Click Next : Networking to navigate to the
networking tab as in Figure 7-5.
142
Chapter 7 Azure SQL from Java Applications
In the demo, we will access it over the public Internet. Select Public endpoint. Add
the IP address of your laptop to the firewall settings so that you can access the database
from the code running on your laptop. Click Review + create as in Figure 7-6.
143
Chapter 7 Azure SQL from Java Applications
Review the configuration and click Create as in Figure 7-7. The Azure SQL database
will get created.
144
Chapter 7 Azure SQL from Java Applications
Once the SQL database gets created, go to the SQL database in the Azure portal and
click Connection strings as in Figure 7-8. Copy the Connection string for JDBC. We will
need this in our code to connect to the database.
145
Chapter 7 Azure SQL from Java Applications
Azure portal provides query editor. Click Query editor as in Figure 7-9, and provide
the database credentials. Now let us create a table named student.
146
Chapter 7 Azure SQL from Java Applications
Execute the script from Listing 7-1 in the query editor to create a student table.
147
Chapter 7 Azure SQL from Java Applications
https://start.spring.io/
Provide the Maven project details. Make sure that you select Java 11 as the runtime
version as in Figure 7-10.
Add the Spring Web, Spring Data JDBC, and MS SQL Server Driver dependencies as
in Figure 7-11 and generate the Java Spring Boot project.
148
Chapter 7 Azure SQL from Java Applications
Let us create a Student class as in Listing 7-3 in the folder where the
DemoApplication class is present. The Student class works as a domain model for the
student database table.
package com.database.demo;
import org.springframework.data.annotation.Id;
public Student(){
}
@Id
private int roll;
149
Chapter 7 Azure SQL from Java Applications
150
Chapter 7 Azure SQL from Java Applications
Now let us add a StudentRepository interface as in Listing 7-4 in the same folder
where we have the Student class and the DemoApplication class. The StudentRepository
interface works as the repository class for the database and the domain model. The
Spring Data JDBC manages this repository class.
package com.database.demo;
import org.springframework.data.repository.CrudRepository;
public interface StudentRepository extends CrudRepository<Student,
Integer> {
}
logging.level.org.springframework.jdbc.core=DEBUG
spring.datasource.url=jdbc:sqlserver://[database server
name].database.windows.net:1433;database=[database
name];encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.
database.windows.net;loginTimeout=30;
spring.datasource.username=[username]@[database server name]
spring.datasource.password=[password]
spring.sql.init.mode=always
Go to the DemoApplication Java class and convert it into a REST Controller using the
RestController attribute. Add the createStudents POST method to insert a Student record
to the student table and add a getStudents GET method to fetch all the records in the
student table. See Listing 7-6 for the code.
151
Chapter 7 Azure SQL from Java Applications
package com.database.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
@RestController
@RequestMapping("/")
public class DemoApplication {
@PostMapping("/")
@ResponseStatus(HttpStatus.CREATED)
public Student createStudents(@RequestBody Student student) {
return studentRepository.save(student);
}
@GetMapping("/")
public Iterable<Student> getStudents() {
return studentRepository.findAll();
}
Run the application. Once the application is started, go to the Postman tool and fire a
POST call to insert a record. Make sure that you have added the Content-Type header as
in Figure 7-12.
152
Chapter 7 Azure SQL from Java Applications
Note To build and run your application, navigate to the application folder where
the POM file is there. Run the command mvn clean install to build the JAR.
Navigate to the folder where the JAR file got generated and run the command
java -jar [jar name] to start the application.
153
Chapter 7 Azure SQL from Java Applications
Go to the Query Editor in the SQL Server service in the Azure portal and execute a
select query, and you can see the inserted record as in Figure 7-14.
154
Chapter 7 Azure SQL from Java Applications
Go to the Postman tool and fire a GET call to get all the inserted records as in
Figure 7-15.
155
Chapter 7 Azure SQL from Java Applications
You can manage the firewall settings like setting TLS, whitelisting IP addresses to
access the database, and many more as in Figure 7-17.
156
Chapter 7 Azure SQL from Java Applications
Data at rest is encrypted using Transparent data encryption default as in Figure 7-18.
It is not advisable to turn this encryption off.
157
Chapter 7 Azure SQL from Java Applications
Summary
In this chapter, we learned Azure SQL's details and how to create an Azure SQL database
using the Azure portal. We then developed a Java Spring Boot code and performed an
insert and read operation on the Azure SQL database. In the next chapter, we will learn
how to work with Azure Cosmos DB programmatically from Java applications.
The following are the key takeaways from this chapter:
• You may choose to deploy a single database for Azure SQL that is
fully managed by the underlying Azure platform and guarantees
you isolation from other databases or choose to run multiple single
databases on an elastic pool sharing the compute resources like CPU
and memory.
158
Chapter 7 Azure SQL from Java Applications
• Azure SQL database does not support Microsoft SQL Server features
like linked server, SQL agent, SQL data sync, SQL Auditing, Service
Broker, and many more. However, you can use Azure SQL Managed
Instance in such cases that supports almost all the on-premises SQL
Server Enterprise Edition capabilities on Azure.
159
CHAPTER 8
Structure
In this chapter, we will discuss the following aspects of the Azure Cosmos DB:
Objectives
After studying this chapter, you should be able to do the following:
• SQL API helps you access the stored data using SQL queries. You
can migrate your relational database workload to Cosmos DB using
SQL APIs.
• API for MongoDB helps you access data MongoDB queries. You can
insert your data in the document structure using the MongoDB-
supported BSON format.
• Gremlin API stores data in a graph database format using edges and
vertices. You can query the data using the graph query language.
For Azure Cosmos DB, you get charged for the throughput you need and the storage
you consume on an hourly basis. The throughput is defined using Request Units.
Request Units are the combination of CPU, memory, and IOPs consumed to perform
a database operation. You can create an Azure Cosmos DB account based on your
consumption of the Restricted Units. The following are the supported ways to create a
Cosmos DB account:
162
Chapter 8 Work with Azure Cosmos DB
• You do not choose any Restricted Units using the Serverless mode,
and you get billed for the number of Restricted Units you have
consumed during the billing period.
163
Chapter 8 Work with Azure Cosmos DB
You will be taken to the Azure Marketplace in Figure 8-2. Click Databases. You will
get all the database offerings here. Click Azure Cosmos DB as in Figure 8-2.
164
Chapter 8 Work with Azure Cosmos DB
165
Chapter 8 Work with Azure Cosmos DB
Provide the subscription and the resource group details. Provide the account name
for the Cosmos DB and location. Click Review + create as in Figure 8-4.
166
Chapter 8 Work with Azure Cosmos DB
Click Create as in Figure 8-5. This action will spin up Azure Cosmos DB for you.
167
Chapter 8 Work with Azure Cosmos DB
Once the Cosmos DB gets created, navigate to the Cosmos DB and click Keys as in
Figure 8-6. We need the endpoint URL and the access key to connect to the Cosmos DB
from the Java code.
168
Chapter 8 Work with Azure Cosmos DB
<groupId>com.cosmosdb</groupId>
<artifactId>sqldemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-cosmos</artifactId>
<version>4.4.0</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
Inside a Cosmos DB database, data is stored inside a container, and the data stored
should have a partition key and ID column. Let us create a Java class as in Listing 8-2
having the Main function where we can add the code to create a database, create a
container to store student data, insert records to it, and read the records. Let us create
a Student class with the data fields for the student data. It must have the id and the
partitionKey fields. The data is stored in partitions for faster data access, and each of the
partition is identified by a partition key. A combination of partition key and ID
will uniquely identify a record.
169
Chapter 8 Work with Azure Cosmos DB
public Student(){
}
170
Chapter 8 Work with Azure Cosmos DB
Let us add the code shown in Listing 8-3 to the Main method to connect to Azure
Cosmos DB. We need to provide the endpoint URL and the key that we copied earlier
from the Azure portal.
Let us create a database named students using the code from Listing 8-4. The
database will only get created if it does not exist.
// Create Database
String databaseName = "students";
CosmosDatabaseResponse databaseDetails = client.createDatabaseIfNotExists
(databaseName);
CosmosDatabase database = client.getDatabase(databaseDetails.
getProperties().getId());
System.out.println("Database created - " + database.getId() );
Let us create a container named class where we can store student data for different
classes as in Listing 8-5. The container will only get created if it does not exist.
171
Chapter 8 Work with Azure Cosmos DB
// Create Container
String containerName = "class";
CosmosContainerProperties containerProperties = new CosmosContainer
Properties(containerName, "/partitionKey");
CosmosContainerResponse containerResponse = database.createContainerIf
NotExists(containerProperties);
CosmosContainer container = database.getContainer(containerResponse.
getProperties().getId());
System.out.println("Container created " + container.getId());
//Insert Student 2
student = new Student();
student.setId("2");
student.setMarks(60);
student.setPartitionKey("ninth-standard");
student.setName("Abhijeet");
item = container.createItem(student, new PartitionKey(student.
getPartitionKey()), new CosmosItemRequestOptions());
//Insert Student 3
student = new Student();
student.setId("3");
172
Chapter 8 Work with Azure Cosmos DB
student.setMarks(70);
student.setPartitionKey("eight-standard");
student.setName("Sunny");
item = container.createItem(student, new PartitionKey(student.
getPartitionKey()), new CosmosItemRequestOptions());
//Insert Student 4
student = new Student();
student.setId("4");
student.setMarks(60);
student.setPartitionKey("seventh-standard");
student.setName("Abhilash");
item = container.createItem(student, new PartitionKey(student.
getPartitionKey()), new CosmosItemRequestOptions());
});
Listing 8-8 is the complete code that interacts with the Azure Cosmos DB.
173
Chapter 8 Work with Azure Cosmos DB
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosDatabase;
import com.azure.cosmos.models.CosmosContainerProperties;
import com.azure.cosmos.models.CosmosContainerResponse;
import com.azure.cosmos.models.CosmosDatabaseResponse;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.models.PartitionKey;
import com.azure.cosmos.util.CosmosPagedIterable;
174
Chapter 8 Work with Azure Cosmos DB
//Insert Student 2
student = new Student();
student.setId("2");
student.setMarks(60);
student.setPartitionKey("ninth-standard");
student.setName("Abhijeet");
item = container.createItem(student, new PartitionKey(student.
getPartitionKey()), new CosmosItemRequestOptions());
//Insert Student 3
student = new Student();
student.setId("3");
student.setMarks(70);
student.setPartitionKey("eight-standard");
175
Chapter 8 Work with Azure Cosmos DB
student.setName("Sunny");
item = container.createItem(student, new PartitionKey(student.
getPartitionKey()), new CosmosItemRequestOptions());
//Insert Student 4
student = new Student();
student.setId("4");
student.setMarks(60);
student.setPartitionKey("seventh-standard");
student.setName("Abhilash");
item = container.createItem(student, new PartitionKey(student.
getPartitionKey()), new CosmosItemRequestOptions());
});
}
}
176
Chapter 8 Work with Azure Cosmos DB
Now let us go to the portal and check the data inserted in the Cosmos DB. Click Data
Explorer as in Figure 8-7. You can see the students database.
177
Chapter 8 Work with Azure Cosmos DB
Expand the Student database, and you can see the class container. Click Items as in
Figure 8-8, and you can see the student records stored.
Click on any of the records to see the data stored as in Figure 8-9.
178
Chapter 8 Work with Azure Cosmos DB
Summary
In this chapter, we learned Azure Cosmos DB and how to create an Azure SQL API–
based Azure Cosmos DB using the Azure portal. We then developed a Maven-based Java
code and performed an insert and read operation on the Azure Cosmos DB. In the next
chapter, we will learn how to work with Azure Redis Cache programmatically from Java
applications.
The following are the key takeaways from this chapter:
• Azure Cosmos DB is a NoSQL database that supports horizontal data
replication across the globe and ensures low latency.
• SQL API
• Cassandra API
• Table API
• Gremlin API
• Provisioned throughput
• Serverless
• Autoscale
179
CHAPTER 9
Structure
In this chapter, we will discuss the following aspects of the Azure Redis Cache:
181
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_9
Chapter 9 Storing Runtime Data in Azure Redis Cache
Objectives
After studying this chapter, you should be able to get the following learnings.
182
Chapter 9 Storing Runtime Data in Azure Redis Cache
Azure Redis Cache offers you very low latency and high-throughput data access for
your applications. The data is stored in the memory of the Redis server managed by the
Azure platform and can serve huge volumes of data requests with ease. The data stored
is encrypted and highly secured.
The following are pricing plans available for Azure Redis Cache:
• The Basic plan offers an open source Redis Cache version running on
a single virtual machine instance. You can use this for development
and test purposes. This plan offers no SLAs.
183
Chapter 9 Storing Runtime Data in Azure Redis Cache
You will get navigated to Azure Marketplace. Click Databases and then click Azure
Cache for Redis. See Figure 9-2 for an example.
184
Chapter 9 Storing Runtime Data in Azure Redis Cache
Provide the basic details to create the Redis Cache. You need to give the subscription
and the resource group details, a name for the Redis Cache, the location to create the
Redis Cache, and a pricing plan. We are using the Basic C0 plan with no SLA for this
demonstration, and this should be used only for development and testing purposes.
Click Next : Networking to navigate to the Networking tab as in Figure 9-3.
185
Chapter 9 Storing Runtime Data in Azure Redis Cache
Select Public Endpoint. However, you should configure a Private Link Endpoint
in the production scenario and set up a Private Endpoint. Using a higher pricing plan,
you can integrate the Redis Cache in the Virtual Network. Click Next : Advanced as in
Figure 9-4.
186
Chapter 9 Storing Runtime Data in Azure Redis Cache
Let us select the latest Redis Cache version. Click Review + create as in Figure 9-5.
Click Create as in Figure 9-6. This action will spin up the Redis Cache cluster
on Azure.
Once the Azure Redis Cache is created, navigate it and go to Access keys as in
Figure 9-7. Copy the Primary Key to use it as the cache key in the code. Provisioning
Azure Redis Cache may take about 15 minutes or more.
188
Chapter 9 Storing Runtime Data in Azure Redis Cache
Go to the Properties section as in Figure 9-8 and copy the hostname. We will use the
hostname in the code to connect to the Azure Redis Cache.
189
Chapter 9 Storing Runtime Data in Azure Redis Cache
We need to connect the Azure Redis Cache from the Java code. We need to provide
the hostname and the cache key we copied earlier. Listing 9-2 code builds a connection.
You can test if the connection is successful using the ping function as in Listing 9-3.
You will get a response as Pong if the connection is successful.
190
Chapter 9 Storing Runtime Data in Azure Redis Cache
You can use the set function to set data in the cache as in Listing 9-4. You can set the
data as key-value pairs.
You can use the get function and pass on the data key to fetch the data from the
Azure Redis Cache as in Listing 9-5.
You can choose to expire the cache data for a key using the expire function as in
Listing 9-6. You can specify the number of seconds after the cache data expires.
You can delete the data in the cache using the del function as in Listing 9-7.
// Delete Data 2
jedisCache.del("Data2");
191
Chapter 9 Storing Runtime Data in Azure Redis Cache
Listing 9-8 is the complete code to work with Azure Redis Cache.
import redis.clients.jedis.*;
// Set Data 1 Key : Data 1, Value : This is value for Data1
jedisCache.set("Data1", "This is value for Data1");
// Set Data 2 Key : Data 2, Value : This is value for Data2
jedisCache.set("Data2", "This is value for Data2");
192
Chapter 9 Storing Runtime Data in Azure Redis Cache
jedisCache.close();
}
}
Figure 9-9 shows the output for the code execution. You can use Console to verify the
data. You can use the Get command to see the data inserted in the Redis Cache. The next
section demonstrates how to use Azure Redis Cache Console from the Azure portal.
193
Chapter 9 Storing Runtime Data in Azure Redis Cache
You can insert data to the cache using the SET command. You can read data from
the cache using the GET command. You can execute any Redis command on this
console and work with the Redis Cache cluster. The use of these commands is shown in
Figure 9-11.
194
Chapter 9 Storing Runtime Data in Azure Redis Cache
Summary
In this chapter, we learned Azure Redis Cache basics and how to create an Azure
Redis Cache using the Azure portal. We then developed a Maven-based Java code and
performed read, write, expire, and delete operations on the Azure Redis Cache. In the
next chapter, we will learn how to send emails programmatically from Java applications
using Graph APIs in the Azure environment.
The following are the key takeaways from this chapter:
195
Chapter 9 Storing Runtime Data in Azure Redis Cache
• The data stored is Azure Redis Cache encrypted and highly secured.
• Azure supports both the open source Redis Cache version and the
Redis Lab Enterprise version.
• The following are pricing plans available for Azure Redis Cache:
• Basic
• Standard
• Premium
• Enterprise
• Enterprise flash
196
CHAPTER 10
Structure
In this chapter, we will discuss the following aspects of the Graph API:
197
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_10
Chapter 10 Sending Emails Using Graph API
Objectives
After studying this chapter, you should be able to get the following learnings:
• You can send and read emails, set meetings, and perform email-
related operations using Microsoft 365 Core services. You can use
Graph API endpoints to help you interact with Microsoft 365 Core
services.
• You can build applications that revolve around Azure AD, Intune,
Advanced Threat Protection, and many more using Graph API for
Enterprise Mobility and Security services.
• You can automate controlling and applying the latest patches to your
Windows server using Graph API for Windows 10 services.
• You can build applications that interact with Dynamics 365 using
Graph API for Dynamics 365 Business Central.
Figure 10-1 depicts the interaction between Microsoft services and customer
applications using Graph API.
198
Chapter 10 Sending Emails Using Graph API
Now let us explore a Microsoft 365 Core use case for Graph API. A developer needs
to set up a meeting invite with his supervisor whenever an application fails. He needs
to attach a log file from the OneDrive in the invite. This log file is generated by the
application and stored in the one-drive. All these actions should be automated. The
developer need not monitor the one-drive 24×7 and keep scheduling meetings. We
can build a solution to help the developer using Microsoft Graph API. The solution will
use Graph API to read Lead’s calendar and find a free slot available. It will then access
the developer’s mailbox and set up an invite with the Lead. The solution will use the
Graph API to access the log stored in one-drive and attach it to the invite. Figure 10-2
demonstrates the use case we discussed.
199
Chapter 10 Sending Emails Using Graph API
Figure 10-2. Microsoft Graph API and Microsoft 365 use case
Note You can get a free Microsoft 365 E5 Developer License at https://
developer.microsoft.com/en-us/microsoft-365/dev-program. You
can create your own sandbox environment that you can use for the demonstration
in this chapter.
Once you have purchased the Microsoft 365 Developer E5 license, you can follow
the following steps to configure Graph API and invoke the Graph API to send email from
your application:
200
Chapter 10 Sending Emails Using Graph API
Click Users and then click New user as shown in Figure 10-4. We can create a new
user in the tenant here.
201
Chapter 10 Sending Emails Using Graph API
202
Chapter 10 Sending Emails Using Graph API
Provide username, password details, and other necessary information for the user as
shown in Figure 10-6.
Make sure you provide the usage location for the user you are creating. Click Create
as shown in Figure 10-7. The user will get created.
203
Chapter 10 Sending Emails Using Graph API
Once the user gets created, click on the user, and this action will navigate you to the
user details section, where we can assign a license. Figure 10-8 demonstrates this action.
204
Chapter 10 Sending Emails Using Graph API
205
Chapter 10 Sending Emails Using Graph API
Select the Microsoft 365 E5 Developer license. You will get this license listed here
only if you have purchased the subscription for it. Click Save as shown in Figure 10-10.
206
Chapter 10 Sending Emails Using Graph API
Now that the user is ready, we can send a mail using the user from the Java
application. We need a service principal to authenticate with the Azure tenant and send
an email. In Azure Active Directory, click App registrations. Then click New registration
as shown in Figure 10-11.
207
Chapter 10 Sending Emails Using Graph API
Provide a name for the service principal and click Register as shown in Figure 10-12.
208
Chapter 10 Sending Emails Using Graph API
Once the service principal gets created, open the service principal and click API
permissions. Click Add a permission and add the Mail.Send permission for Microsoft
Graph as shown in Figure 10-13. Make sure that you provide the admin consent to the
permission you added.
209
Chapter 10 Sending Emails Using Graph API
Now we need to generate a secret for the service principal. Go to Certificates & secrets
and click New client secret as shown in Figure 10-14. Generate a secret and copy the
value for the secret you created. You will use this value in the Java code to authenticate
and send mail.
210
Chapter 10 Sending Emails Using Graph API
Go to the Overview page and copy Application (client) ID and Directory (tenant) ID
as shown in Figure 10-15. We will use these values in the Java code.
Figure 10-15. Copy client ID and tenant ID for the service principal
211
Chapter 10 Sending Emails Using Graph API
Now we have the user and the service principal ready. Let us create the Maven-based
Java console application to send emails.
Let us add the Microsoft Graph API and the Identity packages to the pom.xml file as
in Listing 10-1.
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph</artifactId>
<version>[5.0,)</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>[1.3,)</version>
</dependency>
Go to the main function in the Java class file as in Listing 10-2 and add the following
code. We need to create the authentication provider that will use the service principal
client secret to authenticate and authorize the code to send mail using the user we
created.
212
Chapter 10 Sending Emails Using Graph API
.build();
//Build a Token Credential Auth provider that can authenticate using the
Service Principal
final TokenCredentialAuthProvider tokenProvider =
new TokenCredentialAuthProvider(SCOPES, clientCredential);
Build the graph client to use the authentication provider we created and send mail
using the user as in Listing 10-3.
Now let us construct the mail body and subject and provide the recipient list to
whom we need to send the mail as in Listing 10-4.
213
Chapter 10 Sending Emails Using Graph API
Let us use the graph client object and send mail using the user we created earlier as
in Listing 10-5.
//Send mail. Provide the mail address of the user we created in the
Azure AD
//and have assigned Microsoft365 Developer E5 license
graph.users("[email protected]")
.sendMail(UserSendMailParameterSet
.newBuilder()
.withMessage(message)
.withSaveToSentItems(false)
.build())
.buildRequest()
.post();
The following is the complete code to send mail using Graph API from the Java
application as in Listing 10-6.
import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.models.*;
import com.microsoft.graph.requests.GraphServiceClient;
import java.util.LinkedList;
import java.util.List;
214
Chapter 10 Sending Emails Using Graph API
215
Chapter 10 Sending Emails Using Graph API
//Send mail. Provide the mail address of the user we created in the
Azure AD
//and have assigned Microsoft365 Developer E5 license
graph.users("[email protected]")
.sendMail(UserSendMailParameterSet
.newBuilder()
.withMessage(message)
.withSaveToSentItems(false)
.build())
.buildRequest()
.post();
}
}
https://developer.microsoft.com/en-us/graph/graph-explorer
Now let us go to the Microsoft Graph API Explorer and try out a popular Graph
API that will get the user profile. Log in to the Microsoft Graph API using your Azure
credentials. You will be able to see the available Graph APIs. Figure 10-16 displays a few
of the Graph APIs available.
216
Chapter 10 Sending Emails Using Graph API
Expand the Getting Started node and click my profile Graph API as shown in
Figure 10-17.
217
Chapter 10 Sending Emails Using Graph API
You will get the response for the API call as shown in Figure 10-18. You can modify
the API call here and retrigger the API.
218
Chapter 10 Sending Emails Using Graph API
Summary
In this chapter, we learned Microsoft Graph API basics and how to configure permissions
for Azure AD users to send mails using Microsoft Graph API. We then developed a
Maven-based Java code and sent mail using Microsoft Graph API. In the next chapter,
we will learn how to monitor Java applications using Azure Monitor and Application
Insights.
The following are the key takeaways from this chapter:
219
Chapter 10 Sending Emails Using Graph API
• The following are the steps to configure Graph API and invoke the
Graph API to send email from your application:
220
CHAPTER 11
Debugging and
Monitoring Using Azure
Monitor and Application
Insights
Applications may get sluggish at times, and you may need to troubleshoot performance
issues. There can be various reasons for the degradation of application performance. For
example, the CPU or memory utilization can get high while the application is processing
a long-running request. We need a mechanism to periodically monitor the application
performance metrics like CPU, memory, and disk utilization and alert the operations
team whenever the application reaches the threshold performance level. You may also
need logs to debug your application failures. Azure Monitor helps you track application
metrics, and Azure Application Insights helps you analyze the logs for your application
running on Azure or on-premises or any other public cloud.
In the previous chapter, we learned the basic concept of the Microsoft Graph APIs. We
then created a Java-based application and configured Microsoft Graph API to send mails. In
this chapter, we will learn the details of the Azure Monitor and Azure Application Insights.
Then we will build a Java application that can Azure Monitor to generate performance
metrics and Azure Application Insights that can generate logs for the application.
Structure
In this chapter, we will discuss the following aspects of the Azure Monitor and Application
Insights:
221
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_11
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Objectives
After studying this chapter, you should be able to get the following learnings:
• Using Container Insights, you can get insights into your containers.
• Using Virtual Machine Insights, you can get insights for your Virtual
Machines.
222
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
based on your analysis by creating alerts and auto-scale settings. You can integrate
with Logic Apps workflows and perform actions based on your analytics. Figure 11-1
demonstrates Azure Monitor.
Application Insights helps you monitor and gather insights for your logs and
performance metrics data for your applications running on Azure, on-premises,
or any other cloud. It can be integrated with applications developed using various
programming languages and frameworks like .NET, Java, Python, PHP, and many more.
Figure 11-2 demonstrates Application Insights.
223
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
224
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Select Subscription and expand the resource group where the Azure WebApp is
created as in Figure 11-5.
225
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
226
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Select the Metric details for the sum of CPU time as in Figure 11-7.
Let us add this metric to the Dashboard. Once you have added the metric to the
Dashboard, click New chart to create a new metric. First, let us add the existing metric
to the Dashboard and then create a new metric. Click Save to Dashboard and Pin to
Dashboard as in Figure 11-8.
227
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Go to the Create new tab and provide a dashboard name. Click Create and pin as in
Figure 11-9.
228
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Now click New chart and add a metric for the sum of the requests as in Figure 11-10.
229
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
230
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Let us add one more metric for response time by clicking on the New chart as in
Figure 11-12. Pin this to the Dashboard we created earlier.
231
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
232
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Click Edit as in Figure 11-15 and arrange the placements of the charts so that the
Dashboard looks good.
233
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
234
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Browse the WebApp URL, and you can see the metrics being generated in the
Dashboard as in Figure 11-17.
235
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
236
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Let us create an alert for the metric configured. Click New alert rule as in
Figure 11-19.
237
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
238
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Configure the alert condition. In this condition, we are configuring an alert when the
average number of containers exceeds 10. Click Done as in Figure 11-21.
239
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Go to the Actions tab and click Create action group as in Figure 11-22.
240
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Provide subscription, resource group, name, and other details. Click Next:
Notifications as in Figure 11-23.
241
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Let us notify the users using an email when the alert is raised. Select email as the
notification type as in Figure 11-24.
242
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Provide your email address to which you need to send a notification when an alert is
raised. Click OK as in Figure 11-25.
243
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Go to the Actions tab and configure an action. You can choose one of the listed
actions to execute when the alert is raised. The most popular is ITSM, where you can log
an issue or a service request in an ITSM tool whenever there is an alert. Click Review +
create as in Figure 11-26.
244
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
245
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Whenever the average number of blob containers in the Storage Account exceeds 10,
an alert will get raised. Go to the Details tab for the alert rule and provide the necessary
basic details. Click Review + create and then click Create to configure the alert as in
Figure 11-28.
246
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
247
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Provide the basic details for Application Insights. We need to keep the logs in a
Log Analytics workspace, and hence, we need to create one or use an existing one.
Application Insights will connect to this Log Analytics workspace and get the log insights.
Click Review + create and then click Create as in Figure 11-31.
248
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Once the Application Insights spins up, copy the connection string. We will use the
connection string in the Java code as in Figure 11-32.
249
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Now let us create a Maven-based Java console application. Go to the class file where
the Main method is there and add a few logging statements using java.util.logging as in
Listing 11-1.
import java.util.logging.Level;
import java.util.logging.Logger;
//https://github.com/microsoft/ApplicationInsights-Java/releases
public class AppInsightsLoggingDemo {
Create an applicationinsights.json file and keep it in the same folder where the pom.
xml file is present. We need to provide the Application Insights connection string here as
in Listing 11-2.
250
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
{
"connectionString": "Provide your connection string"
}
https://github.com/microsoft/ApplicationInsights-Java/releases
Compile the application and run the application with the JVM argument as
-javaagent:[Your Path]/applicationinsights-agent-3.2.8.jar. Replace [Your
Path] with the path on your system where this file is there.
After a few minutes, go to Application Insights and click Transaction search as in
Figure 11-33. You can see your ingested logs.
251
Chapter 11 Debugging and Monitoring Using Azure Monitor and Application Insights
Summary
In this chapter, we learned Application Insights and Azure Monitor basics. We learned
how to configure dashboard and alerts for Azure Monitor metrics. We then developed a
Maven-based Java code and pushed logs to Application Insights. In the next chapter, we
will learn how to authenticate application users using Azure Monitor.
The following are the key takeaways from this chapter:
• Azure Monitor helps you collect performance metrics and logs data
from your applications and infrastructure on Azure, on-premises, or
any other cloud platforms.
• Application Insights helps you monitor and gather insights for your
logs and performance metrics data for your applications running on
Azure, on-premises, or any other cloud.
252
CHAPTER 12
Authentication and
Authorization Using
Azure Active Directory
Applications should allow access to authenticated users. The authenticated users should
access the application modules that they are authorized for and perform the allowed
activities in the application. Azure Active Directory is an identity provider on Azure that
can be used for authentication and authorization by applications either hosted on Azure
or on-premises or on any other public or private cloud.
In the previous chapter, we learned the basic concept of Azure Monitor and
Application Insights. We then created a Java application that can use Azure Monitor to
generate performance metrics and Azure Application Insights that can generate logs for
the application. In this chapter, we will secure a Java application using Spring Security
and Azure Active Directory.
Structure
In this chapter, we will discuss the following aspects of Azure Active Directory.
253
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_12
Chapter 12 Authentication and Authorization Using Azure Active Directory
Objectives
After studying this chapter, you should be able to get the following learnings:
254
Chapter 12 Authentication and Authorization Using Azure Active Directory
You will get navigated to the Default tenant or directory. Click Users as in Figure 12-2.
We need to create a User using which we can perform authentication and authorization.
255
Chapter 12 Authentication and Authorization Using Azure Active Directory
Click New user to create a user as in Figure 12-3. We need this new user to perform
authentication and authorization on the application.
Select Create user as in Figure 12-4. We need to create a user in the default directory
or tenant and do not need a guest user.
256
Chapter 12 Authentication and Authorization Using Azure Active Directory
Provide the user’s name, password, and other necessary details. Click Create as in
Figure 12-5. A new user will get created.
257
Chapter 12 Authentication and Authorization Using Azure Active Directory
258
Chapter 12 Authentication and Authorization Using Azure Active Directory
Provide a unique name for the application and click Register as in Figure 12-8.
259
Chapter 12 Authentication and Authorization Using Azure Active Directory
Once the application gets created, copy the tenant or directory ID and the
application or client ID for the application. We will use them later in the Java application.
Let us configure a Redirect URI for the application. Click Add a Redirect URI as in
Figure 12-9.
260
Chapter 12 Authentication and Authorization Using Azure Active Directory
Click Add a platform as in Figure 12-10. We need to add a web platform as we are
dealing with a web application.
261
Chapter 12 Authentication and Authorization Using Azure Active Directory
Select Web as we are going to use this application in a Java-based API project.
Figure 12-11 demonstrates this action.
262
Chapter 12 Authentication and Authorization Using Azure Active Directory
263
Chapter 12 Authentication and Authorization Using Azure Active Directory
Now let us create an application role for the user. We will assign this role to the user
we created. Click the App roles section and then click Create app role as in Figure 12-13.
264
Chapter 12 Authentication and Authorization Using Azure Active Directory
Provide the role details. We will use the value for the role in the Java code. Click Apply
as in Figure 12-14. The role gets created in the application.
265
Chapter 12 Authentication and Authorization Using Azure Active Directory
We need to generate a secret for the application. We will use it in the Java code
along with the tenant ID and client ID for the application. Click Certificates & secrets as
in Figure 12-15. Then click New client secret. Copy the value of the secret once you have
created it. You will be able to see the value once, and this value will not be available to
you once you leave this page.
266
Chapter 12 Authentication and Authorization Using Azure Active Directory
267
Chapter 12 Authentication and Authorization Using Azure Active Directory
Search for the application we created earlier and then click on the application in the
search results as in Figure 12-17.
268
Chapter 12 Authentication and Authorization Using Azure Active Directory
Go to Users and groups. Click Add user/group as in Figure 12-18. We need to search
for the user and do a role assignment.
Let us add the user against the role we created. Click None Selected as in
Figure 12-19.
269
Chapter 12 Authentication and Authorization Using Azure Active Directory
Click Assign as in Figure 12-21 to complete the role assignment for the user.
270
Chapter 12 Authentication and Authorization Using Azure Active Directory
271
Chapter 12 Authentication and Authorization Using Azure Active Directory
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.
apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.azure.azuread</groupId>
<artifactId>authdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>authdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<azure.version>3.14.0</azure.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-starter-active-directory</
artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
272
Chapter 12 Authentication and Authorization Using Azure Active Directory
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-bom</artifactId>
<version>${azure.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Go to the class file having the Main method. Convert the class as REST Controller as
in Listing 12-3. Add a REST method as SuperUser. In the PreAuthorize attribute, provide
the Role value in the format APPROLE_{RoleValue} where RoleValue is the value of the
Role you create in the Azure Active Directory application.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.method.configuration.
EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.
EnableWebSecurity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.access.prepost.PreAuthorize;
@SpringBootApplication
@RestController
public class AuthedApplication {
@GetMapping("SuperUser")
@ResponseBody
@PreAuthorize("hasAuthority('APPROLE_superuser')")
public String SuperUser() {
return "This is a Super User !! You have all kinds of Access in the
WORLD !!";
}
274
Chapter 12 Authentication and Authorization Using Azure Active Directory
Build and run the application. Browse the URL as in Listing 12-4 once that
application starts up.
http://localhost:8080/SuperUser
You will get prompted to provide the sign-in credentials as in Figure 12-22. You need
to provide the credentials for the user we created. For all other users, the authorization
will not be successful, and you will get Forbidden response.
Once you have provided the correct credentials, you get the following output
rendered in the browser as in Figure 12-23.
275
Chapter 12 Authentication and Authorization Using Azure Active Directory
277
Chapter 12 Authentication and Authorization Using Azure Active Directory
In this scenario, the Web API does not get the user details in the JWT token as the
Web Application is driving the authentication. Delegated User Identity helps the Web
Application to authenticate as the user, and the JWT will have user details that can be
used by the Web API.
Summary
In this chapter, we learned Azure Active Directory basics. We learned how to create a
user and register an application in Azure Active Directory. We then developed a Maven-
based Java code and used the application in Azure Active Directory. In the next chapter,
we will learn how to provision Azure resources using Azure DevOps and Azure CLI.
The following are the key takeaways from this chapter:
278
PART III
Provisioning Resources
with Azure DevOps
and Azure CLI
Infrastructure as Code (IaC) is a widely adopted practice in a cloud-based enterprise.
You automate the creation of cloud infrastructure using scripts and DevOps tools.
You need to create these scripts or solutions once and reuse them across multiple
environments. This approach saves a lot of time and effort for you. There are many
Infrastructure-as-Code (IaC) utilities and tools available that will help you automate
provisioning the Azure infrastructure.
In the previous chapter, we learned the basic concept of Azure Active Directory.
We then created a Java application that can use Azure Active Directory to perform user
authentication and authorization. In this chapter, we will explore the Infrastructure-as-
Code (IaC) options for Azure and then spin up an Azure WebApp using Azure CLI and
Azure DevOps.
Structure
In this chapter, we will discuss the following aspects of infrastructure creation using
Azure DevOps and Azure CLI:
281
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_13
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Objectives
After studying this chapter, you should be able to get the following learnings:
• Azure PowerShell
• ARM Templates
• Terraform
• Bicep
282
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Azure CLI is a cross-platform offering where you can spin up Azure resources
either from Windows, Linux, or Mac systems. You can run the Azure CLI scripts from a
command prompt, bash prompt, or PowerShell prompt.
Azure PowerShell offers you PowerShell modules that will help you spin up Azure
resources. If you use Azure PowerShell Core that can run on PowerShell 6 and above, you
can build cross-platform scripts.
Using ARM Templates, you can represent the Azure infrastructure as a JSON
template and then deploy the template to the Azure environment and spin up your
Azure resources. Both Azure CLI and Azure PowerShell expose commands that you can
use to deploy these templates.
Azure PowerShell, Azure CLI, and ARM Templates are Microsoft offerings to create
Azure resources. Terraform is a third-party offering that would help you script Azure
resources creation.
You can use Azure DevOps pipelines to invoke the scripts developed using Azure
CLI, PowerShell, Terraform, or ARM Templates to spin up Azure infrastructure. In
this case, you can build release pipelines to spin up infrastructure across various
environments. This approach will help you build single-click automation that will spin
up Azure infrastructure and deploy an application.
https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
You will get prompted in your browser to provide your credentials as in Figure 13-1.
283
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Once you have logged in to Azure, you need to select a subscription where you
need to create your resources. Execute the command as in Listing 13-3 to select your
subscription. Replace {subscription} with your subscription name or subscription
ID. You can use the az account show command to list out all the subscriptions in the
chapter.
You need to create a resource group inside which you can create a WebApp. You may
choose to use an existing resource group. In that case, you can ignore this step. Replace
{name} with the resource group name. Here, we are creating the resource group in the
eastus location. You may choose to have an Azure location matching your need. Listing 13-4
demonstrates the command to create a resource group.
284
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
We need an App Service Plan for our WebApp. Execute the command as in Listing 13-5
to explore how to use the create command for the App Service Plan. You will also learn
about the various App Service Plan SKUs available here and other details.
Execute the command as in Listing 13-6 to create an App Service Plan in the region
eastus and with standard SKU (S1). Replace {resourcegroup-name} with the name of the
resource group and {name} with the name of the App Service Plan.
Execute the command as in Listing 13-7 to create a WebApp. Replace {name} with
the name of the WebApp, {resourcegroup-name} with the name of the resource group,
and {plan-name} with the name of the App Service Plan.
Your WebApp gets created in the resource group provided. You can use
the command curl -s -o /dev/null -w "%{http_code}" https://{name}.
azurewebsites.net/ where {name} is the name of the WebApp. You should get response
code as 200.
285
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Once you have logged in to Azure DevOps, we can create a new DevOps project.
Click New project as in Figure 13-2.
Provide the project’s name, mark it as a private project, and click Create as in
Figure 13-3.
286
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Hover on Pipelines as in Figure 13-4. This action will list out the menu items for the
Pipelines.
287
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Click Releases as in Figure 13-5. You will get navigated to the release pipelines.
288
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Let us create a new release pipeline. Click New pipeline as in Figure 13-6.
You will get prompted to select a job template. Select Empty job as in Figure 13-7.
289
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
We need to add the Azure CLI task to the Stage. Click 1 job, 0 task as in Figure 13-8.
We need to add the Azure CLI task. Click on the + icon as in Figure 13-9.
290
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Search for Azure CLI and select Azure CLI as in Figure 13-10.
291
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Click Add to include this task in the agent job as in Figure 13-11.
292
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Click the Azure CLI task as in Figure 13-12. We need to configure this task to
create WebApp.
293
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Click Authorize as in Figure 13-14. This action will authorize the pipeline to connect
to the Azure environment and create the WebApp.
294
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
295
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
You can provide the command to create a resource group, an App Service Plan, and
the WebApp. Select PowerShell as Script Type and Inline script as Script Location as in
Figure 13-16. We need not log in to the Azure portal and select a subscription.
296
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Click Save as in Figure 13-18. This action will save the pipeline.
297
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
298
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
299
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
300
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
Click Deploy to start creating the WebApp as in Figure 13-23. Once the pipeline
completes successfully, the WebApp gets created.
301
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
For verification, you can use the command curl -s -o /dev/null -w "%{http_
code}" https://mywebappiacdemo01.azurewebsites.net/ and check for the status
code as 200.
Summary
This chapter taught us how to create Azure resources using Azure CLI and Azure
DevOps. We created a resource group, an App Service Plan, and an Azure WebApp using
Azure CLI and Azure DevOps. In the next chapter, we will learn how to deploy Java
applications to Azure using Azure DevOps.
The following are the key takeaways from this chapter:
302
Chapter 13 Provisioning Resources with Azure DevOps and Azure CLI
• Azure CLI
• Azure PowerShell
• ARM Templates
• Terraform
• Bicep
303
CHAPTER 14
Structure
In this chapter, we will discuss the following DevOps-based application deployment
concepts:
Objectives
After studying this chapter, you should be able to get the following learnings:
305
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_14
Chapter 14 Building and Deploying Using Azure DevOps
• Learn how to build and deploy Java applications to Azure using Azure
DevOps Pipeline
306
Chapter 14 Building and Deploying Using Azure DevOps
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
You can generate a class file with the Main method and a REST API. Listing 14-2
shows the content of the class file.
package com.iac.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
307
Chapter 14 Building and Deploying Using Azure DevOps
@GetMapping("/")
public String index() {
return "Hello Spring Boot on WebApp !!";
}
Once the application builds successfully, you can check in the code to the Azure
Repos. As a prerequisite, you should have Git installed on your system. We can use the
Azure DevOps project we created in the previous chapter. Open the Azure DevOps URL
and navigate to the project. Click Repos and then click Generate Git Credentials as in
Figure 14-1.
Copy the HTTPS URL, Git username, and password as in Figure 14-2.
308
Chapter 14 Building and Deploying Using Azure DevOps
Navigate to the Java project root folder in the Windows Explorer with the POM file.
Execute the command from Listing 14-3 to initiate a local Git repository.
git init
Execute the command as in Listing 14-4 to add the folder content to the local Git
repository.
git add .
Execute the command as in Listing 14-5 to commit the added files in the local Git
repository.
Execute the commands as in Listing 14-6 to connect to the Git repository in Azure
DevOps and push the local Git changes to it. Replace [HTTPS_URL] with the URL you
copied earlier while generating the Git credentials.
309
Chapter 14 Building and Deploying Using Azure DevOps
You will get prompted to provide credentials for Git. You can provide the username
and password you copied while generating Git credentials. Ensure you provide the
username and the domain—for example, [email protected]. The Java project
will get checked into the Azure DevOps Repo as in Figure 14-3.
310
Chapter 14 Building and Deploying Using Azure DevOps
311
Chapter 14 Building and Deploying Using Azure DevOps
You will be asked to provide the source code repository details where the Java code
is checked in. We have checked in to Azure Repos Git. So select Azure Repos Git as in
Figure 14-6.
Select the repository where you have checked in the code as in Figure 14-7.
312
Chapter 14 Building and Deploying Using Azure DevOps
You can select the template Maven package Java project Web App to Linux on Azure
as in Figure 14-8. If you do not see this template listed, click Show More and search for it.
Select the Azure subscription where you have created the Azure WebApp. Click
Continue as in Figure 14-9.
313
Chapter 14 Building and Deploying Using Azure DevOps
You will be prompted to provide your login credentials as in Figure 14-10. Provide
your credentials to log in to the system.
Select the name of the WebApp where you are planning to deploy the Java code.
Click Validate and configure as in Figure 14-11. You need to wait for some time for the
314
Chapter 14 Building and Deploying Using Azure DevOps
WebApp to get reflected here in the Azure DevOps environment after creating it. At
times, it may take a few hours.
The YAML pipeline will get generated for you. Listing 14-7 demonstrates the YAML
pipeline content.
trigger:
- master
variables:
315
Chapter 14 Building and Deploying Using Azure DevOps
stages:
- stage: Build
displayName: Build stage
jobs:
- job: MavenPackageAndPublishArtifacts
displayName: Maven Package and Publish Artifacts
pool:
vmImage: $(vmImageName)
steps:
- task: Maven@3
displayName: 'Maven Package'
inputs:
mavenPomFile: 'pom.xml'
- stage: Deploy
displayName: Deploy stage
316
Chapter 14 Building and Deploying Using Azure DevOps
dependsOn: Build
condition: succeeded()
jobs:
- deployment: DeployLinuxWebApp
displayName: Deploy Linux Web App
environment: $(environmentName)
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: AzureWebApp@1
displayName: 'Azure Web App Deploy: webapp-iacdemo-linux'
inputs:
azureSubscription: $(azureSubscription)
appType: webAppLinux
appName: $(webAppName)
package: '$(Pipeline.Workspace)/drop/**/target/*.?(war|jar)'
Click Save and run as in Figure 14-12. This will save the pipeline changes and
navigate you to the next screen where you can execute the pipeline.
317
Chapter 14 Building and Deploying Using Azure DevOps
Once the build stage completes, the pipeline will ask you for permissions to deploy
to Azure WebApp. Click View as in Figure 14-14 and provide permissions.
318
Chapter 14 Building and Deploying Using Azure DevOps
Once the pipeline completes, the Java application gets deployed to Azure
WebApp. You can browse the WebApp URL and see the response on the browser as in
Figure 14-15.
Summary
This chapter showed how to build and deploy a Java Spring Boot application to Azure
WebApp using Azure DevOps. We used Git commands to check in the Java code to Azure
Repos. We then created a YAML-based pipeline to build and deploy the Java application
from Azure Repos to Azure WebApp. In the next chapter, we will learn the best cases and
explore a real-time Java application on Azure.
The following are the key takeaways from this chapter:
• Azure Repos provides a Git repository, and you can execute Git
commands to check in to Azure Repos.
• You can also create a TFS-based code repository using Azure DevOps.
• You can create a YAML-based pipeline to build Java code and deploy
it to Azure.
319
CHAPTER 15
A Near-Production
Azure-Based Java
Application
Now we have come to the end of the book. We have learned the basics of the essential
Azure services that will come in handy while working with Java-based applications on
Azure. We learned how to deploy Java-based applications on Azure services like Azure
WebApp, Azure Kubernetes Service, Azure Spring Cloud, and Azure Functions. We
also learned how to work with Azure SQL, Azure Storage, and Azure Redis Cache. We
explored how to monitor, secure, and use Azure DevOps for automated infrastructure
creation.
We learned how to build and deploy Java applications to Azure services using Azure
DevOps in the last chapter. In this chapter, we will explore the best practices to be
followed while designing and implementing a Java-based application on Azure. We will
also work on a near-production scenario.
Structure
In this chapter, we will discuss the following:
• Best practices
Objectives
After studying this chapter, you should be able to get the following learnings:
321
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9_15
Chapter 15 A Near-Production Azure-Based Java Application
Best Practices
Designing an application architecture is a crucial activity. You must consider the best
practices while implementing the application architecture. Applications targeted for
Azure should be planned with very high precision so that the application performs as
expected in the Azure environment. Let us discuss a few essential best practices that you
should consider while designing a Java-based application for Azure.
322
Chapter 15 A Near-Production Azure-Based Java Application
323
Chapter 15 A Near-Production Azure-Based Java Application
324
Chapter 15 A Near-Production Azure-Based Java Application
Problem Statement
We have a student table in the Azure SQL database. The table contains the roll number,
name, and marks of the students in a class. Let us build a Spring Boot web application
to get the student records from the student table and render it in the browser. The
application should be available to authenticated users only. Make sure that you enable
logging for the application so that you can debug the application when it is deployed on
Azure WebApp.
325
Chapter 15 A Near-Production Azure-Based Java Application
Let us create each of these services first before building the Java application. We can
start with creating Azure WebApp and Application Insights. Azure WebApp will host the
application, and Application Insights will be used for logging and monitoring purposes.
The application will ingest runtime logs into Application Insights that can be used later
for debugging purposes.
Go to the Azure portal and click Create a resource as in Figure 15-1. You will get
navigated to Azure Marketplace.
Click Web in the Marketplace and then click Web App as in Figure 15-2
326
Chapter 15 A Near-Production Azure-Based Java Application
Provide subscription, resource group, and name for the WebApp as in Figure 15-3.
327
Chapter 15 A Near-Production Azure-Based Java Application
Scroll down and provide other necessary basic details for the Web App as in
Figure 15-4.
328
Chapter 15 A Near-Production Azure-Based Java Application
Go to the Monitoring tab and enable Application Insights. You need to select an
existing Application Insights or create a new one. Click Review + create as in Figure 15-5
329
Chapter 15 A Near-Production Azure-Based Java Application
Click Create as in Figure 15-6. Your Web App and Application Insights will get
created. The Application Insights will get associated with the Web App, and you can
ingest application logs to the Application Insights without making any other changes in
the Web App.
330
Chapter 15 A Near-Production Azure-Based Java Application
Now let us spin up the Azure SQL database, create the student table and insert some
student data. Go to the Azure portal and click Create a resource as in Figure 15-7.
331
Chapter 15 A Near-Production Azure-Based Java Application
Provide basic details for the Azure SQL database. Click Next : Networking as in
Figure 15-9.
332
Chapter 15 A Near-Production Azure-Based Java Application
Select the access to the database as a Public endpoint as in Figure 15-10. Modify
firewall rules for the database to allow other Azure services to access it, and also, you
should be able to access the database from your local system. Click Review + create. And
then click Create.
333
Chapter 15 A Near-Production Azure-Based Java Application
Once the database gets created, go to the Query editor as in Figure 15-11, provide the
database credentials, and log in to the database.
334
Chapter 15 A Near-Production Azure-Based Java Application
Run the SQL query shown in Listing 15-1 to create the student table.
335
Chapter 15 A Near-Production Azure-Based Java Application
We can follow the same steps as Chapter 12 to create a user in Azure Active Directory
and register an application. While registering the application in Azure Active Directory,
make sure you provide the following URL as in Listing 15-3 as Redirect URI. Replace
{WebApp} with the name of the WebApp we created.
https://{WebApp}.azurewebsites.net/login/oauth2/code/
336
Chapter 15 A Near-Production Azure-Based Java Application
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
337
Chapter 15 A Near-Production Azure-Based Java Application
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-spring-boot-starter</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-starter-active-directory
</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-bom</artifactId>
<version>${azure.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
338
Chapter 15 A Near-Production Azure-Based Java Application
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Listing 15-5 is the application.properties file. Here in this file, we need to provide the
database connection details, Application Insights instrumentation key, and Azure Active
Directory details.
logging.level.org.springframework.jdbc.core=DEBUG
spring.sql.init.mode=always
# Specifies App Insights Instrumentation Key:
azure.application-insights.instrumentation-key={Provide
Instrumentation Key}
spring.application.name=StudentApp
# Specifies your Active Directory ID:
azure.activedirectory.tenant-id={Provide Tenant ID}
# Specifies your App Registration's Application ID:
azure.activedirectory.client-id={Provide Client ID}
# Specifies your App Registration's secret key:
azure.activedirectory.client-secret={Provide Client Secret}
339
Chapter 15 A Near-Production Azure-Based Java Application
Let us create a Student class as in Listing 15-6. The Student class works as a domain
model for the student database table.
package com.database.demo;
import org.springframework.data.annotation.Id;
public Student(){
}
@Id
private int roll;
340
Chapter 15 A Near-Production Azure-Based Java Application
Now let us add a StudentRepository interface as in Listing 15-7 in the same folder
where we have the Student class and the DemoApplication class. The StudentRepository
interface works as the repository class for the database and the domain model. The
Spring Data JDBC manages this repository class.
package com.database.demo;
import org.springframework.data.repository.CrudRepository;
public interface StudentRepository extends CrudRepository<Student,
Integer> {
}
341
Chapter 15 A Near-Production Azure-Based Java Application
Create the DemoApplication Java class as in Listing 15-8 and convert it into a REST
Controller. In the REST GET API, we access the Azure SQL database and return an HTML
response with the student details. Authentication is enabled, and we are logging traces
and exceptions to Application Insights.
package com.database.demo;
import com.microsoft.applicationinsights.TelemetryClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
@RestController
@RequestMapping("/")
public class DemoApplication {
@GetMapping("/")
@ResponseBody
@PreAuthorize("hasAuthority('APPROLE_superuser')")
342
Chapter 15 A Near-Production Azure-Based Java Application
try{
for (Student student: students) {
html = html+"<p>";
html = html+"<b>Roll : </b>" + student.getRoll()+"<br/>";
html = html+"<b>Name : </b>" + student.getName()+"<br/>";
html = html+"<b>Age : </b>" + student.getAge()+"<br/>";
html = html+"<b>Marks : </b>" + student.getMarks()+"<br/>";
return html;
}
343
Chapter 15 A Near-Production Azure-Based Java Application
Click Azure and then click Deploy to Azure Web Apps as in Figure 15-15.
344
Chapter 15 A Near-Production Azure-Based Java Application
Select Azure CLI and click Sign in as in Figure 15-16. Provide your credentials when
prompted.
345
Chapter 15 A Near-Production Azure-Based Java Application
Once the deployment is complete, wait for ten minutes for the application to boot
up in the WebApp. Navigate the WebApp URL after ten minutes. You will be prompted to
provide login credentials as in Figure 15-19. Use the user’s credentials you created and
authorized in the registered application in Azure Active Directory.
You can see the student details rendered on the browser as in Figure 15-20.
347
Chapter 15 A Near-Production Azure-Based Java Application
Summary
In this chapter, we used the concepts we learned in the book to design and develop
a complex use case. We explored the IntelliJ Azure plug-in to build and deploy Java
applications to Azure WebApp. We applied multiple concepts like reading from Azure
SQL, authenticating using Azure AD, monitoring applications using Application Insights,
and deploying to Azure WebApp in a single Spring Boot application.
349
Index
A Spring Boot application,
271, 273, 275, 276
Application Insights, 223, 247
user creation, 255–257
basic details, 249
authentication scenarios, 276
connection string, 250
native application, 277
creation, 248
Server application, 278
.json file, 250
Single Page Application, 277
JVM argument, 251
Web Application, 277
logs, 251
Web Browser to Web
search result, 247
Application, 276
Application.properties file, 151, 339
Azure Container Registry, 67, 71, 73, 79,
App Service Plan
85–88, 91, 92
dedicated compute, 17
Azure Cosmos DB, 161–163, 167, 171
isolated plan, 18
complete code, 173
Java application
concept, 161
Azure WebApp, 25–29
database, 162
Azure WebApp, deploy,
Java application, 161
29–31, 33, 34
Java code, 168
Java Spring Boot, 18–24
location, 166
scaling, 34–37
NoSQL database, 179
Shared Compute, 17
operations, 161
Azure, 10
POM file, 168
Java-based applications, 12
student database, 178
pricing model, 11
ways, 162
services, 11
Azure DevOps, 286
WebApp, 12
login URL, 286
Azure Active Directory, 207, 254
project creation, 286
authentication and
Authorize connection, 295
authorization, 255
CLI task, 290, 293, 296
enterprise applications, 267, 268,
deploy release, 301
270, 271
job template, 289
register an application, 258, 260,
login credentials, 295
263, 267
351
© Abhishek Mishra 2022
A. Mishra, Microsoft Azure for Java Developers, https://doi.org/10.1007/978-1-4842-8251-9
INDEX
352
INDEX
C DevOps solutions, 16
Docker file, 71, 83, 84
Cloud computing, 4
Docker images, 84
advantages, 8, 9
Dynamics 365 services, 198, 220
deployment models, 7
Cloud service types, 5
Cloud vendor, 5–9, 14
E
Code execution output, 193
e-commerce application, 197
Command line interface (CLI), 283
App Service Plan, 285
az account show command, 284
F
installation URL, 283
Function-as-a-Service (FaaS), 40, 63
log in to Azure, 283
Function().run method, 55
resource group, 284
subscription, 284
WebApp. Replace, 285
G
Consumption Plan, 43, 47, 64
Geo-Redundant Storage (GRS), 117
Container registry, 67, 70
getBlobClient function, 123, 124
Containers, 65, 67
getBlobContainerClient function, 122
create container registry, 71–74
Git-Based Azure Repos, 306
create Kubernetes Service, 75–80
added project, 310
docker, 85, 86
class file, 307
image, 84
credentials, 308
Java application, 81, 83
POM file, 306
Kubernetes, 69, 70
Git credentials, 308–310
Kubernetes cluster, 80
Graph API, 195, 197, 198
run Kubernetes Service,
Graph client object, 213, 214
87, 88, 90, 91
Gremlin API stores data, 162
Content-Type header, 152, 153
Cosmos DB, 168, 169, 177
createBlobContainer function, 122
H
Hosting plans
D Consumption Plan, 43
Database transaction, 138 Dedicated Plan, 43
Data replication, 116, 161, 179 Environment Plan, 44
Dedicated Compute, 17, 38 Kubernetes Plan, 44
Dedicated Plan, 43, 44, 64 Premium Plan, 43
DemoApplication class, 151, 152, 341, 342 Hosting software, 6
353
INDEX
I K
Infrastructure as a Service (IaaS), 5–7, Kubernetes, 11, 13, 14, 40, 44, 63, 69
10, 13, 14
Infrastructure-as-Code (IaC), 16, 282, 325
ARM templates, 283 L
Azure CLI, 283 listBlobContainers function, 122
Azure PowerShell, 283 Locally Redundant Storage (LRS), 116
Terraform, 283
Infrastructure creation, 326
Application Insights, 330 M, N
basic details, 328 Maven-based Java code, 121, 127,
firewall rules, 334 179, 195
query editor, 335 Maven-based Java console application,
resource creation, 326, 332 121, 127, 212, 250
SQL Database, 332 Maven execution, 52
Web App, 327 Maven project, 148
Isolated plan, 18 Microsoft Graph API, 198, 209, 216,
219, 220
Microsoft Graph API Explorer, 216, 217
J Microsoft SQL Server engine, 138
Java application, 138, 198, 207, 214, 220 Microsoft 365 Core, 198, 199, 220
Java-based application, 197, 322 Microsoft 365 Core services,
authentication/authorization, 324 197, 198, 220
auto-scaling mechanisms, 324 Microsoft 365 E5 Developer license,
Azure services, 323 200, 206
cloud cost meeting, 323 Microsoft 365 E5 license, 200, 220
debugging and monitoring Modern applications, 3, 39, 40, 161, 181,
strategy, 324 197, 323
disaster recovery strategy, 325
identify the host options, 322
production environment (see O
Infrastructure creation) Open Container Initiative (OCI), 71
using DevOps, 325
Java code, 136, 168, 179, 184, 211
Java Maven project, 168 P, Q
Java Runtime Environment (JRE), 5 Ping Redis Cache, 191
Java Spring Boot application, 18–24, 139 Platform-as-a-Service, 10, 12, 14, 16, 37,
Java Spring Boot project, 148 40, 93, 94, 116, 136, 137, 195, 254
354
INDEX
T, U
R Table Storage, 46, 47, 130
Read Access Geo-Redundant Storage Triggers, 44
(RA-GRS), 117 Runtime 1.x, 45
receiveMessages function, 128 Runtime 2.x, 45
Redis Cache, 179, 181–186
Redis Cache cluster, 188, 194
Redis Cache version, 183, 187, 196 V
Redis Console, 193–195 Virtual Machines, 4, 66–69, 222, 254, 322
Right Graph API, 216 Visual Studio Code, 13, 18, 21,
29, 51, 343
S
Scale-out WebApp, 37 W, X
Scale-up WebApp, 34 WebApp, 11, 12, 14–17, 30, 34, 39, 41, 43,
Serverless architecture, 40 182, 224, 226, 261, 277, 278, 281,
Shared Compute, 17, 38 283, 285, 286, 294–296, 301, 302,
Software as a Service, 6, 7, 10, 14 305, 313
Spring Boot application, 271 Windows 10 services, 198, 220
API response, 276
application.properties file, 273
Forbidden response, 275 Y
Main method, 274 YAML-based pipeline, 310
pom.xml file, 271 Azure Repos Git, 312
Spring Boot Java application, 82 browser response, 319
SQL query editor, 147 code implementation, 315
355
INDEX
356