Skip to content

Commit 0857c98

Browse files
authored
Merge pull request #4 from galiacheng/main
Update the sample to support mssql.
2 parents 2c7e28c + e173abd commit 0857c98

33 files changed

Lines changed: 6685 additions & 137 deletions

File tree

README.md

Lines changed: 97 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
This Java Web Application is not Microservices Application but standard Java Web Application which is wrote by using Java EE 8 technologies.
77

88
At first, you can select and expand the Continent (North America, Asia, Africa, Europe, South America, Oceania, Antarctica) in the left side of the menu, then you can see the countries in the Continent. Then you can select the one country, then you can see the cities where has the number of the population over 1,000,000 in right side of the screen like follows.
9-
All of the data is coming from Managed [Azure Database for MySQL](https://docs.microsoft.com/azure/mysql/?WT.mc_id=docs-github-yoterada).
10-
9+
Data is coming from Managed [Azure Database for MySQL](https://docs.microsoft.com/azure/mysql/?WT.mc_id=docs-github-yoterada) or Managed [Azure SQL Database](https://learn.microsoft.com/en-us/azure/azure-sql/database/sql-database-paas-overview?view=azuresql).
1110

1211
![](./images/screenshot.jpg)
1312

14-
1513
## Runtime selection of Azure App Service on Linux for Java
1614

1715
In order to confirm which Java Runtime we can use on [Azure App Service on Linux](https://docs.microsoft.com/azure/app-service/containers/app-service-linux-intro?WT.mc_id=docs-github-yoterada), you can execute the following command.
@@ -60,13 +58,28 @@ OpenJDK 64-Bit Server VM 19.4-(Zulu-11.31+11-linux-musl-x64)-Microsoft-Azure-res
6058
```
6159
[Java long-term support and medium-term support on Azure and Azure Stack](https://docs.microsoft.com/azure/developer/java/fundamentals/java-jdk-long-term-support?WT.mc_id=docs-github-yoterada)
6260

63-
## Setup MySQL Server before creating the Java Web App
6461

65-
In order to run this application, you need to install and configure the [Azure Database for MySQL ](https://docs.microsoft.com/azure/mysql/?WT.mc_id=docs-github-yoterada) before the deploy.
66-
In order to install and create the MySQL, please refer to the following documents?
62+
> [!NOTE]
63+
> The following sections include step by step guide of how to create a Web App project and how to create the Java application.
64+
> You can also jump into subfolder *[java-webapp-with-mysql](java-webapp-with-mysql)* and *[java-webapp-with-mssql](java-webapp-with-mssql)* to run the application for a quick test.
65+
66+
67+
## Setup Database before creating the Java Web App
68+
69+
In order to run this application, you need to install and configure the database before the deploy.
70+
71+
This application can be run with both MySQL and Azure SQL. You can set up one of them per your requirement and configure corresponding setting in Web Application.
72+
73+
### Setup Azure Database for MySQL
74+
75+
In order to install and create the MySQL, please refer to the following documents.
6776

6877
[Create DB for MySQL on Azure](https://github.com/yoshioterada/microprofile-samples/blob/master/MySQL/Azure-MySQL-Setup-For-Sample-App.md) for preparation of this Java Web App.
6978

79+
### Setup Azure SQL database
80+
81+
Follow [Setup Azure SQL database](java-webapp-with-mssql/AzureSQL-Setup-For-Sample-App.md) to create database and import world data.
82+
7083

7184
## Create Maven Project for Java Web App
7285

@@ -141,6 +154,8 @@ The above directory structure will be automatically created and it is created to
141154

142155
### Modify
143156

157+
The following pom.xml file is for MySQL.
158+
144159
```
145160
<?xml version="1.0" encoding="UTF-8"?>
146161
@@ -250,6 +265,26 @@ The above directory structure will be automatically created and it is created to
250265
251266
```
252267

268+
For Azure SQL, replace MySQL dependency:
269+
270+
```xml
271+
<dependency>
272+
<groupId>mysql</groupId>
273+
<artifactId>mysql-connector-java</artifactId>
274+
<version>8.0.21</version>
275+
</dependency>
276+
```
277+
278+
With SQL dependency:
279+
280+
```xml
281+
<dependency>
282+
<groupId>com.microsoft.sqlserver</groupId>
283+
<artifactId>mssql-jdbc</artifactId>
284+
<version>12.6.1.jre11</version>
285+
</dependency>
286+
```
287+
253288
## Modify web.xml file
254289

255290
```xml
@@ -291,42 +326,59 @@ In order to deploy the Java Web App to Azure App Service, if you configure the [
291326
Please execute the following command?
292327

293328
```azurecli
294-
mvn com.microsoft.azure:azure-webapp-maven-plugin:1.9.1:config
329+
mvn com.microsoft.azure:azure-webapp-maven-plugin:2.13.0:config
295330
```
296331

297332
If you execute the command, it will show following question. If you answer all of the qeustion, the plugin will automatically added the configuration to deploy to Azure.
298333

299334
```azurecli
300-
$ mvn com.microsoft.azure:azure-webapp-maven-plugin:1.9.1:config
301-
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
302-
[INFO] Scanning for projects...
303-
[INFO]
304-
[INFO] -----------< com.microsoft.azure.samples:azure-javaweb-app >------------
305-
[INFO] Building azure-javaweb-app 0.0.1-SNAPSHOT
306-
[INFO] --------------------------------[ war ]---------------------------------
307-
[INFO]
308-
[INFO] --- azure-webapp-maven-plugin:1.9.1:config (default-cli) @ azure-javaweb-app ---
309-
310-
Define value for OS(Default: Linux):
311-
1. linux [*]
312-
2. windows
313-
3. docker
314-
Enter index to use: 1
315-
Define value for javaVersion(Default: Java 8):
316-
1. Java 11
317-
2. Java 8 [*]
318-
Enter index to use: 1
319-
Define value for runtimeStack(Default: TOMCAT 8.5):
320-
1. TOMCAT 9.0
321-
2. TOMCAT 8.5 [*]
322-
Enter index to use: 1
335+
$ mvn com.microsoft.azure:azure-webapp-maven-plugin:2.13.0:config
336+
Create new run configuration (Y/N) [Y]: y
337+
Define value for OS [Linux]:
338+
1: Windows
339+
* 2: Linux
340+
3: Docker
341+
Enter your choice: 2
342+
Define value for javaVersion [Java 17]:
343+
* 1: Java 17
344+
2: Java 11
345+
3: Java 8
346+
Enter your choice: 2
347+
Define value for webContainer [Tomcat 10.0]:
348+
* 1: Tomcat 10.0
349+
2: Tomcat 9.0
350+
3: Tomcat 8.5
351+
4: Jbosseap 7
352+
Enter your choice: 2
353+
Define value for pricingTier [P1v2]:
354+
1: D1
355+
2: B3
356+
* 3: P1v2
357+
4: P1v3
358+
5: P2v2
359+
6: P2v3
360+
7: P3v2
361+
8: P3v3
362+
9: B1
363+
10: B2
364+
11: F1
365+
12: S1
366+
13: S2
367+
14: S3
368+
15: EP3
369+
16: EP2
370+
17: EP1
371+
18: Y1
372+
19: FC1
373+
Enter your choice: 3
323374
Please confirm webapp properties
324-
AppName : azure-javaweb-app-1595955014168
325-
ResourceGroup : azure-javaweb-app-1595955014168-rg
375+
AppName : azure-javaweb-app-1709710313394
376+
ResourceGroup : azure-javaweb-app-1709710313394-rg
326377
Region : westeurope
327-
PricingTier : PremiumV2_P1v2
378+
PricingTier : P1v2
328379
OS : Linux
329-
RuntimeStack : TOMCAT 9.0-java11
380+
Java Version: Java 11
381+
Web server stack: Tomcat 9.0
330382
Deploy to slot : false
331383
Confirm (Y/N)? : Y
332384
[INFO] Saving configuration to pom.
@@ -344,11 +396,11 @@ After finished the command, you will be able to see following additional configu
344396
<plugin>
345397
<groupId>com.microsoft.azure</groupId>
346398
<artifactId>azure-webapp-maven-plugin</artifactId>
347-
<version>1.9.1</version>
399+
<version>2.13.0</version>
348400
<configuration>
349401
<schemaVersion>V2</schemaVersion>
350-
<resourceGroup>azure-javaweb-app-1595955014168-rg</resourceGroup>
351-
<appName>azure-javaweb-app-1595955014168</appName>
402+
<resourceGroup>azure-javaweb-app-1709710313394-rg</resourceGroup>
403+
<appName>azure-javaweb-app-1709710313394</appName>
352404
<pricingTier>P1v2</pricingTier>
353405
<region>westeurope</region>
354406
<runtime>
@@ -374,8 +426,8 @@ After finished the command, you will be able to see following additional configu
374426

375427
The above configuration will create and deploy to the following environment.
376428

377-
* Azure Resoure Group : `azure-javaweb-app-1595955014168-rg`
378-
* Azure Resource Name : `azure-javaweb-app-1595955014168`
429+
* Azure Resoure Group : `azure-javaweb-app-1709710313394-rg`
430+
* Azure Resource Name : `azure-javaweb-app-1709710313394`
379431
* Azure Resoruce Location : `westeurope`
380432

381433
If you would like to change the Resource Group Name, Resource Name and Location, you can change the configuration like follows.
@@ -460,7 +512,7 @@ $ mvn clean package azure-webapp:deploy
460512
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ azure-javaweb-app ---
461513
[INFO] Deleting /Users/yoterada/azure-javaweb-app/target
462514
463-
[INFO] --- azure-webapp-maven-plugin:1.9.1:deploy (default-cli) @ azure-javaweb-app ---
515+
[INFO] --- azure-webapp-maven-plugin:2.13.0:deploy (default-cli) @ azure-javaweb-app ---
464516
[INFO] Auth Type : AZURE_CLI, Auth Files : [/Users/yoterada/.azure/azureProfile.json, /Users/yoterada/.azure/accessTokens.json]
465517
[INFO] [Correlation ID: 3a3d53b9-7f39-4e46-b42f-3d4c91ed34df] Instance discovery was successful
466518
[INFO] Subscription : Microsoft Azure Internal Billing-CDA(f77aafe8-6be4-4d3d-bd9c-d0c37687ef70)
@@ -506,7 +558,7 @@ Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
506558
[INFO] Building azure-javaweb-app 0.0.1-SNAPSHOT
507559
[INFO] --------------------------------[ war ]---------------------------------
508560
[INFO]
509-
[INFO] --- azure-webapp-maven-plugin:1.9.1:config (default-cli) @ azure-javaweb-app ---
561+
[INFO] --- azure-webapp-maven-plugin:2.13.0:config (default-cli) @ azure-javaweb-app ---
510562
Please choose which part to config
511563
1. Application
512564
2. Runtime
@@ -1161,13 +1213,10 @@ In order to use it, I added the `p:dataTable` tag in index.xtml.
11611213

11621214

11631215

1164-
## Final Directory Structure of this Project
1216+
## Final Directory Structure of this application
11651217

11661218
```text
11671219
.
1168-
├── README.md
1169-
├── images
1170-
│   └── screenshot.jpg
11711220
├── pom.xml
11721221
└── src
11731222
├── main
@@ -1200,96 +1249,8 @@ In order to use it, I added the `p:dataTable` tag in index.xtml.
12001249
└── java
12011250
```
12021251

1252+
## Database Settings in the Web Application
12031253

1254+
For MySQL, see [MySQL Settings in the Web Application](java-webapp-with-mysql/MySQL-Settings-For-Sample-App.md).
12041255

1205-
## MySQL Settings in the Web Application
1206-
1207-
1208-
### Configure the environment variable values in Application settings
1209-
1210-
```azurecli
1211-
az webapp config appsettings set \
1212-
--resource-group WebApp \
1213-
--name yoshiowebapp \
1214-
--settings JDBC_DRIVER="com.mysql.cj.jdbc.Driver"
1215-
```
1216-
1217-
```azurecli
1218-
az webapp config appsettings set \
1219-
--resource-group WebApp \
1220-
--name yoshiowebapp \
1221-
--settings JDBC_URL="jdbc:mysql://my-mysqlserver.mysql.database.azure.com:3306/world?useSSL=true&requireSSL=false&serverTimezone=JST"
1222-
```
1223-
1224-
```azurecli
1225-
# user should be string like: azureuser@my-mysqlserver
1226-
az webapp config appsettings set \
1227-
--resource-group WebApp \
1228-
--name yoshiowebapp \
1229-
--settings DB_USER="USER"
1230-
```
1231-
1232-
```azurecli
1233-
az webapp config appsettings set \
1234-
--resource-group WebApp \
1235-
--name yoshiowebapp \
1236-
--settings DB_PASSWORD="PASSWORD"
1237-
```
1238-
1239-
```azurecli
1240-
$ az webapp config appsettings list --name yoshiowebapp -g WebApp
1241-
[
1242-
{
1243-
"name": "JDBC_DRIVER",
1244-
"slotSetting": false,
1245-
"value": "com.mysql.cj.jdbc.Driver"
1246-
},
1247-
{
1248-
"name": "JDBC_URL",
1249-
"slotSetting": false,
1250-
"value": "jdbc:mysql://my-mysqlserver.mysql.database.azure.com:3306/world?useSSL=true&requireSSL=false&serverTimezone=JST"
1251-
},
1252-
{
1253-
"name": "DB_USER",
1254-
"slotSetting": false,
1255-
"value": "azureuser@my-mysqlserver"
1256-
},
1257-
{
1258-
"name": "DB_PASSWORD",
1259-
"slotSetting": false,
1260-
"value": "mypassword"
1261-
}
1262-
]
1263-
```
1264-
1265-
### Configure in the persistence.xml
1266-
1267-
```xml
1268-
<?xml version="1.0" encoding="UTF-8"?>
1269-
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
1270-
<persistence-unit name="JPAWorldDatasourcePU" transaction-type="RESOURCE_LOCAL">
1271-
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
1272-
<class>com.microsoft.azure.samples.entities.City</class>
1273-
<class>com.microsoft.azure.samples.entities.Country</class>
1274-
<exclude-unlisted-classes>true</exclude-unlisted-classes>
1275-
<properties>
1276-
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
1277-
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://my-mysqlserver.mysql.database.azure.com:3306/world?useSSL=true&amp;requireSSL=false&amp;serverTimezone=JST"/>
1278-
<property name="javax.persistence.jdbc.user" value="USER"/>
1279-
<property name="javax.persistence.jdbc.password" value="PASSWORD"/>
1280-
1281-
<property name="eclipselink.cache.shared.default" value="false" />
1282-
<!-- <property name="eclipselink.ddl-generation" value="create-tables" />
1283-
<property name="eclipselink.ddl-generation.output-mode"
1284-
value="database" /> -->
1285-
<property name="eclipselink.logging.level" value="SEVERE" />
1286-
</properties>
1287-
</persistence-unit>
1288-
</persistence>
1289-
1290-
```
1291-
1292-
***Note:***
1293-
In the *jdbc.url* you need to escape the "&" as "&amp";
1294-
1295-
1256+
For Azure SQL, see [Azure SQL Settings in the Web Application](java-webapp-with-mssql/AzureSQL-Settings-For-Sample-App.md).

0 commit comments

Comments
 (0)