An open API service indexing awesome lists of open source software.

https://github.com/selvasingh/e2e-java-experience-in-app-service-linux-part-2

Deploy Spring Boot app to Java SE - end-to-end Java experience in App Service Linux
https://github.com/selvasingh/e2e-java-experience-in-app-service-linux-part-2

Last synced: about 1 year ago
JSON representation

Deploy Spring Boot app to Java SE - end-to-end Java experience in App Service Linux

Awesome Lists containing this project

README

          

---
services: app-service, cosmos-db
platforms: java
author: selvasingh
---

# End-to-end Java Experience in App Service Linux

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 Boot](https://spring.io/projects/spring-boot),
[Spring Data for Cosmos DB](https://docs.microsoft.com/en-us/java/azure/spring-framework/configure-spring-boot-starter-java-app-with-cosmos-db?view=azure-java-stable),
[Azure Cosmos DB](https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-introduction)
and
[App Service Linux](https://docs.microsoft.com/en-us/azure/app-service/containers/app-service-linux-intro).

## 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](https://azure.microsoft.com/pricing/member-offers/msdn-benefits-details/)
or sign up for a
[free Azure account]((https://azure.microsoft.com/pricing/free-trial/)).

In addition, you will need the following:

| [Azure CLI](http://docs.microsoft.com/cli/azure/overview)
| [Java 8](https://www.azul.com/downloads/azure-only/zulu)
| [Maven 3](http://maven.apache.org/)
| [Git](https://github.com/)
|

## 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-part-2/complete](https://github.com/Azure-Samples/e2e-java-experience-in-app-service-linux-part-2/tree/master/complete).

You can start from [e2e-java-experience-in-app-service-linux-part-2/initial](https://github.com/Azure-Samples/e2e-java-experience-in-app-service-linux-part-2/tree/master/initial).
Or, you can clone from [spring-todo-app](https://github.com/microsoft/spring-todo-app)

If you are starting from scratch, you can scaffold a Web app using
[Maven Web app archetype](https://maven.apache.org/archetypes/maven-archetype-webapp/)
or [start.spring.io](https://start.spring.io/).

#### Step ONE - Clone and Prep

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

## Create Azure Cosmos DB

Create Azure Cosmos DB
using [Azure CLI 2.0](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest)

### STEP A - LOGIN to Azure
Login your Azure CLI, and set your subscription

```bash
az login
az account set -s
```
### STEP B - Create Resource Group

Create an Azure Resource Group, and note down the resource group name

```bash
az group create -n \
-l
```

### STEP C - Create COSMOS DB

Create Azure Cosmos DB with GlobalDocumentDB kind.
The name of Cosmos DB must use only lower case letters. Note down the `documentEndpoint` field in the response

```bash
az cosmosdb create --kind GlobalDocumentDB \
-g \
-n
```

### STEP D - Get COSMOS DB Key

Get your Azure Cosmos DB key, get the `primaryMasterKey`

```bash
az cosmosdb list-keys -g -n
```

## Build and Run Locally - Spring TODO App Powered Using Azure Cosmos DB

Open the [initial/spring-todo-app](https://github.com/Azure-Samples/e2e-java-experience-in-app-service-linux-part-2/tree/master/initial)
sample app in your favorite IDE - IntelliJ | Eclipse | VS Code.

### STEP 1 - Change directory

```bash
cd initial/spring-todo-app
```

### STEP 2 - Configure the app

Set environment variables using a script file. Start with
the supplied template in the repo:

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

Edit .scripts/set-env-variables.sh and supply Azure
Cosmos DB connection info. Particularly:

```bash
export COSMOSDB_URI=
export COSMOSDB_KEY=
export COSMOSDB_DBNAME=

// App Service Linux Configuration
export RESOURCEGROUP_NAME=
export WEBAPP_NAME=
export REGION=
```

Set environment variables:

```bash
source .scripts/set-env-variables.sh
```

### STEP 3 - Run Spring TODO App locally

```bash
mvn package spring-boot:run
```

```bash
bash-3.2$ mvn package spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-todo-app 2.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]

[INFO] SimpleUrlHandlerMapping - Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[INFO] SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[INFO] WelcomePageHandlerMapping - Adding welcome page: class path resource [static/index.html]
2018-10-28 15:04:32.101 INFO 7673 --- [ main] c.m.azure.documentdb.DocumentClient : Initializing DocumentClient with serviceEndpoint [https://asir-cosmosdb-01-19-2008-westus.documents.azure.com:443/], ConnectionPolicy [ConnectionPolicy [requestTimeout=60, mediaRequestTimeout=300, connectionMode=Gateway, mediaReadMode=Buffered, maxPoolSize=800, idleConnectionTimeout=60, userAgentSuffix=;spring-data/2.0.6;098063be661ab767976bd5a2ec350e978faba99348207e8627375e8033277cb2, retryOptions=com.microsoft.azure.documentdb.RetryOptions@6b9fb84d, enableEndpointDiscovery=true, preferredLocations=null]], ConsistencyLevel [null]
[INFO] AnnotationMBeanExporter - Registering beans for JMX exposure on startup
[INFO] TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
[INFO] TodoApplication - Started TodoApplication in 45.573 seconds (JVM running for 76.534)
```

You can access Spring TODO App here: [http://localhost:8080/](http://localhost:8080/).

![](./media/spring-todo-app-running-locally.jpg)

## Deploy to App Service Linux

### Step 4 - Configure for Cloud Deployment

Add [Maven Plugin for Azure App Service](https://github.com/Microsoft/azure-maven-plugins/blob/develop/azure-webapp-maven-plugin/README.md) configuration to POM.xml and deploy
Spring TODO App to Java SE in App Service Linux:

```xml






com.microsoft.azure
azure-webapp-maven-plugin
1.4.0

jar


${RESOURCEGROUP_NAME}
${WEBAPP_NAME}
${REGION}


jre8



COSMOSDB_URI
${COSMOSDB_URI}


COSMOSDB_KEY
${COSMOSDB_KEY}


COSMOSDB_DBNAME
${COSMOSDB_DBNAME}


JAVA_OPTS
-Dserver.port=80





...

```

### Step 5 - Deploy to Java SE in App Service Linux

```bash

// Deploy
bash-3.2$ mvn azure-webapp:deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-todo-app 2.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- azure-webapp-maven-plugin:1.4.0:deploy (default-cli) @ spring-todo-app ---
[INFO] Authenticate with Azure CLI 2.0
[INFO] Target Web App doesn't exist. Creating a new one...
[INFO] Creating App Service Plan 'ServicePlanb6ba8178-5bbb-49e7'...
[INFO] Successfully created App Service Plan.
[INFO] Successfully created Web App.
[INFO] Trying to deploy artifact to spring-todo-app...
[INFO] Successfully deployed the artifact to https://spring-todo-app.azurewebsites.net
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:19 min
[INFO] Finished at: 2018-10-28T15:32:03-07:00
[INFO] Final Memory: 50M/574M
[INFO] ------------------------------------------------------------------------
```

#### Open Spring Boot TODO App running on Java SE in App Service Linux

```bash
open https://spring-todo-app.azurewebsites.net
```
![](./media/spring-todo-app-running-in-app-service.jpg)

### Step 6 - Trouble Shoot Spring TODO App 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:

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

bash-3.2$ az webapp log tail --name ${WEBAPP_NAME} --resource-group ${RESOURCEGROUP_NAME}
2018-10-28T22:50:17 Welcome, you are now connected to log-streaming service.
2018-10-28T22:44:56.265890407Z _____
2018-10-28T22:44:56.265930308Z / _ \ __________ _________ ____
2018-10-28T22:44:56.265936008Z / /_\ \___ / | \_ __ \_/ __ \
2018-10-28T22:44:56.265940308Z / | \/ /| | /| | \/\ ___/
2018-10-28T22:44:56.265944408Z \____|__ /_____ \____/ |__| \___ >
2018-10-28T22:44:56.265948508Z \/ \/ \/
2018-10-28T22:44:56.265952508Z A P P S E R V I C E O N L I N U X
2018-10-28T22:44:56.265956408Z Documentation: http://aka.ms/webapp-linux
2018-10-28T22:44:56.266260910Z Setup openrc ...
2018-10-28T22:44:57.396926506Z Service `hwdrivers' needs non existent service `dev'
2018-10-28T22:44:57.397294409Z * Caching service dependencies ... [ ok ]
2018-10-28T22:44:57.474152273Z Starting ssh service...
...
...
2018-10-28T22:46:13.432160734Z [INFO] AnnotationMBeanExporter - Registering beans for JMX exposure on startup
2018-10-28T22:46:13.744859424Z [INFO] TomcatWebServer - Tomcat started on port(s): 80 (http) with context path ''
2018-10-28T22:46:13.783230205Z [INFO] TodoApplication - Started TodoApplication in 57.209 seconds (JVM running for 70.815)
2018-10-28T22:46:14.887366993Z 2018-10-28 22:46:14.887 INFO 198 --- [p-nio-80-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-10-28T22:46:14.887637695Z [INFO] DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization started
2018-10-28T22:46:14.998479907Z [INFO] DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 111 ms

2018-10-28T22:49:20.572059062Z Sun Oct 28 22:49:20 GMT 2018 GET ======= /api/todolist =======
2018-10-28T22:49:25.850543080Z Sun Oct 28 22:49:25 GMT 2018 DELETE ======= /api/todolist/{4f41ab03-1b12-4131-a920-fe5dfec106ca} =======
2018-10-28T22:49:26.047126614Z Sun Oct 28 22:49:26 GMT 2018 GET ======= /api/todolist =======
2018-10-28T22:49:30.201740227Z Sun Oct 28 22:49:30 GMT 2018 POST ======= /api/todolist ======= Milk
2018-10-28T22:49:30.413468872Z Sun Oct 28 22:49:30 GMT 2018 GET ======= /api/todolist =======

```

When you are finished, you can check your results
against YOUR code in
[e2e-java-experience-in-app-service-linux-part-2/complete](https://github.com/Azure-Samples/e2e-java-experience-in-app-service-linux-part-2/tree/master/complete).

## Scale out the Spring TODO App

Scale out Java Web app using Azure CLI:

```bash
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 Boot](https://spring.io/projects/spring-boot),
[Spring Data for Cosmos DB](https://docs.microsoft.com/en-us/java/azure/spring-framework/configure-spring-boot-starter-java-app-with-cosmos-db?view=azure-java-stable),
[Azure Cosmos DB](https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-introduction)
and
[App Service Linux](https://docs.microsoft.com/en-us/azure/app-service/containers/app-service-linux-intro).
.

## Resources

- [Java in App Service Linux dev guide](https://docs.microsoft.com/en-us/azure/app-service/containers/app-service-linux-java)
- [Azure for Java Developers](https://docs.microsoft.com/en-us/java/azure/)

---

This project has adopted
the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [opencode@microsoft.com](mailto:opencode@microsoft.com)
with any additional
questions or comments.