快速入门:使用 Java 和 Maven/Gradle 创建函数并将其发布到 AzureQuickstart: Use Java and Maven/Gradle to create and publish a function to Azure

本文介绍如何使用 Maven/Gradle 命令行工具生成 Java 函数并将该函数发布到 Azure Functions。This article shows you how to build and publish a Java function to Azure Functions with the Maven/Gradle command-line tool. 完成后,函数代码会在 Azure 的无服务器托管计划中运行,并由 HTTP 请求触发。When you're done, your function code runs in Azure in a serverless hosting plan and is triggered by an HTTP request.


若要使用 Java 开发函数,必须安装以下软件:To develop functions using Java, you must have the following installed:

还需要一个有效的 Azure 订阅。You also need an active Azure subscription. 如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.


JAVA_HOME 环境变量必须设置为 JDK 的安装位置,以完成本快速入门。The JAVA_HOME environment variable must be set to the install location of the JDK to complete this quickstart.

准备 Functions 项目Prepare a Functions project

在空的文件夹中,运行以下命令以从 Maven archetype 生成 Functions 项目。In an empty folder, run the following command to generate the Functions project from a Maven archetype.

mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype 


如果使用的是 Powershell,请记得在参数的两侧添加 ""。If you're using Powershell, remeber to add "" around parameters.


如果在运行命令时遇到问题,请看看使用了什么 maven-archetype-plugin 版本。If you're experiencing issues with running the command, take a look at what maven-archetype-plugin version is used. 由于你是在空的没有 .pom 文件的目录中运行该命令,因此它会尝试使用 ~/.m2/repository/org/apache/maven/plugins/maven-archetype-plugin 中的旧版插件(如果你从旧版升级了 Maven)。Because you are running the command in an empty directory with no .pom file, it might be attempting to use a plugin of the older version from ~/.m2/repository/org/apache/maven/plugins/maven-archetype-plugin if you upgraded your Maven from an older version. 如果是这样,请尝试删除 maven-archetype-plugin 目录并重新运行命令。If so, try deleting the maven-archetype-plugin directory and re-running the command.

Maven 会请求你提供所需的值,以在部署上完成项目的生成。Maven asks you for values needed to finish generating the project on deployment. 系统提示时提供以下值:Provide the following values when prompted:

ValueValue 说明Description
groupIdgroupId 一个值,用于按照 Java 的包命名规则在所有项目中标识你的项目。A value that uniquely identifies your project across all projects, following the package naming rules for Java. 本快速入门中的示例使用 com.fabrikam.functionsThe examples in this quickstart use com.fabrikam.functions.
artifactIdartifactId 一个值,该值是 jar 的名称,没有版本号。A value that is the name of the jar, without a version number. 本快速入门中的示例使用 fabrikam-functionsThe examples in this quickstart use fabrikam-functions.
versionversion 选择默认值 1.0-SNAPSHOTChoose the default value of 1.0-SNAPSHOT.
packagepackage 一个值,该值是所生成函数代码的 Java 包。A value that is the Java package for the generated function code. 使用默认值。Use the default. 本快速入门中的示例使用 com.fabrikam.functionsThe examples in this quickstart use com.fabrikam.functions.
appNameappName 全局唯一名称,用于标识 Azure 中的新函数应用。Globally unique name that identifies your new function app in Azure. 请使用默认值,即 artifactId 追加一个随机数字。Use the default, which is the artifactId appended with a random number. 请记下该值,稍后需要它。Make a note of this value, you'll need it later.
appRegionappRegion 选择离你近或离函数访问的其他服务近的区域Choose a region near you or near other services your functions access. 默认为 chinanorthThe default is chinanorth. 请运行以下 Azure CLI 命令,获取所有区域的列表:Run this Azure CLI command to get a list of all regions:
az account list-locations --query '[].{Name:name}' -o tsv
resourceGroupresourceGroup 要在其中创建函数应用的新资源组的名称。Name for the new resource group in which to create your function app. 请使用 myResourceGroup(由本快速入门中的示例使用)。Use myResourceGroup, which is used by examples in this quickstart. 资源组必须对 Azure 订阅来说独一无二。A resource group must be unique to your Azure subscription.

键入 Y 或按 Enter 进行确认。Type Y or press Enter to confirm.

Maven 在名为 artifactId 的新文件夹(在此示例中为 fabrikam-functions)中创建项目文件。Maven creates the project files in a new folder with a name of artifactId, which in this example is fabrikam-functions. 运行以下命令,将目录切换到创建的项目文件夹。Run the following command to change the directory to the created project folder.

cd fabrikam-function

使用以下命令克隆示例项目:Use the following command to clone the sample project:

git clone https://github.com/Azure-Samples/azure-functions-samples-java.git
cd azure-functions-samples-java/

打开 build.gradle,将以下节中的 appName 更改为唯一名称,以免在部署到 Azure 时发生域名冲突。Open build.gradle and change the appName in the following section to a unique name to avoid domain name conflict when deploying to Azure.

