Last Updated: 10-02-2022
Status: Draft
Overview
In this document, we will cover how to deploy a Springboot project on Azure Cloud.
Note that a limitation of deploying to Azure is that the Springboot project must be compiled on either Java version 8 or 11 only.
It is important to stop your resources when not in use, or delete them when you no longer need them, as Microsoft will continue to bill you if the resources are still running or taking up space.
Java Version
The JRE of the Springboot needs to be either Java version 8 or 11. To check what version you have installed, go to C:\Program Files\Java .
You can download either version here if you do not have it installed: https://www.oracle.com/java/technologies/downloads/archive/
To change the Java version, see steps below:
- Right-click on the project, select Properties
- In the panel that appears, select Java Compiler
- If the Java version is NOT 1.8(This is the same as Java 8) or 11, then uncheck the Use compliance....
- Select either Java 1.8 or 11
- Click Apply and Close.
- Check that the project JRE has changed to the appropriate version
- Visit https://azure.microsoft.com/en-us/free/students/and click the button [ Activate Now > ].
- Sign in using your RP account 19NNNNNN@myrp.edu.sg and password. You will be presented a page where you can enter your personal particulars.
- Do the following data inputs under About you
- Country/Region: Select Singapore.
- First name:
- Last name:
- Email address: Enter the same RP email address in Step 2
- Phone: Enter your 8 digit mobile phone
- Click on the button [ Next ]
- Do the following data inputs under Agreement
- Check the two check boxes.
- Click on the button [ Sign up ]
- You will be redirected the Welcome to Microsoft Azure page. Click on the button [ Maybe later ].
- From the menu at the top-right corner, click Home
- Click and you should see a new subscription created for you.
- Once logged into Azure portal, you will see the screenshot similar to below.
- Click Create a resource
- In the Create a resource page, enter Azure Spring Cloud in the search bar and select Azure Spring Cloud.
- Click Create
- In the configurations that comes up in Project Details,
- Under Subscription, select Azure for Students
- Under Resource group, click Create new and enter c372 as the Name, click OK
(If the name is not available, include your student ID)
6. In Service Details,
- Under Name, enter c372projects
(If the name is not available, include your student ID)
- Under Region, select East US
7. Click Review and Create
8. The resource group will take a while to be created and deployed. You will receive a notification once it is done.
9. Once completed, you should see this a similar screen to below.
Note: In this page, you can also delete the instance when not in use.
- Go back to Azure portal homepage.
- Click Create a resource
- In the Create a resource page, enter Mysql in the search bar and select Azure Database for MySQL
- Click Create
- In the page below, select Create for the Flexible server
- In the Basics tab, in Project Details,
- Under Subscription, select Azure for Students
- Under Resource group, select c372
7. In the Basics tab, in Server Details,
- Under Server name, enter c372mysql
(If the name is not available, include your student ID)
- Under Region, select East US
- Under Workload type, select Development
- Under Compute + storage, leave it if it is similar to the screenshot. Otherwise, click on configure server to select the lowest setting.
- Under Availability, leave it as No preference
- Under MySQL version, select 5.7 (Note, we are not using version 8 as workbench will only work with version 5)
8. In the Basics tab, in High Availability and Administrator account:
- Do not check Enable high availability
- Create an admin username and password. (Note this down)
9. In the Networking tab,
- Under Network connectivity, select Public access
- Under Firewall rules, check Allow public access ....
- Under Firewall rules, click Add current client IP
10. Click Review and create
11. After the settings are validated, click Create
12. Creation and Deployment of the database will take a while. You will receive a notification once deployed.
13. Once deployed, go to the database resource to get the server name. Do make a note of it as you will need to use it later.
Note: In this page you can stop or delete the database when not in use
14. Start MySQL Workbench, click on the + symbol to create a new connection
15. Enter connection details similar as shown.
- Under Hostname, use the database hostname from step 13
- Under username, use the username that you set.
- The password will be prompted
16. Click Test Connection to check that the connection works. (Note, this may not work if using RP network)
17. Click OK
18. You can use workbench as per how you normally do it.
19. Create a database named c372 (Or the same name as you use in the local MySQL)
In pom.xml, you should remove dependencies that are not required when deploying to reduce the size of the upload. For e.g. spring-boot-devtools
is only helpful during development.
- In application.properties, you will need to update the mysql database host.
- Comment the existing configurations for mysql.
Note:
- When you are working on localhost, you will need to switch back to this
- Use the database hostname, username and password from the previous section
Before you start this section, you will need to:
- Start Windows PowerShell and change directory to your project folder.
- To find your project folder, in Eclipse, right click on the project, select Properties
- You will see the location of your code, copy that location
- Use the cd command in PowerShell to change directory to that folder
- In PowerShell, ensure that you are inside the project folder and type the following command to package the project into a JAR file:
mvn clean package -DskipTests
- In Eclipse, refresh your project, you should see the JAR file created.
Note the name of the JAR file as you will need to use it later.
- In Powershell, login to azure by executing the command:
az login
- You will be prompted to login on a web browser, use the same login details as you do for the Azure Portal.
- Once logged in, you will see the output similar to below:
- To create an app, use the command below:
az spring-cloud app create -n -s -g --assign-endpoint true --runtime-version=Java_11
Note:
- Replace <app name> with something suitable such as lesson12
- Replace <service name> with the name of the Azure Spring Cloud created earlier in Section B. In this document, we called it c372projects.
- Replace <resource group name> with the resource group created earlier in Section B In this document, we called it c372.
- If your project is compiled with Java 8, then omit
--runtime-version=Java_11
- To deploy the app, use the command below:
az spring-cloud app deploy -n -s -g --artifact-path target/ --verbose
Note:
- Replace <app name> with the name of the app
- Replace <service name> with the name of the Azure Spring Cloud created earlier in Section B. In this document, we called it c372projects.
- Replace <resource group name> with the resource group created earlier in Section B In this document, we called it c372.
- Replace JAR file name with the actual JAR name found in the project target folder in Eclipse. E.g. LP04-0.0.1-SNAPSHOT.jar
- To check the logs in case of any issue, use the command:
az spring-cloud app logs - -s -g
Note:
- Replace <app name> with the name of the app
- Replace <service name> with the name of the Azure Spring Cloud created earlier in Section B. In this document, we called it c372projects.
- Replace <resource group name> with the resource group created earlier in Section B In this document, we called it c372.
- Once deployed, go to the Azure spring cloud service you created, select Apps. You should see the app you created there.
- Click on the app you created
- Click on the URL generated
Note: In this page, you can also stop or delete the instance when not in use.
It is important to stop your resources when not in use, or delete them when you no longer need them, as Microsoft will continue to bill you if the resources are still running or taking up space.
To easily delete everything, you can go to the resource group that you have grouped your resources under, and click Delete resource group. All related resources will be deleted.