使用 Java 和 Eclipse 创建第一个函数(预览版)Create your first function with Java and Eclipse (Preview)

Note

用于 Azure Functions 的 Java 当前为预览版。Java for Azure Functions is currently in preview.

本文介绍如何使用 Eclipse IDE 和 Apache Maven 创建无服务器函数项目、对其进行测试和调试,然后将其部署到 Azure Functions。This article shows you how to create a serverless function project with the Eclipse IDE and Apache Maven, test and debug it, then deploy it to Azure Functions.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

设置开发环境Set up your development environment

若要使用 Java 和 Eclipse 开发函数应用,必须安装以下软件:To develop a functions app with Java and Eclipse, you must have the following installed:

Important

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

我们强烈建议安装 Azure Functions Core Tools 版本 2,它为运行和调试 Azure Functions 提供本地环境。It's highly recommended to also install Azure Functions Core Tools, version 2, which provide a local environment for running and debugging Azure Functions.

创建 Functions 项目Create a Functions project

  1. 在 Eclipse 中,依次选择“文件”菜单、“项目”。In Eclipse, select the File menu, then select Project.
  2. 在“新建项目”窗口中打开“Java 项目”文件夹,然后依次选择“Maven 项目”、“下一步”。Open the Java Project folder in the New Project window and select Maven Project, then select Next.
  3. 接受“新建 Maven 项目”对话框中的默认设置,然后选择“下一步”。Accept the defaults in the New Maven Project dialogue and select Next.
  4. 选择“添加原型”,并为 azure-functions-archetype 添加条目。Select Add Archetype and add the entries for the azure-functions-archetype.
    • 原型组 ID:com.microsoft.azureArchetype Group ID: com.microsoft.azure
    • 原型项目 ID:azure-functions-archetypeArchetype Artifact ID: azure-functions-archetype
    • 版本:使用中心存储库中的最新版本 Eclipse Maven 创建Version: Use latest version from the central repository Eclipse Maven create
  5. 单击“确定”并输入当前项目的详细信息,最后单击“完成”。Click OK and enter details for current project, and eventually Finish.

Maven 在新文件夹中创建名为 artifactId 的项目文件。Maven creates the project files in a new folder with a name of artifactId. 项目中生成的代码是一个简单的回显触发 HTTP 请求正文的 HTTP 触发函数。The generated code in the project is a simple HTTP triggered function that echoes the body of the triggering HTTP request.

在 IDE 本地运行函数Run functions locally in the IDE

Note

必须安装 Azure Functions Core Tools 版本 2,才能在本地运行和调试函数。Azure Functions Core Tools, version 2 must be installed to run and debug functions locally.

  1. 右键单击生成的项目,然后选择“运行方式”和“Maven 生成”。Right-click on the generated project, then choose Run As and Maven build.
  2. 在“编辑配置”对话框中的“目标”和“名称”字段内输入 package,然后选择“运行”。In the Edit Configuration dialog, Enter package in the Goals and Name fields, then select Run. 这会生成并打包函数代码。This will build and package the function code.
  3. 生成完成后,使用 azure-functions:run 作为目标和名称,如上所述创建另一个“运行”配置。Once the build is complete, create another Run configuration as above, using azure-functions:run as the goal and name. 选择“运行”,在 IDE 中运行函数。Select Run to run the function in the IDE.

完成函数测试后,在控制台窗口中终止运行时。Terminate the runtime in the console window when you're done testing your function. 一次只能有一个函数主机处于活动状态并在本地运行。Only one function host can be active and running locally at a time.

在 Eclipse 中调试函数Debug the function in Eclipse

在上一步骤的“运行方式”配置设置中,将 azure-functions:run 更改为 mvn azure-functions:run -DenableDebug 并运行更新的配置,以便在调试模式下启动函数应用。In your Run As configuration set up in the previous step, change azure-functions:run to mvn azure-functions:run -DenableDebug and run the updated configuration to start the function app in debug mode.

选择“运行”菜单并打开“调试配置”。Select the Run menu and open Debug Configurations. 选择“远程 Java 应用程序”并创建新配置。Choose Remote Java Application and create a new one. 为配置命名,并填写设置。Give your configuration a name and fill in the settings. 端口应与函数主机打开的调试端口一致,默认为 5005The port should be consistent with the debug port opened by function host, which by default is 5005. 设置后,单击 Debug 开始调试。After setup, click on Debug to start debugging.

在 Eclipse 中调试函数

使用 IDE 设置断点并检查函数中的对象。Set breakpoints and inspect objects in your function using the IDE. 完成后,停止调试程序和正在运行的函数主机。When finished, stop the debugger and the running function host. 一次只能有一个函数主机处于活动状态并在本地运行。Only one function host can be active and running locally at a time.

将函数部署到 AzureDeploy the function to Azure

部署到 Azure Functions 的过程中会使用 Azure CLI 中的帐户凭据。The deploy process to Azure Functions uses account credentials from the Azure CLI. 在继续使用计算机的命令提示符之前,请使用 Azure CLI 进行登录Log in with the Azure CLI before continuing using your computer's command prompt.

az login

使用新“运行方式”配置中的 azure-functions:deploy Maven 目标,将代码部署到新的函数应用。Deploy your code into a new Function app using the azure-functions:deploy Maven goal in a new Run As configuration.

部署完成后,将显示可用于访问你的 Azure 函数应用的 URL:When the deploy is complete, you see the URL you can use to access your Azure function app:

[INFO] Successfully deployed Function App with package.
[INFO] Deleting deployment package from Azure Storage...
[INFO] Successfully deleted deployment package fabrikam-function-20170920120101928.20170920143621915.zip
[INFO] Successfully deployed Function App at https://fabrikam-function-20170920120101928.chinacloudsites.cn
[INFO] ------------------------------------------------------------------------

后续步骤Next steps

  • 有关开发 Java 函数的详细信息,请查看 Java 函数开发人员指南Review the Java Functions developer guide for more information on developing Java functions.
  • 使用 azure-functions:add Maven 目标将具有不同触发器的其他函数添加到你的项目。Add additional functions with different triggers to your project using the azure-functions:add Maven target.