When running Spring Boot web apps on PaaS, I've mainly used Heroku and Cloud Foundry (PWA, etc.) because it's "easy". However, when I was researching Microsoft Azure the other day, I found an official document that it can be easily deployed to App Service, so I tried it.
https://docs.microsoft.com/ja-jp/java/azure/spring-framework/deploy-spring-boot-java-app-with-maven-plugin
When I tried it, it was really "easy"!
I have deployed it with a simpler procedure than the above document, so I will summarize it. The main differences in the procedure are as follows.
--Based on Spring Initializr --Deploy as jar instead of war --Do not add tomcat dependency --Do not use SpringBootServletInitializer inheritance
Click here for the code. https://github.com/kikutaro/AzureSpringBoot
Create using "** Spring Initializr Java Support **" in the Visual Studio Code extension.
** Select "View" **-> ** "Command Palette" **
MS documentation is Maven based, so select ** "Spring Initializr: Generate a Maven Project" **
Select Java
Enter the GroupId. I chose "tech.kikutaro".
Enter the ArtifactId. I chose "azure spring boot".
Select 2.0.5 for Spring Boot
Select two dependencies, Web and DevTools
that's all. After saving, the following dialog will be displayed. Open it with "Open it".
Before deploying to Azure, write a simple code and check the operation at hand. Here, the code is just to return "Hello, Azure!" In REST GET.
DemoApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello, Azure!";
}
}
Confirm the startup.
.\mvnw.cmd clean package
.\mvnw.cmd spring-boot:run
It is OK if you can access the local environment and check the operation.
MS's document wars packaging I have devised to make it work with other Servlets such as jetty, such as changing to, adding tomcat dependency, inheriting SpringBootServletInitializer, but this time I proceeded without changing this area in particular. ..
Actually, the following settings are not required, but I added it this time to prevent the URL of the deployed application from colliding.
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- add from -->
<maven.build.timestamp.format>yyyyMMddHHmm</maven.build.timestamp.format>
<!-- add to -->
</properties>
Add the settings for azure-webapp-maven-plugin. The documentation says "<!-Check latest version on Maven Central->".
In addition, when I tried using 1.1.0 as the first document and setting the deploymentType to jar, an error occurred. It seems that deploymentType does not support jar.
With the latest 1.4.0, I was able to specify the jar without any problems.
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- add from -->
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-webapp-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<resourceGroup>maven-projects</resourceGroup>
<appName>${project.artifactId}-${maven.build.timestamp}</appName>
<region>westus</region>
<javaVersion>1.8</javaVersion>
<deploymentType>jar</deploymentType>
</configuration>
</plugin>
<!-- add to -->
</plugins>
</build>
I installed Azure CLI before deploying.
When you log in with the Azure CLI, the browser will start automatically and the login screen will be displayed. Enter your username and password to log in.
python
az login
All you have to do is deploy.
.\mvnw.cmd clean package
.\mvnw.cmd azure-webapp:deploy
Wait for the deployment to complete.
If successful, open the Azure App Service to see the deployed application.
After selecting the application, click the URL link displayed in the upper right to open the created page.
moved!
It looks long because I wrote the procedure in detail, but it was as easy as deploying to Heroku etc.
Recommended Posts