Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy Spring Boot jar on Azure App Service

I'm having trouble getting a Spring Boot API to work on an Azure app service. I've followed the Microsoft guide on https://learn.microsoft.com/en-us/java/azure/spring-framework/deploy-spring-boot-java-web-app-on-azure but having no luck so far.

The application does start (I can see the app boot up in the log file) but http requests to the app service url always end in a timeout.

I've read that Azure app services only pick up embedded tomcat servers that run on port 80 or 8080, but had no luck with that as well.

The app is deployed in the www root and an appropriate web.config is deployed as well.

I've tried running the App Service with and without an application server (Tomcat and Jetty, that is not needed because the server is embedded in the application), but both approaches failed.

Am I missing some other configuration part? Or could this be related to the type of plan I'm using on azure? Maybe some issue with the resource?

Any pointers?

Thx,

Bert

like image 789
Bert Vandamme Avatar asked Dec 07 '17 17:12

Bert Vandamme


4 Answers

In order to get a Springboot application running you need to upload your JAR file and add the web.config file.

To communicate to the service what you are trying to run, you need to add a web.config file to the site\wwwroot folder of the app service. As you have already created web.config file, use Maven to add the following and get a project / release dynamically included on package.

<build>
    <resources>
        <resource>
            <directory>${project.basedir}/wwwroot</directory>
            <filtering>true</filtering>
            <targetPath>${basedir}/target</targetPath>
        </resource>
    </resources>
</build> 

Now place the jar file and the web.config within the Azure App Service.

You can just check once whether you have created the web.config file as below,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
        </handlers>
        <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
        arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\@project.artifactId@[email protected]@.jar&quot;">
        </httpPlatform>
    </system.webServer>
</configuration>
like image 61
Swikruti Bose Avatar answered Oct 19 '22 00:10

Swikruti Bose


Please use following steps given by spring and azure community to deploy spring boot app on azure:

1) Go inside of your app folder where you have pom file and run

make sure following plugins should be in pom file

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- tag::actuator[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- end::actuator[] -->
        <!-- tag::tests[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- end::tests[] -->
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
        <maven.build.timestamp.format>yyyyMMddHHmmssSSS</maven.build.timestamp.format>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-webapp-maven-plugin</artifactId>
                <version>0.1.5</version>
                <configuration>
                    <authentication>
                        <serverId>azure-auth</serverId>
                    </authentication>
                    <resourceGroup>maven-plugin</resourceGroup>
                    <appName>maven-web-app-${maven.build.timestamp}</appName>
                    <region>westus</region>
                    <javaVersion>1.8</javaVersion>
                    <deploymentType>ftp</deploymentType>
                    <stopAppDuringDeployment>true</stopAppDuringDeployment>
                    <resources>
                        <resource>
                            <directory>${project.basedir}/target</directory>
                            <targetPath>/</targetPath>
                            <includes>
                                <include>*.jar</include>
                            </includes>
                        </resource>
                        <resource>
                            <directory>${project.basedir}</directory>
                            <targetPath>/</targetPath>
                            <includes>
                                <include>web.config</include>
                            </includes>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Note : Make sure you have created web app on azure with same name as
maven-web-app-${maven.build.timestamp}

Now create file on root with name "web.config" and add your jar in web.comfig

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
        </handlers>
        <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
                      arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\azure-rest-example-app-0.1.0.jar&quot;">
        </httpPlatform>
    </system.webServer>
</configuration>

enter image description here

Now open azure CLI and run following commands enter image description here

  • mvn clean package
  • mvn spring-boot:run

Make sure app is working fine on local.

Now use following commands if you have multiple account associated with your id

  • az login

  • az account list

  • az account set --subscription XXX-XXX-XXX-XXXXXXXXXXXX

Now you need to create "Service Principals in Microsoft Azure"

1) Open a terminal window.

2) Sign into your Azure account with the Azure CLI by typing az login

3) Create an Azure service principal by typing az ad sp create-for-rbac --name "vaquarkhan" --password "yourpassword" (vaquarkhan is the user name and yourpassword is the password for the service principal).

az ad sp create-for-rbac --name "app-name" --password "password"

