Click here to Skip to main content
15,884,672 members
Articles / Hosted Services / Azure
Article

Modernizing Java Apps and Data on Azure Part One: Introduction

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
24 Feb 2022CPOL6 min read 3.2K   1  
How to set up the project and prepare the database
This is Part 1 of a 6-part series that demonstrates how to take a monolithic Java application and gradually modernize both the application and its data using Azure tools and services. This article shows how to modernize legacy Java apps and their data on Azure.

This article is a sponsored article. Articles such as these are intended to provide you with information on products and services that we consider useful and of value to developers

Businesses often must refocus their resources when applications run on servers that don’t match their needs. Many companies have a monolithic Java application that needs a new home, but they need to know how to tackle this challenge. This series is here to help.

Using the Spring Boot PetClinic sample application as a Java monolith example, this series shows how to rehome an application on Microsoft’s Azure platform to gain the benefits of being in the cloud. The following six articles show how to rearchitect it into a proper cloud-native application.

Migrating a monolithic application to a set of cloud-native services is a journey, but you can do it as a sequence of smaller tasks.

Series Overview

The first step, covered in the next article, is to migrate the data from a local database to a cloud data service. This approach limits the type of problems to access issues when connecting an application to the data. Because application development remains on the same platform and process, it’s possible to adjust the application quickly. This approach also allows populating the database with test data and validating access controls before putting actual customer data at risk.

After connecting the application to the new data services, the third part of this series moves the application to cloud computing services. Application security is not part of this series. While Azure provides services for handling security, this series only demonstrates how to provide basic security to ensure the data is not publicly available, and it provides some references for further study.

The final three parts of this series cover containerizing the application, then migrating to Azure Database for PostgreSQL using the Hyperscale (Citus) deployment option to handle large-scale applications. The series wraps up by migrating part of the application to Azure Functions to demonstrate the pathway to being fully cloud-native.

First, this article will set up the project and prepare the database.

Project Overview

The Spring Boot PetClinic application is a complete monolithic application using an embedded web server. It also supports both MySQL and Postgres databases. This tutorial uses a local Postgres database, and Azure’s Database for PostgreSQL is its drop-in replacement.

The only change you’ll need to make to the application is the data server’s hostname. Azure’s App Service provides reliable computing services that are easy to set up, and they can run this application without any changes.

Azure’s computing and data services simplify setting up the hardware and network so that you can focus on development. The Azure platform keeps security risks during development to a minimum. By default, an Azure Database for PostgreSQL server is secure because it’s not publicly accessible. Still, there are readily available options to configure access from development machines and other Azure services.

Setting up specific firewall rules or VLAN access is straightforward when you're ready to deploy a production service. The Azure App Service provides a configured load balancer and SSL certificates to limit application access to qualified users.

Setting Up the Project

The following are the prerequisites for setting up Spring Boot’s PetClinic application:

  • Java 11
  • Git
  • IntelliJ (or IDE of your choice)
  • Maven Helper
  • Postgres running on a local server

PetClinic supports Java 8, 11, and 17. This article uses Java 11 and the Git client to clone the repository. It also uses the IntelliJ IDE for the development environment, but you could also use Eclipse or any other IDE. The application supports Maven and Gradle, though this article works with Maven and the Maven Helper installed with IntelliJ.

Preparing the Database

PetClinic provides a schema and initial data. Spring Boot uses Spring’s implementation of the Java Persistence API (JPA) to create a database and populate it (if needed) when the application starts. The application uses this for implementation, but it needs a database user and an empty database owned by that user so that PetClinic can connect to the server. There are many ways to do this. This article uses the Postgres interactive command-line client.

The following psql command sets up the petclinic user with the required password. The host, user ID, and database values below vary for each environment.

C:\Projects\PetClinic\postgresDb>psql -h localhost -U Admin -d postgres
Password for user Admin:
psql (14.1)
WARNING: Console code page (437) differs from Windows code page (1252)
       8-bit characters might not work correctly. See psql reference
       page "Notes for Windows users" for details.
Type "help" for help.

postgres=# create user petclinic password 'petclinic';
CREATE ROLE

The next step is to create the empty database and assign ownership to the petclinic user:

postgres=# create database petclinic with owner petclinic;
CREATE DATABASE
postgres=# \q

Cloning the Repository

Now it’s time to set up the development environment using the Git command-line client to clone the repository.

git clone https://github.com/spring-projects/spring-petclinic.git spring-petclinic

Setting Up the Project in IntelliJ

To set up the project in IntelliJ, click Open and select the Projects directory.

Image 1

When IntelliJ opens the project, it sees both the Maven and Gradle configurations. Select Maven project.

Image 2

IntelliJ opens the project and displays the readme.md file. To ensure that the complete sources and folders exist, right-click the project name and select Maven and Generate Sources and Update Folders.

Image 3

To run the application, create a Run configuration. Click Run > Edit Configuration to start the process.

Image 4

The project doesn’t have any configurations, so you must create a new one, but you also need to make sure it’s a Maven configuration.

Image 5

There are two groups of settings to change. Add the goals to the Run command and add the Postgres profile to the environment variables, like below:

  • Run: spring-javaformat:apply spring-boot:run
  • JRE: Project SDK 11
  • Environment Variables: spring.profiles.active=postgres

Image 6

Image 7

Image 8

Click OK to save the changes, then run the application using the configuration you just created.

Image 9

IntelliJ builds the application, creates, and initializes the database, then restarts the application. You’ll see something like the following screenshot when the process is successful. The last line will say, “Started....”

Image 10

You can verify that the application is running by opening http://localhost:8080 in a browser.

Image 11

One challenge with the Postgres profile is that it doesn’t update the primary key sequences after inserting the sample data. The application throws exceptions if you try to add a pet or a visit. To fix this, you must resequence the database.

Resequencing the Database

Create a text file named resetSeqeunce.pg with the following SQL statements:

SQL
SELECT setval('owners_id_seq', max(id)) FROM owners;
SELECT setval('pets_id_seq', max(id)) FROM pets;
SELECT setval('specialties_id_seq', max(id)) FROM specialties;
SELECT setval('types_id_seq', max(id)) FROM types;
SELECT setval('vets_id_seq', max(id)) FROM vets;
SELECT setval('visits_id_seq', max(id)) FROM visits;

Execute these statements using the following psql command. You should see the setval results as the commands execute. The user id appears below because you need it to update the existing database. The password for the database is petclininc.

C:\Projects\PetClinic\postgresDb> psql -h localhost -U petclinic -d petclinic -f resetSequence.pg
Password for user petclinic:

setval
--------
   10
(1 row)
setval
--------
   13
(1 row)
setval
--------
    3
(1 row)
setval
--------
    6
(1 row)
setval
--------
    6
(1 row)
setval
--------
    4
(1 row)
C:\Projects\PetClinic\postgresDb>

Next Steps

That’s it! PetClinic is up, running, and ready to go. You also have most of the tools you’ll need to migrate the data and application to the Azure platform. In article three of this series, you’ll add the Azure Toolkit for IntelliJ.

For the second article of this series, you’ll need an Azure account. If you don’t have one already, create a free Azure account.

And if you want a preview of what the following two articles cover, check out these training modules:

Continue to the following article to learn more about migrating data.

If you'd like to discuss a specific Java app migration scenario with the Microsoft Java on Azure team, please fill out this questionnaire and a representative will contact you.

This article is part of the series 'Modernizing Java Apps and Data on Azure View All

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --
Modernizing Java Apps and Data on Azure