Microsoft Azure provides fully managed Cloud SQL databases for use by your Azure hosted cloud services. Payara® Micro is built to be the best runtime for Cloud Native Java EE and MicroProfile applications. Here’s how to rapidly create a REST web service that retrieves data from an Azure SQL Database and returns it as JSON.
Creating the Database
In this blog we will use the example Adventure Works database which provides us with an example schema already populated with data. Throughout the example you will need to replace the names of resource groups and servers etc. with your choice.
To create a new database server use:
After the database is created we need to create a firewall rule to allow Azure services access to the database:
Note this will allow access from any service running on Azure so ensure your database is secured.
Then to create the example database using the Adventure Works sample schema:
With the database created we can now create the Java EE application using REST.
You can clone the example application using:
This will build the application into the target directory.
The application consists of a Simple REST web service which uses an EJB Facade to retrieve data from the SQL database using JPA.
The datasource to access the Azure SQL Database is defined in the web.xml
The persistence.xml file references the JNDI name of the datasource:
Finally, the ProductResource class utilises the EJB to expose Product data via REST for example the method below retrieves all products and returns them as JSON;
In this example we will package the application as a Docker Image and run it directly on Azure using Azure Container Instances. In this way we don’t have to worry about creating and configuring Virtual Machines. Azure Container Instances gives us the ability to run a Docker Image directly on Azure.
We will assume that you have an Azure Container Registry created in your account.
The example application contains two docker files one for a base Payara Micro image which you can use for other applications and the second that builds on that image to add the application.
To build the base image from the directory containing the DockerFile we can use Azure’s container build capability.
Then you can build the application image. Note you will have to edit the Dockerfile and replace the registry name in the line:
with the name of your registry then build the image.
You should now have at least 2 images in your docker registry.
Running the Application on Azure
Now that the images are available in the registry we can run the docker image directly on Azure. The key point is that we need to pass environment variables to the image that replace those contained in the data source definition to ensure they refer to the database created in the first step.
For example the command below will create and run the docker image on Azure:
Note the environment variables must correspond to the data entered when creating the SQL Database.
Testing the Application
Once your container is successfully running you should be able to navigate to the URL http://your-payara-azure-sql-example.westeurope.azurecontainer.io/AzureSQLExample/adventureworks/product and see the list of products as JSON data.
Payara Micro Cloud Native Capabilities Run Applications on Microsoft Azure
With the built-in Cloud Native capabilities of Payara Micro, it’s simple to package and run Java EE and MicroProfile applications on Microsoft Azure. Payara Micro can easily be packaged into a Docker Image with a data source to access Azure SQL databases which can be reconfigured easily to suit your cloud deployments.