Skip to content

Commit bb2df39

Browse files
edburnsggailey777
andcommitted
Upgraded Acrolinx. Conformed to style guide.
Update articles/azure-functions/functions-create-first-quarkus.md On branch 20230113-edburns-msft-dd-1617770-functions-quarkus-tutorial-backup Address issues from @ggailey777 modified: articles/azure-functions/TOC.yml modified: articles/azure-functions/functions-create-first-quarkus.md modified: articles/azure-functions/index.yml modified: articles/azure-functions/media/functions-create-first-quarkus/portal-metrics.png modified: articles/azure-functions/media/functions-create-first-quarkus/portal-metrics.png modified: articles/azure-functions/media/functions-quarkus-tutorial/azure-function-app-detail.png modified: articles/azure-functions/media/functions-quarkus-tutorial/azure-function-app-ready.png modified: articles/azure-functions/media/functions-quarkus-tutorial/azure-function-app.png Co-authored-by: Glenn Gailey <[email protected]> Signed-off-by: Ed Burns <[email protected]>
1 parent 49e681b commit bb2df39

File tree

7 files changed

+326
-1
lines changed

7 files changed

+326
-1
lines changed

articles/azure-functions/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@
326326
href: ./functions-create-your-first-function-visual-studio.md
327327
- name: Visual Studio Code
328328
href: ./create-first-function-vs-code-csharp.md
329+
- name: Java with Quarkus
330+
href: functions-create-first-quarkus.md
329331
- name: Java using Gradle
330332
href: functions-create-first-java-gradle.md
331333
- name: Java using Eclipse
Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
---
2+
title: Deploy Serverless Java Apps with Quarkus on Azure Functions
3+
description: Deploy Serverless Java Apps with Quarkus on Azure Functions
4+
ms.author: edburns
5+
ms.service: azure-functions
6+
ms.topic: how-to
7+
ms.date: 01/10/2023
8+
ms.devlang: java
9+
ms.custom: devx-track-java, devx-track-javaee, devx-track-javaee-quarkus-functions, devx-track-javaee-quarkus-functions
10+
---
11+
12+
# Deploy Serverless Java Apps with Quarkus on Azure Functions
13+
14+
In this article, you'll develop, build, and deploy a serverless Java app with Quarkus on Azure Functions. This article uses Quarkus Funqy and its built-in support for Azure Functions HTTP trigger for Java. Using Quarkus with Azure Functions gives you the power of the Quarkus programming model with the scale and flexibility of Azure Functions. When you're finished, you'll run serverless [Quarkus](https://quarkus.io) applications on Azure Functions and continuing to monitor the application on Azure.
15+
16+
## Prerequisites
17+
18+
* [Azure CLI](/cli/azure/overview), installed on your own computer.
19+
* [An Azure Account](https://azure.microsoft.com/)
20+
* [Java JDK 17](/azure/developer/java/fundamentals/java-support-on-azure) with JAVA_HOME configured appropriately. This article was written with Java 17 in mind, but Azure functions and Quarkus support older versions of Java as well.
21+
* [Apache Maven 3.8.1+](https://maven.apache.org)
22+
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
23+
24+
## A first look at the sample application
25+
26+
Clone the sample code for this guide. The sample is on [GitHub](https://github.com/Azure-Samples/quarkus-azure).
27+
28+
Explore the sample function. Open the file *functions-quarkus/src/main/java/io/quarkus/GreetingFunction.java*. The `@Funq` annotation makes your method (e.g. `funqyHello`) a serverless function. Azure Functions Java has its own set of Azure-specific annotations, but these annotations are not necessary when using Quarkus on Azure Functions in a simple capacity as we're doing here. For more information on the Azure Functions Java annotations, see [Azure Functions Java developer guide](/azure/azure-functions/functions-reference-java).
29+
30+
```java
31+
@Funq
32+
public String funqyHello() {
33+
return "hello funqy";
34+
}
35+
```
36+
37+
Unless you specify otherwise, the function's name is taken to be same as the method name. You can also define the function name with a parameter to the annotation, as shown here.
38+
39+
```java
40+
@Funq("alternateName")
41+
public String funqyHello() {
42+
return "hello funqy";
43+
}
44+
```
45+
46+
The name is important: the name becomes a part of the REST URI to invoke the function, as shown later in the article.
47+
48+
## Test the Serverless Function locally
49+
50+
Use `mvn` to run `Quarkus Dev mode` on your local terminal. Running Quarkus in this way enables live reload with background compilation. When you modify your Java files and/or your resource files and refresh your browser, these changes will automatically take effect.
51+
52+
A browser refresh triggers a scan of the workspace. If any changes are detected, the Java files are recompiled and the application is redeployed. Your redeployed application services the request. If there are any issues with compilation or deployment an error page will let you know.
53+
54+
Quarkus dev mode listens for a debugger on port `5005`. If you want to wait for the debugger to attach before running you can pass `-Dsuspend` on the command line. When you don’t want the debugger at all, use `-Ddebug=false`.
55+
56+
Replace `yourResourceGroupName` with a resource group name. Resource group names must be globally unique within a subscription. For this reason, consider prepending some unique identifier to any names you create that must be unique. A useful technique is to use your initials followed by today's date in `mmdd` format. The resourceGroup is not necessary for this part of the instructions, but it's required later. For simplicity, the maven project requires the property be defined.
57+
58+
1. Invoke Quarkus dev mode.
59+
60+
```bash
61+
cd functions-azure
62+
mvn -DskipTests -DresourceGroup=<yourResourceGroupName> quarkus:dev
63+
```
64+
65+
The output should look like this.
66+
67+
```output
68+
...
69+
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
70+
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
71+
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
72+
INFO [io.quarkus] (Quarkus Main Thread) quarkus-azure-function 1.0-SNAPSHOT on JVM (powered by Quarkus xx.xx.xx.) started in 1.290s. Listening on: http://localhost:8080
73+
74+
INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
75+
INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, funqy-http, smallrye-context-propagation, vertx]
76+
77+
--
78+
Tests paused
79+
Press [r] to resume testing, [o] Toggle test output, [:] for the terminal, [h] for more options>
80+
```
81+
82+
1. Access the function using the `CURL` command on your local terminal.
83+
84+
```bash
85+
curl localhost:8080/api/funqyHello
86+
```
87+
88+
The output should look like this.
89+
90+
```output
91+
"hello funqy"
92+
```
93+
94+
### Add Dependency injection to function
95+
96+
Dependency injection in Quarkus is provided by the open standard technology Jakarta EE Contexts and Dependency Injection (CDI). For a high level overview on injection in general, and CDI in specific, see the [Jakarta EE tutorial](https://eclipse-ee4j.github.io/jakartaee-tutorial/#injection).
97+
98+
1. Add a new function that uses dependency injection
99+
100+
Create a *GreetingService.java* file in the *functions-quarkus/src/main/java/io/quarkus* directory. Make the source code of the file be the following.
101+
102+
```java
103+
package io.quarkus;
104+
105+
import javax.enterprise.context.ApplicationScoped;
106+
107+
@ApplicationScoped
108+
public class GreetingService {
109+
110+
public String greeting(String name) {
111+
return "Welcome to build Serverless Java with Quarkus on Azure Functions, " + name;
112+
}
113+
114+
}
115+
```
116+
117+
Save the file.
118+
119+
`GreetingService` is an injectable bean that implements a `greeting()` method returning a string `Welcome...` message with a parameter `name`.
120+
121+
1. Open the existing the *functions-quarkus/src/main/java/io/quarkus/GreetingFunction.java* file. Replace the class with the below code to add a new field `gService` and method `greeting`.
122+
123+
```java
124+
package io.quarkus;
125+
126+
import javax.inject.Inject;
127+
import io.quarkus.funqy.Funq;
128+
129+
public class GreetingFunction {
130+
131+
@Inject
132+
GreetingService gService;
133+
134+
@Funq
135+
public String greeting(String name) {
136+
return gService.greeting(name);
137+
}
138+
139+
@Funq
140+
public String funqyHello() {
141+
return "hello funqy";
142+
}
143+
144+
}
145+
```
146+
147+
Save the file.
148+
149+
1. Access the new function `greeting` using the `CURL` command on your local terminal.
150+
151+
```bash
152+
curl -d '"Dan"' -X POST localhost:8080/api/greeting
153+
```
154+
155+
The output should should look like this.
156+
157+
```output
158+
"Welcome to build Serverless Java with Quarkus on Azure Functions, Dan"
159+
```
160+
161+
> [!IMPORTANT]
162+
> `Live Coding` (also referred to as dev mode) allows you to run the app and make changes on the fly. Quarkus will automatically re-compile and reload the app when changes are made. This is a powerful and efficient style of developing that you'll use throughout the tutorial.
163+
164+
Before moving forward to the next step, stop Quarkus Dev Mode by pressing `CTRL-C`.
165+
166+
## Deploy the Serverless App to Azure Functions
167+
168+
1. If you haven't already, sign in to your Azure subscription by using the [az login](/cli/azure/reference-index) command and follow the on-screen directions.
169+
170+
```azurecli
171+
az login
172+
```
173+
174+
> [!NOTE]
175+
> If you've multiple Azure tenants associated with your Azure credentials, you must specify which tenant you want to sign in to. You can do this with the `--tenant` option. For example, `az login --tenant contoso.onmicrosoft.com`.
176+
> Continue the process in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
177+
178+
Once you've signed in successfully, the output on your local terminal should look similar to the following.
179+
180+
```output
181+
xxxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxxx 'Microsoft'
182+
[
183+
{
184+
"cloudName": "AzureCloud",
185+
"homeTenantId": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxx",
186+
"id": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
187+
"isDefault": true,
188+
"managedByTenants": [],
189+
"name": "Contoso account services",
190+
"state": "Enabled",
191+
"tenantId": "xxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxx",
192+
"user": {
193+
"name": "[email protected]",
194+
"type": "user"
195+
}
196+
}
197+
]
198+
```
199+
200+
1. Build and deploy the functions to Azure
201+
202+
The *pom.xml* you generated in the previous step uses the `azure-functions-maven-plugin`. Running `mvn install` generates config files and a staging directory required by the `azure-functions-maven-plugin`. For `yourResourceGroupName`, use the value you used previously.
203+
204+
```bash
205+
mvn clean install -DskipTests -DtenantId=<your tenantId from shown previously> -DresourceGroup=<yourResourceGroupName> azure-functions:deploy
206+
```
207+
208+
1. During deployment, sign in to Azure. The `azure-functions-maven-plugin` is configured to prompt for Azure sign in each time the project is deployed. Examine the build output. During the build, you'll see output similar to the following.
209+
210+
```output
211+
[INFO] Auth type: DEVICE_CODE
212+
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code AXCWTLGMP to authenticate.
213+
```
214+
215+
Do as the output says and authenticate to Azure using the browser and provided device code. Many other authentication and configuration options are available. The complete reference documentation for `azure-functions-maven-plugin` is available at [Azure Functions: Configuration Details](https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details).
216+
217+
1. After authenticating, the build should continue and complete. The output should include `BUILD SUCCESS` near the end.
218+
219+
```output
220+
Successfully deployed the artifact to https://quarkus-demo-123451234.azurewebsites.net
221+
```
222+
223+
You can also find the `URL` to trigger your function on Azure in the output log.
224+
225+
```output
226+
[INFO] HTTP Trigger Urls:
227+
[INFO] quarkus : https://quarkus-azure-functions-http-archetype-20220629204040017.azurewebsites.net/api/{*path}
228+
```
229+
230+
It will take a while for the deployment to complete. In the meantime, let's explore Azure Functions in the portal.
231+
232+
## Access and Monitor the Serverless Function on Azure
233+
234+
Sign in to the Portal and ensure you've selected the same tenant and subscription used in the Azure CLI. You can visit the portal at [https://aka.ms/publicportal](https://aka.ms/publicportal).
235+
236+
1. Type `Function App` in the search bar at the top of the Azure portal and press Enter. Your function should be deployed and show up with the name `<yourResourceGroupName>-function-quarkus`.
237+
238+
:::image type="content" source="media/functions-create-first-quarkus/azure-function-app.png" alt-text="The function app in the portal":::
239+
240+
Select the `function name`. you'll see the function app's detail information such as **Location**, **Subscription**, **URL**, **Metrics**, and **App Service Plan**.
241+
242+
1. In the detail page, select the `URL`.
243+
244+
:::image type="content" source="media/functions-create-first-quarkus/azure-function-app-detail.png" alt-text="The function app detail page in the portal":::
245+
246+
Then, you'll see if your function is "up and running" now.
247+
248+
:::image type="content" source="media/functions-create-first-quarkus/azure-function-app-ready.png" alt-text="The function welcome page":::
249+
250+
1. Invoke the `greeting` function using `CURL` command on your local terminal.
251+
252+
> [!IMPORTANT]
253+
> Replace `YOUR_HTTP_TRIGGER_URL` with your own function URL that you find in Azure portal or output.
254+
255+
```bash
256+
curl -d '"Dan on Azure"' -X POST https://YOUR_HTTP_TRIGGER_URL/api/greeting
257+
```
258+
259+
The output should look similar to the following.
260+
261+
```output
262+
"Welcome to build Serverless Java with Quarkus on Azure Functions, Dan on Azure"
263+
```
264+
265+
You can also access the other function (`funqyHello`).
266+
267+
```bash
268+
curl https://YOUR_HTTP_TRIGGER_URL/api/funqyHello
269+
```
270+
271+
The output should be the same as you observed above.
272+
273+
```output
274+
"hello funqy"
275+
```
276+
277+
If you want to exercise the basic metrics capability in the Azure portal, try invoking the function within a shell for loop, as shown here.
278+
279+
```bash
280+
for i in {1..100}; do curl -d '"Dan on Azure"' -X POST https://YOUR_HTTP_TRIGGER_URL/api/greeting; done
281+
```
282+
283+
After a while, you'll see some metrics data in the portal, as shown next.
284+
285+
:::image type="content" source="media/functions-create-first-quarkus/portal-metrics.png" alt-text="Function metrics in the portal":::
286+
287+
Now that you've opened your Azure function in the portal, here are some more features accessible from the portal.
288+
289+
* Monitor the performance of your Azure function. For more information, see [Monitoring Azure Functions](/azure/azure-functions/monitor-functions).
290+
* Explore telemetry. For more information, see [Analyze Azure Functions telemetry in Application Insights](/azure/azure-functions/analyze-telemetry-data).
291+
* Set up logging. For more information, see [Enable streaming execution logs in Azure Functions](/azure/azure-functions/streaming-logs).
292+
293+
## Clean up resources
294+
295+
If you don't need these resources, you can delete them by running the following command in the Cloud Shell or on your local terminal:
296+
297+
```azurecli
298+
az group delete --name <yourResourceGroupName> --yes
299+
```
300+
301+
## Next steps
302+
303+
In this guide, you learned how to:
304+
> [!div class="checklist"]
305+
>
306+
> * Run Quarkus dev mode
307+
> * Deploy a Funqy app to Azure functions using the `azure-functions-maven-plugin`
308+
> * Examine the performance of the function in the portal
309+
310+
To learn more about Azure Functions and Quarkus, see the following articles and references.
311+
312+
* [Azure Functions Java developer guide](/azure/azure-functions/functions-reference-java)
313+
* [Quickstart: Create a Java function in Azure using Visual Studio Code](/azure/azure-functions/create-first-function-vs-code-java)
314+
* [Azure Functions documentation](/azure/azure-functions/)
315+
* [Quarkus guide to deploying on Azure](https://quarkus.io/guides/deploying-to-azure-cloud)

articles/azure-functions/index.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,12 @@ landingContent:
6969
- text: Azure Serverless Community Library
7070
url: https://www.serverlesslibrary.net/
7171
- text: Azure Resource Manager templates
72-
url: https://azure.microsoft.com/resources/templates/?resourceType=Microsoft.Web&term=functions
72+
url: https://azure.microsoft.com/resources/templates/?resourceType=Microsoft.Web&term=functions
73+
- linkListType: tutorial
74+
links:
75+
- text: Functions with Logic Apps
76+
url: functions-twitter-email.md
77+
- text: Develop Python functions with VS Code
78+
url: create-first-function-vs-code-python.md
79+
- text: Create serverless APIs using Visual Studio
80+
url: openapi-apim-integrate-visual-studio.md
93.9 KB
Loading
104 KB
Loading
88 KB
Loading
74.4 KB
Loading

0 commit comments

Comments
 (0)