Search Tutorials


Deploy a Spring Boot + MySQL App to Azure App Service | JavaInUse

Deploy a Spring Boot + MySQL App to Azure App Service

In previous tutorial we deployed Spring Boot 3 Application JAR to Azure App Service. In previous tutorial we implemented Spring Boot 3 + MySQL Example to perform CRUD operations. In this tutorial we will be deploying this spring boot + mysql application as azure app service. For this we will be using the following workflow.
Deploy a Spring Boot App(JAR) to Azure App Service

Video

This tutorial is explained in the below Youtube Video.

Implementation

Create MySQL For Azure Instance

Go to the azure portal, and go to azure database for mysql servers.
Deploy a Spring Boot App(JAR) to Azure App Service
Click on create button and select Flexible Server
Deploy a Spring Boot App(JAR) to Azure App Service
Give server details as
Server name - javainuse-mysql
Username - javainuse
Password - Hello@123

Deploy a Spring Boot App(JAR) to Azure App Service

Deploy a Spring Boot App(JAR) to Azure App Service
And Click Create
Deploy a Spring Boot App(JAR) to Azure App Service
Azure MySQL instance with url - javainuse-mysql.mysql.database.azure.com will get created.
Deploy a Spring Boot App(JAR) to Azure App Service
Next click on the connect button of azure mysql instance which will allow other azure services to connect to this mysql instance.
Deploy a Spring Boot App(JAR) to Azure App Service

Spring Boot Application

In previous tutorial we had implemented spring boot + mysql application. We will be making use of this spring boot application which you can download from the tutorial. Start the MySQL database
Deploy a Spring Boot App(JAR) to Azure App Service




Next start the spring boot application
Deploy a Spring Boot App(JAR) to Azure App Service
If we now go to localhost:8080/employees to get the list of employees that we had previously inserted in mysql database.
Deploy a Spring Boot App(JAR) to Azure App Service
We will now be changing the MySQL configuration in the properties file to make use of the azure mysql instead of the local instance.
spring.datasource.url= jdbc:mysql://javainuse-mysql.mysql.database.azure.com/javainusedb?createDatabaseIfNotExist=true
spring.datasource.username= javainuse
spring.datasource.password= Hello@123

spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto= update

Azure Webapp Maven Plugin

To the pom.xml we will be adding the azure webapp maven plugin as follows-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>3.2.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javainuse</groupId>
	<artifactId>boot-mysql-crud</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>boot-mysql-crud</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</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.9.0</version>
			</plugin>
		</plugins>
	</build>

</project>
Next we will be building this pom.xml using the maven command - azure-webapp:config
Deploy a Spring Boot App(JAR) to Azure App Service
This will download all the azure-webapp maven plugin dependencies. Also it will create the following configuration for our application.
Deploy a Spring Boot App(JAR) to Azure App Service
In the pom.xml abve configuration gets added automatically-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>3.2.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javainuse</groupId>
	<artifactId>boot-mysql-crud</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>boot-mysql-crud</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</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.9.0</version>
			    <configuration>
			        <schemaVersion>v2</schemaVersion>
			        <resourceGroup>boot-mysql-crud-rg1</resourceGroup>
			        <appName>boot-mysql-crud1</appName>
			        <pricingTier>P1v2</pricingTier>
			        <region>centralus</region>
			        <runtime>
			            <os>Linux</os>
			            <javaVersion>Java 17</javaVersion>
			            <webContainer>Java SE</webContainer>
			        </runtime>
			        <deployment>
			            <resources>
			                <resource>
			                    <directory>/target</directory>
			                    <includes>
			                        <include>*.jar</include>
			                    </includes>
			                </resource>
			            </resources>
			        </deployment>
			    </configuration>
			</plugin>	
		</plugins>
	</build>

</project>
We will be modifying the above generated configuration to specify the resource group name, application name and also sepcify the server port on which the above application will be running when deployed to azure webapp.
				<configuration>
			        <schemaVersion>v2</schemaVersion>
			        <resourceGroup>boot-mysql-crud-rg1</resourceGroup>
			        <appName>boot-mysql-crud1</appName>
			        <pricingTier>P1v2</pricingTier>
			        <region>centralus</region>
			        <runtime>
			            <os>Linux</os>
			            <javaVersion>Java 17</javaVersion>
			            <webContainer>Java SE</webContainer>
			        </runtime>
			        <appSettings>
			            <property>
			                <name>JAVA_OPTS</name>
			                <value>-Dserver.port=80</value>
			            </property>
			        </appSettings>
			        <deployment>
			            <resources>
			                <resource>
			                    <directory>/target</directory>
			                    <includes>
			                        <include>*.jar</include>
			                    </includes>
			                </resource>
			            </resources>
			        </deployment>
			    </configuration>
Next in the command prompt login to the azure portal as follows-
Deploy a Spring Boot App(JAR) to Azure App Service
az login
Next go to the project location and use the mvn azure-webapp:deploy command to deploy the jar file to azure app service.
Deploy a Spring Boot App(JAR) to Azure App Service
With the above maven command the application gets deployed to azure web apps.
Deploy a Spring Boot App(JAR) to Azure App Service
If we now try to access the exposed GET endpoint for getting all the employees , we get the output as follows-
Deploy a Spring Boot App(JAR) to Azure App Service
Let us insert some records in the azure mysql database as follows-
Deploy a Spring Boot App(JAR) to Azure App Service
If we now try to get the employee records we get the output as follows-
Deploy a Spring Boot App(JAR) to Azure App Service

Download Source Code

Download it -
Spring Boot + MySQL + Azure App + JAR Example