NOTE :if you getting error need to change settings---> here

"azure.graphrbac.models.graph_error.GraphErrorException: Guest users are not allowed to perform this action."

if success

Azure should print out a JSON response resembling this:

{
   "appId": "XXX-XXXX-XXX-XXX-XXXX",
   "displayName": "vaquarkhan",
   "name": "http://vaquarkhan",
   "password": "yourpassword",
   "tenant": "YYY-YYYY-YYY-YYY-YYYY"
}

Configure Maven to use your Azure service principal

1) Open your Maven settings.xml file in a text editor (usually found at either /etc/maven/settings.xml or $HOME/.m2/settings.xml).

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository/>
  <interactiveMode/>
  <usePluginRegistry/>
  <offline/>
  <pluginGroups/>

  <servers>
   <server>
     <id>azure-auth</id>
      <configuration>
         <client>ur key</client>
         <tenant>ur tenant</tenant>
         <key>YOUR PASSWORD</key>
         <environment>AZURE</environment>
      </configuration>
   </server>
</servers>


  <proxies/>

  <profiles>
    <profile>
      <id>hwx</id>
      <repositories>
        <repository>
          <id>hwx</id>
          <name>hwx</name>
          <url>http://nexus-private.hortonworks.com/nexus/content/groups/public/</url>
        </repository>
      </repositories>
    </profile>
  </profiles>


  <mirrors>
    <mirror>
      <id>public</id>
      <mirrorOf>*</mirrorOf>
      <url>http://nexus-private.hortonworks.com/nexus/content/groups/public/</url>
    </mirror>
  </mirrors>

  <activeProfiles/>
</settings>

2) Add your Azure service principal settings from the previous section of this tutorial to the collection in the settings.xml file as shown below:

<servers>
   <server>
     <id>azure-auth</id>
      <configuration>
         <client>aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</client>
         <tenant>tttttttt-tttt-tttt-tttt-tttttttttttt</tenant>
         <key>pppppppp</key>
         <environment>AZURE</environment>
      </configuration>
   </server>
</servers>

3) Save and close the settings.xml file.

Build and deploy your app to Azure

1) run following command

  • mvn azure-webapp:deploy
  • When your web app has been deployed, visit the Azure portal to manage it. It will be listed in App Services.

  • Click on the application. From there, the publicly-facing URL for your web app will be listed in the Overview section

  • Determining the URL for your web app You can click on this link to visit the Spring Boot application and interact with it.

enter image description here enter image description here

Azure maven plugin doc

  • https://learn.microsoft.com/en-us/java/api/overview/azure/maven/azure-webapp-maven-plugin/readme

Note : The website name has to be globally unique and its generated using app name , make sure name should be unique.

like image 20
vaquar khan Avatar answered Oct 19 '22 01:10

vaquar khan


Combining the steps in the official tutorials and your actual situation, I provide the following check points:

Point 1: Please use mvn package to bulid the JAR package in the directory under which the pom.xml file is located.

enter image description here]

Point 2: Please make sure that the jar package name configured in web.config is the same as the uploaded jar package name.

enter image description here

web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
        arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\<your project name>&quot;">
    </httpPlatform>
  </system.webServer>
</configuration>

Point 3: Please use FTP to publish jar files and web.config to D:\home\site\wwwroot\ directory on KUDU.

Point 4: Please make sure ApplicationSettings matches your project such as jdk version,tomcat version.

enter image description here

If you want to deploy a war file, you need to configure the ApplicationSettings of your app service on Azure portal, then upload the war file into the path D:\home\site\wwwroot\webapps.

In addition, you could check the log files on KUDU : https://<your project name>.scm.azurewebsites.net/DebugConsole.

As references, please refer to the documents and threads below.

1.Configure web apps in Azure App Service

2.Create a Java web app in Azure App Service

3.Deploying Springboot to Azure App Service.

Hope it helps you.

like image 2
Jay Gong Avatar answered Oct 19 '22 00:10

Jay Gong


It turns out that my hunch about it being an issue with the azure resource was correct. Upscaling resource memory and CPU resolved the issue.

like image 1
Bert Vandamme Avatar answered Oct 19 '22 01:10

Bert Vandamme