使用 Java 和 Gradle 创建函数并将其发布到 AzureUse Java and Gradle to create and publish a function to Azure

本文介绍如何使用 Gradle 命令行工具生成 Java 函数项目并将其发布到 Azure Functions。This article shows you how to build and publish a Java function project to Azure Functions with the 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.

备注

如果 Gradle 不是你的首选开发工具,请查看面向使用 MavenIntelliJ IDEAVS Code 的 Java 开发人员的类似教程。If Gradle is not your prefered development tool, check out our similar tutorials for Java developers using Maven, IntelliJ IDEA and VS Code.

先决条件Prerequisites

若要使用 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

使用以下命令克隆示例项目: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 = 'chinanorth2'
    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:

gradle jar --info
gradle azureFunctionsRun

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

...

Now listening on: http://0.0.0.0:7071
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 the following cURL command in a new terminal window:

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

预期输出如下所示:The expected output is the following:

Hello, AzureFunctions

备注

如果将 authLevel 设置为 FUNCTIONADMIN,则在本地运行时不需要函数密钥If you set authLevel to FUNCTION or ADMIN, 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.

gradle azureFunctionsDeploy

这会根据 build.gradle 文件中的值在 Azure 中创建以下资源:This creates the following resources in Azure, based on the values in the build.gradle file:

  • 资源组。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 Consumption plan 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.

示例项目中 HTTP 触发器的 authLevel 为 ANONYMOUS,它将跳过身份验证。The authLevel for HTTP Trigger in sample project is ANONYMOUS, which will skip the authentication. 但是,如果使用其他 authLevel(如 FUNCTIONADMIN),则需要获得函数密钥以通过 HTTP 调用函数终结点。However, if you use other authLevel like FUNCTION or ADMIN, 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, select Functions, choose your function, then click </> 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" http://azure-functions-sample-demo.chinacloudsites.cn/api/HttpExample --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...