loading...
Microsoft Azure

Creating and configuring Azure Web App and MySQL to host a Spring Boot application (2/7)

jdubois profile image Julien Dubois Updated on ・5 min read

This blog post is part of a series on "deploying a Spring Boot and Angular application on Azure", here is the full list of posts:

Creating a resource group

Let's start using Azure! Go to the Azure portal and create a free account if you don't have one currently. What we will do here shouldn't cost very much, so if you are just testing for one day this should only cost you a couple of Euros and should be covered by your free credits. Please note there are other ways to get free credits, for example if you are student you have the GitHub student pack, or if your enterprise pays you a Visual Studio Enterprise account (which is we what we will use here).

One great thing with Azure is that you create "resource groups" in which you will add and configure your services. This means that everything we do here will be in a specific resource group, that we will delete at the end of our tests: there is no risk to forget running resources, and pay for them.

Create a "spring-on-azure" resource group:

Creating the Azure MySQL database

In the search box at the top, look for "MySQL" and select "Azure Database for MySQL".

Create a MySQL database using the resource group we configured earlier, and don't forget to modify the default pricing tier! Here we used the least expensive one, which costs about 27 Euros per month (so about 1 Euro if you run it one full day, which should be more than enough for doing this demo).

Please note somewhere your database name, your username and password, as we will need them later in this article.

Wait for your database to be created (you can skip and read ahead a little bit!), but we are not done yet with the database setup: by default, databases on Azure are protected by a firewall. While security is very important, it's also useful that our database is reachable both by our application and from our development machine, so we need to configure it.

In your MySQL database, go to "Connection security", and select "Allow access to Azure services". You should also click on the "Add client IP" button to automatically add your current IP to the firewall rules.

Now that your MySQL server is running, you need to create a schema. We are going to use MySQL Workbench in this example, but you can of course use any other database tool.

Connect to your database using the credentials we just set up. Please note that your user name is in the form "username@database", and that you should have configured the MySQL firewall to allow your IP address (see 2 paragraphs above).

Now that you are connected to your database, please create the "bugtracker" schema that we will use. As here we are using JHipster, we only need to create an empty schema (JHipster uses Liquibase to update the database schema automatically), but if you are not using JHipster or Liquibase, this is where you could run your SQL update scripts.

Creating the Azure Web App

There are several ways to host a Spring Boot application on Azure, and the most commons ones are to use virtual machines, use Azure Web Apps, or use Azure Kubernetes Services. Here we're going to use Azure Web Apps, as it provides a fully-managed operating system and JDK (unlike a virtual machine, which you must set up and maintain yourself), and it's also very easy to use and very cost-effective (compared to Azure Kubernetes Service). Please note that Azure Web Apps uses the Zulu OpenJDK, so you have a fully-supported, TCK-compliant JDK, even on older JDK releases.

In the Azure portal, search for "App Services" and create a new application. Please note that:

  • We are using Java 11, as Spring Boot applications are just executable Jar files ("make Jar, not War" as my friend Josh Long says).
  • We changed the default machine type to use a "B1", which is very inexpensive yet powerful enough for our needs.

A word on pricing, as here we are trying to budget-conscious: as of this writing, the B1 instances are free the first month, then cost about $30 per month. So that's more than enough for our demo!

Before creating your Web App, the online wizard will propose a specific tab to add monitoring: of course, you need to select it to create a new "Application Insights" instance for your application. We will configure this in the following parts of this blog series: here also, this is all free for a small to moderate application, and the benefits of monitoring are huge!

Now finish creating your application, and continue reading will your instance is being provisioned!

Configuring the Azure Web App

Once that your Azure Web App instance is ready, you will need to configure it. Select your Web Application and click on the "Configuration" menu, to modify the application settings. Those will be exposed as environment variables, that Spring Boot will use when booting up.

  • You should already have a APPINSIGHTS_INSTRUMENTATIONKEY key, which we will use later for configuring Azure Application Insights
  • For your database connection, add SPRING_DATASOURCE_URL, which should point to the MySQL instance we configured above. With a JHipster application, the value should be like jdbc:mysql://spring-on-azure-database.mysql.database.azure.com:3306/bugtracker?useUnicode=true&characterEncoding=utf8& useSSL=true&useLegacyDatetimeCode=false&serverTimezone=UTC. Of course, you will need to replace spring-on-azure-database by your database name.
  • Set up a SPRING_DATASOURCE_USERNAME variable with your database user name in the form of username@database. So if you created a username called juliendubois in a database called spring-on-azure-database, then you will need to write juliendubois@spring-on-azure-database here.
  • Set up a SPRING_DATASOURCE_PASSWORD variable with your database password.
  • If you want to see your application's debug messages, you can configure the LOGGING_LEVEL_YOUR_PACKAGE_NAME key, replacing YOUR_PACKAGE_NAME by the package name used in your application. In our sample application, that would be LOGGING_LEVEL_IO_GITHUB_JDUBOIS_BUGTRACKER (as the package name is io.github.jdubois.bugtracker), and we set it to DEBUG.
  • If you are using JHipster, we also recommend you create a JHIPSTER_SECURITY_AUTHENTICATION_JWT_BASE64_SECRET key, and assign it a base64-encoded token that is a least 256 bits long. This will be your production security key, which shouldn't be shared.

Your Azure infrastructure should now be ready! In the next post of this blog series, we will use Azure Pipelines to deploy the application automatically.

Posted on by:

jdubois profile

Julien Dubois

@jdubois

Azure developer advocate, JHipster creator and lead developer

Microsoft Azure

Any language. Any platform.

Discussion

pic
Editor guide