Pomiń nawigację

End-to-end Java Experience in App Service Linux

Ostatnia aktualizacja: 03.09.2018
Edytuj w witrynie GitHub

This guide walks you through the process of building, configuring, deploying, trouble shooting and scaling Java Web apps in App Service Linux.

What you will build

You will build a Java Web app using Spring Framework, JSP, Spring Data, Hibernate, JDBC, App Service Linux and Azure Database for MySQL.

What you will need

In order to deploy a Java Web app to cloud, you need an Azure subscription. If you do not already have an Azure subscription, you can activate your MSDN subscriber benefits or sign up for a free Azure account).

In addition, you will need the following:

| Azure CLI | Java 8 | Maven 3 | Git | Tomcat | MySQL CLI |

Getting Started

You can start from scratch and complete each step, or you can bypass basic setup steps that you are already familiar with. Either way, you will end up with working code.

When you are finished, you can check your results against YOUR code in e2e-java-experience-in-app-service-linux/complete.

You can start from e2e-java-experience-in-app-service-linux/initial-hsqldb. Or, you can clone from spring-framework-petclinic

If you are starting from scratch, you can scaffold a Web app using Maven Web app archetype or start.spring.io.

Step ONE - Clone and Prep

git clone --recurse-submodules https://github.com/Azure-Samples/e2e-java-experience-in-app-service-linux.git
yes | cp -rf .prep/* .

Build and Deploy Pet Clinic Powered Using HSQLDB

Open the initial-hsqldb/spring-framework-petclinic sample app in your favorite IDE - IntelliJ | Eclipse | VS Code.

Build Pet Clinic

cd initial-hsqldb/spring-framework-petclinic

bash-3.2$ mvn package
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------------------------------------------------...
...

[INFO] --- maven-war-plugin:2.3:war (default-war) @ spring-petclinic [INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------   ```

#### Deploy to Local Tomcat

Add local Tomcat configuration to POM.xml and deploy
Pet Clinic to a Tomcat in your local machine: 

```xml
<plugins>

    <!--*************************************************-->
    <!-- Deploy to Tomcat on localhost                   -->
    <!--*************************************************-->
    <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <version>1.6.7</version>
        <configuration>
            <container>
                <containerId>tomcat8x</containerId>
                <type>installed</type>
                <home>${TOMCAT_HOME}</home>
            </container>
            <configuration>
                <type>existing</type>
                <home>${TOMCAT_HOME}</home>
            </configuration>
            <!-- Deployables configuration -->
            <deployables>
                <deployable>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>${project.artifactId}</artifactId>
                    <type>war</type>
                    <properties>
                        <context>/</context>
                    </properties>
                </deployable>
            </deployables>
        </configuration>
    </plugin>
    ...

</plugins>

Set environment variables for binding secrets at runtime:

cp set-env-variables-template.sh .scripts/set-env-variables.sh

Modify .scripts/set-env-variables.sh and set Azure Resource Group name, App Service Web App Name, Azure Region and Tomcat directory in the local machine. Then, set environment variables:

source .scripts/set-env-variables.sh

Deploy to a local Tomcat:

bash-3.2$ mvn cargo:deploy
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------------------------------------------------[INFO] 
[INFO] --- cargo-maven2-plugin:1.6.7:deploy (default-cli) @ spring-petclinic [INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------```

#### Start Tomcat

```bash
cd ${TOMCAT_HOME}
./bin/catalina.sh run

Open Pet Clinic (HSQLDB) running on Local Tomcat

open http://localhost:8080

Deploy to App Service Linux

Add Maven Plugin for Azure App Service configuration to POM.xml and deploy Pet Clinic to Tomcat in App Service Linux:

<plugins> 

    <!--*************************************************-->
    <!-- Deploy to Tomcat in App Service Linux           -->
    <!--*************************************************-->

    <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>1.4.0</version>
        <configuration>

            <!-- Web App information -->
            <resourceGroup>${RESOURCEGROUP_NAME}</resourceGroup>
            <appServicePlanName>${WEBAPP_PLAN_NAME}</appServicePlanName>
            <appName>${WEBAPP_NAME}</appName>
            <region>${REGION}</region>

            <!-- Java Runtime Stack for Web App on Linux-->
            <linuxRuntime>tomcat 8.5-jre8</linuxRuntime>

        </configuration>
    </plugin>
    ...
</plugins>

Deploy to Tomcat in App Service Linux:

// Log into Azure
az login

// Deploy
bash-3.2$ mvn azure-webapp:deploy
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------------------------------------------------[INFO] 
[INFO] --- azure-webapp-maven-plugin:1.4.0:deploy (default-cli) @ spring-petclinic [INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------```

#### Open Pet Clinic running on Tomcat in App Service Linux

```bash
open https://petclinic-hsqldb.azurewebsites.net

When you are finished, you can check your results against YOUR code in e2e-java-experience-in-app-service-linux/initial-mysql.

Build and Deploy Pet Clinic Powered Using Azure Database for MySQL

Start your next leg of your journey ... change directory:

cd ../../initial-mysql/spring-framework-petclinic

Activate MySQL Profile

Activate MySQL profile in POM.xml by moving the xml <activation></activation> markup to MySQL profile:

        <profile>
            <id>MySQL</id>
            <!-- Activate MySQL profile -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            ...
        </profile>

Set environment variables for binding secrets at runtime

cp set-env-variables-template.sh .scripts/set-env-variables.sh

Modify .scripts/set-env-variables.sh and set Azure Resource Group name, App Service Web App Name, Azure Region, Tomcat directory in the local machine and MySQL server info. Then, set environment variables:

source .scripts/set-env-variables.sh

Provision and Configure Pet Clinic DB in Azure Database for MySQL

Provision Pet Clinic DB using Azure CLI and MySQL CLI:

// create mysql server
az mysql server create --resource-group ${RESOURCEGROUP_NAME} \
 --name ${MYSQL_SERVER_NAME}  --location westus2 \
 --admin-user ${MYSQL_SERVER_ADMIN_LOGIN_NAME} \
 --admin-password ${MYSQL_SERVER_ADMIN_PASSWORD} \
 --sku-name GP_Gen4_2 \
 --ssl-enforcement Disabled \
 --version 5.7

// allow access from Azure resources
az mysql server firewall-rule create --name allAzureIPs \
 --server ${MYSQL_SERVER_NAME} \
 --resource-group ${RESOURCEGROUP_NAME} \
 --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0

// allow access from your dev machine for testing
az mysql server firewall-rule create --name allAzureIPs \
 --server ${MYSQL_SERVER_NAME} \
 --resource-group ${RESOURCEGROUP_NAME} \
 --start-ip-address <ip-address-of-your-dev-machine> \
 --end-ip-address <ip-address-of-your-dev-machine>

// increase connection timeout
az mysql server configuration set --name wait_timeout \
 --resource-group ${RESOURCEGROUP_NAME} \
 --server ${MYSQL_SERVER_NAME} --value 2147483

// set server timezone
az mysql server configuration set --name time_zone \
 --resource-group ${RESOURCEGROUP_NAME} \
 --server ${MYSQL_SERVER_NAME} --value US/Pacific

mysql -u ${MYSQL_SERVER_ADMIN_LOGIN_NAME} \
 -h ${MYSQL_SERVER_FULL_NAME} -P 3306 -p

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 64379
Server version: 5.6.39.0 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE ${MYSQL_DATABASE_NAME};
Query OK, 1 row affected (0.10 sec)

mysql> CREATE USER 'root' IDENTIFIED BY '${MYSQL_DATABASE_NAME}';
Query OK, 0 rows affected (0.11 sec)

mysql> GRANT ALL PRIVILEGES ON ${MYSQL_DATABASE_NAME}.* TO 'root';
Query OK, 0 rows affected (1.29 sec)

mysql> quit
Bye

Setup configuration for binding secrets at runtime instead of compile time

Modify MySQL configuration in POM.xml to bind secrets at runtime:

<profile>
    <id>MySQL</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <db.script>mysql</db.script>
        <jpa.database>MYSQL</jpa.database>
        <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
        <jdbc.url>jdbc:mysql://${DOLLAR}{MYSQL_SERVER_FULL_NAME}:3306/${DOLLAR}{MYSQL_DATABASE_NAME}?useUnicode=true</jdbc.url>
        <jdbc.username>${DOLLAR}{MYSQL_SERVER_ADMIN_LOGIN_NAME}</jdbc.username>
        <jdbc.password>${DOLLAR}{MYSQL_SERVER_ADMIN_PASSWORD}</jdbc.password>
    </properties>
    ...
</profile>

Modify Maven Plugin for Azure App Service configuration in POM.xml to bind secrets at runtime:

<!--*************************************************-->
<!-- Deploy to tomcat in app service linux           -->
<!--*************************************************-->

<plugin>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-webapp-maven-plugin</artifactId>
    <version>1.4.0</version>
    <configuration>

        <!-- Web App information -->
        <resourceGroup>${RESOURCEGROUP_NAME}</resourceGroup>
        <appServicePlanName>${WEBAPP_PLAN_NAME}</appServicePlanName>
        <appName>${WEBAPP_NAME}</appName>
        <region>${REGION}</region>

        <!-- Java Runtime Stack for Web App on Linux-->
        <linuxRuntime>tomcat 8.5-jre8</linuxRuntime>

        <appSettings>
            <property>
                <name>MYSQL_SERVER_FULL_NAME</name>
                <value>${MYSQL_SERVER_FULL_NAME}</value>
            </property>
            <property>
                <name>MYSQL_SERVER_ADMIN_LOGIN_NAME</name>
                <value>${MYSQL_SERVER_ADMIN_LOGIN_NAME}</value>
            </property>
            <property>
                <name>MYSQL_SERVER_ADMIN_PASSWORD</name>
                <value>${MYSQL_SERVER_ADMIN_PASSWORD}</value>
            </property>
            <property>
                <name>MYSQL_DATABASE_NAME</name>
                <value>${MYSQL_DATABASE_NAME}</value>
            </property>
        </appSettings>
    </configuration>
</plugin>

Build Pet Clinic

bash-3.2$ mvn package
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------------------------------------------------...
...
[INFO] --- maven-war-plugin:2.3:war (default-war) @ spring-petclinic [INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------```
#### Deploy to Local Tomcat and Test

Deploy to a local Tomcat:

```bash
bash-3.2$ mvn cargo:deploy
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------------------------------------------------[INFO] 
[INFO] --- cargo-maven2-plugin:1.6.7:deploy (default-cli) @ spring-petclinic [INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------```

Start local Tomcat:

```bash
cd ${TOMCAT_HOME}
./bin/catalina.sh run

The Pet Clinic app is now powered using Azure Database for MySQL. The app should look the same as before.

Deploy to App Service Linux

Deploy to Tomcat in App Service Linux:

bash-3.2$ mvn azure-webapp:deploy
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------------------------------------------------[INFO] 
[INFO] --- azure-webapp-maven-plugin:1.4.0:deploy (default-cli) @ spring-petclinic [INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------```

#### Trouble Shoot Pet Clinic on Azure by Viewing Logs

Configure logs for the deployed Java Web 
app in App Service Linux:

```bash
az webapp log config --name ${WEBAPP_NAME} \
 --resource-group ${RESOURCEGROUP_NAME} \
  --web-server-logging filesystem

Open Java Web app remote log stream from a local machine:

az webapp log tail --name ${WEBAPP_NAME} \
 --resource-group ${RESOURCEGROUP_NAME}

2018-09-03T14:45:21  Welcome, you are now connected to log-streaming service.

2018-09-03 14:43:44.347 INFO  - Starting container for site
2018-09-03 14:43:44.354 INFO  - docker run -d -p 17101:80 --name petclinic-mysql_2 -e WEBSITE_SITE_NAME=petclinic-mysql -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_INSTANCE_ID=4440212f0d71a318f30d6be76de43332336c145d0f11304df4b060a498433702 -e HTTP_LOGGING_ENABLED=1 appsvc/tomcat:8.5-jre8_0000000000  
...
...
2018-09-03T14:45:20.208498228Z INFO  SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'
2018-09-03T14:45:20.485989744Z INFO  DispatcherServlet - FrameworkServlet 'dispatcher': initialization completed in 6597 ms
2018-09-03T14:45:20.751742047Z 03-Sep-2018 14:45:20.751 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/home/site/wwwroot/webapps/ROOT] has finished in [69,452] ms
2018-09-03T14:45:20.771519969Z 03-Sep-2018 14:45:20.770 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-80"]
2018-09-03T14:45:20.829513635Z 03-Sep-2018 14:45:20.829 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 70243 ms

Open Pet Clinic running on Tomcat in App Service Linux

open https://petclinic-mysql.azurewebsites.net

When you are finished, you can check your results against YOUR code in e2e-java-experience-in-app-service-linux/complete.

Scale out the Pet Clinic App

Scale out Java Web app using Azure CLI:

az appservice plan update --number-of-workers 2 \
   --name ${WEBAPP_PLAN_NAME} \
   --resource-group ${RESOURCEGROUP_NAME}

Congratulations!

Congratulations!! You built and scaled out a Java Web app using Spring Framework, JSP, Spring Data, Hibernate, JDBC, App Service Linux and Azure Database for MySQL.

Resources


This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.