azurefunctions {
    resourceGroup = 'java-functions-group'
    appName = 'azure-functions-sample-demo'
    pricingTier = 'Consumption'
    region = 'chinanorth'
    runtime {
      os = 'windows'
    localDebug = "transport=dt_socket,server=y,suspend=n,address=5005"

在文本编辑器中打开 src/main/java 路径中的新 Function.java 文件,查看生成的代码。Open the new Function.java file from the src/main/java path in a text editor and review the generated code. 该代码是一个 HTTP 触发的函数,用于回显请求的正文。This code is an HTTP triggered function that echoes the body of the request.

在本地运行函数Run the function locally

运行以下命令,以生成并运行函数项目:Run the following command to build then run the function project:

mvn clean package 
mvn azure-functions:run
gradle jar --info
gradle azureFunctionsRun

在本地运行项目时,会看到 Azure Functions Core Tools 的如下所示的输出:You will see output like the following from Azure Functions Core Tools when you run the project locally:


Now listening on:
Application started. Press Ctrl+C to shut down.

Http Functions:

    HttpExample: [GET,POST] http://localhost:7071/api/HttpExample

使用 cURL 在新的终端窗口中从命令行触发函数:Trigger the function from the command line using cURL in a new terminal window:

curl -w "\n" http://localhost:7071/api/HttpExample --data AzureFunctions
Hello AzureFunctions!

在本地运行时,不需要功能键The function key isn't required when running locally. 在终端中使用 Ctrl+C 停止函数代码。Use Ctrl+C in the terminal to stop the function code.

将函数部署到 AzureDeploy the function to Azure

首先部署函数应用时,会在 Azure 中创建函数应用和相关资源。A function app and related resources are created in Azure when you first deploy your function app. 在部署之前,必须使用 az login Azure CLI 命令登录到 Azure 订阅。Before you can deploy, use the az login Azure CLI command to sign in to your Azure subscription.

az login


如果帐户可以访问多个订阅,请使用 az account set 设置此会话的默认订阅。If your account can access multiple subscriptions, use az account set to set the default subscription for this session.

使用以下命令将项目部署到新的函数应用。Use the following command to deploy your project to a new function app.

mvn azure-functions:deploy
gradle azureFunctionsDeploy

这会在 Azure 中创建以下资源:This will create the following resources in Azure:

  • 资源组。Resource group. 使用你提供的 resourceGroup 命名。Named with the resourceGroup you supplied.
  • 存储帐户。Storage account. Functions 所需。Required by Functions. 此名称根据存储帐户名称要求随机生成。The name is generated randomly based on Storage account name requirements.
  • 应用服务计划。App service plan. 为指定 appRegion 中的函数应用进行的无服务器托管。Serverless hosting for your function app in the specified appRegion. 此名称随机生成。The name is generated randomly.
  • 函数应用。Function app. 函数应用是函数的部署和执行单元。A function app is the deployment and execution unit for your functions. 此名称是 appName 追加一个随机生成的数字。The name is your appName, appended with a randomly generated number.

此部署还会在启用“从包运行”模式的情况下,使用 zip deployment 将项目文件打包并部署到新函数应用。The deployment also packages the project files and deploys them to the new function app using zip deployment, with run-from-package mode enabled.

部署完成后,会显示可用于访问函数应用终结点的 URL。After the deployment completes, you see the URL you can use to access your function app endpoints. 由于我们发布的 HTTP 触发器使用 authLevel = AuthorizationLevel.FUNCTION,因此你需要获取函数密钥,以便通过 HTTP 调用函数终结点。Because the HTTP trigger we published uses authLevel = AuthorizationLevel.FUNCTION, you need to get the function key to call the function endpoint over HTTP. 若要获取函数密钥,最简单的方法是使用 Azure 门户The easiest way to get the function key is from the Azure portal.

获取 HTTP 触发器 URLGet the HTTP trigger URL

可以从 Azure 门户获取通过函数密钥触发函数所需的 URL。You can get the URL required to trigger your function, with the function key, from the Azure portal.

  1. 浏览到 Azure 门户,登录,在页面顶部将函数应用的 appName 键入“搜索”栏, 然后按 Enter。Browse to the Azure portal, sign in, type the appName of your function app into Search at the top of the page, and press enter.

  2. 在函数应用中展开“函数(只读)”,选择你的函数,然后选择右上角的“</> 获取函数 URL”。 In your function app, expand Functions (Read Only), choose your function, then select </> Get function URL at the top right.

    从 Azure 门户复制函数 URL

  3. 选择“默认(函数密钥)”,然后选择“复制” 。Choose default (Function key) and select Copy.

现在可以使用复制的 URL 来访问函数。You can now use the copied URL to access your function.

在 Azure 中验证函数Verify the function in Azure

若要使用 cURL 验证在 Azure 上运行的函数应用,请将以下示例中的 URL 替换为从门户复制的 URL。To verify the function app running on Azure using cURL, replace the URL from the sample below with the URL that you copied from the portal.

curl -w "\n" https://fabrikam-functions-20190929094703749.chinacloudsites.cn/api/HttpExample?code=zYRohsTwBlZ68YF.... --data AzureFunctions

这会向函数终结点发送一个 POST 请求,请求正文中包含 AzureFunctionsThis sends a POST request to the function endpoint with AzureFunctions in the body of the request. 会看到以下响应。You see the following response.

Hello AzureFunctions!

后续步骤Next steps

我们已使用 HTTP 触发的函数创建 Java 函数项目,在本地计算机上运行该项目,并将其部署到 Azure。You've created a Java functions project with an HTTP triggered function, run it on your local machine, and deployed it to Azure. 现在,通过以下方式扩展函数Now, extend your function by...