使用 Java 和 IntelliJ 创建你的第一个 Azure 函数(预览版)Create your first Azure function with Java and IntelliJ (preview)

Note

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

本文介绍:This article shows you:

  • 如何使用 IntelliJ IDEA 和 Apache Maven 创建无服务器函数项目How to create a serverless function project with IntelliJ IDEA and Apache Maven
  • 在自己的计算机上的集成开发环境 (IDE) 中测试和调试函数的步骤Steps for testing and debugging the function in the integrated development environment (IDE) on your own computer
  • 将函数项目部署到 Azure Functions 的说明Instructions for deploying the function project 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 和 IntelliJ 开发函数,请安装以下软件:To develop a function with Java and IntelliJ, install the following software:

Important

JAVA_HOME 环境变量必须设置为 JDK 的安装位置才能完成本文中的步骤。The JAVA_HOME environment variable must be set to the install location of the JDK to complete the steps in this article.

建议安装 Azure Functions Core Tools 版本 2We recommend that you install Azure Functions Core Tools, version 2. 它为编写、运行和调试 Azure Functions 提供了本地开发环境。It provides a local development environment for writing, running, and debugging Azure Functions.

创建 Functions 项目Create a Functions project

  1. 在 IntelliJ IDEA 中选择“Create New Project”(创建新项目)。In IntelliJ IDEA, select Create New Project.
  2. 在“New Project”(新建项目)窗口的左窗格中,选择“Maven”。In the New Project window, select Maven from the left pane.
  3. azure-functions-archetype 选中“Create from archetype”(通过 archetype 创建)复选框,然后选中“Add Archetype”(添加 Archetype)的复选框。Select the Create from archetype check box, and then select Add Archetype for the azure-functions-archetype.
  4. 在“Add Archetype”(添加 Archetype)窗口中完成相关字段,如下所示:In the Add Archetype window, complete the fields as follows:
    • GroupId:com.microsoft.azureGroupId: com.microsoft.azure
    • ArtifactId:azure-functions-archetypeArtifactId: azure-functions-archetype
    • Version(版本):使用中央存储库中的最新版本 在 IntelliJ IDEA 中根据 archetype 创建 Maven 项目Version: Use the latest version from the central repository Create a Maven project from archetype in IntelliJ IDEA
  5. 选择“确定”,然后选择“下一步”。Select OK, and then select Next.
  6. 输入当前项目的详细信息,然后选择“Finish”(完成)。Enter your details for current project, and select Finish.

Maven 在新文件夹中项目文件,该文件夹使用与 ArtifactId 值相同的名称。Maven creates the project files in a new folder with the same name as the ArtifactId value. 项目的生成代码是一个简单的 HTTP 触发的函数,回显触发 HTTP 请求的正文。The project's generated code 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 版本 2To run and debug functions locally, make sure you've installed Azure Functions Core Tools, version 2.

  1. 手动导入所做的更改,或者启用自动导入Import changes manually or enable auto import.

  2. 打开“Maven Projects”(Maven 项目)工具栏。Open the Maven Projects toolbar.

  3. 展开“Lifecycle”(生命周期),然后打开“package”(包)。Expand Lifecycle, and then open package. 此时会在新创建的目标目录中生成解决方案并将其打包。The solution is built and packaged in a newly created target directory.

  4. 展开“Plugins”(插件) > “azure-functions”,打开 azure-functions:run,以便启动 Azure Functions 本地运行时。Expand Plugins > azure-functions and open azure-functions:run to start the Azure Functions local runtime.
    Azure Functions 的 Maven 工具栏Maven toolbar for Azure Functions

  5. 完成函数测试后关闭运行对话框。Close the run dialog box when you're done testing your function. 一次只能有一个函数主机处于活动状态并在本地运行。Only one function host can be active and running locally at a time.

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

  1. 若要在调试模式下启动函数主机,请在运行函数时添加 -DenableDebug 作为参数。To start the function host in debug mode, add -DenableDebug as the argument when you run your function. 可以在 maven 目标中更改配置,或在终端窗口中运行以下命令:You can either change the configuration in maven goals or run the following command in a terminal window:

    mvn azure-functions:run -DenableDebug
    

    该命令导致函数主机打开调试端口 5005。This command causes the function host to open a debug port at 5005.

  2. 在“Run”(运行)菜单中选择“Edit Configurations”(编辑配置)。On the Run menu, select Edit Configurations.

  3. 选择“(+)”,添加“Remote”(远程)。Select (+) to add a Remote.

  4. 完成“Name”(名称)和“Settings”(设置)字段,然后选择“OK”(确定)以保存配置。Complete the Name and Settings fields, and then select OK to save the configuration.

  5. 在设置后,选择“Debug”(调试)>“Remote Configuration Name”(远程配置名称)或在键盘上按 Shift+F9 以启动调试。After setup, select Debug < Remote Configuration Name > or press Shift+F9 on your keyboard to start debugging.

    在 IntelliJ 中调试函数

  6. 完成后,停止调试器和正在运行的进程。When you're finished, stop the debugger and the running process. 一次只能有一个函数主机处于活动状态并在本地运行。Only one function host can be active and running locally at a time.

将函数部署到 AzureDeploy the function to Azure

  1. 在向 Azure 部署函数之前,必须使用 Azure CLI 登录Before you can deploy your function to Azure, you must log in by using the Azure CLI.

    az login
    
  2. 使用 azure-functions:deploy Maven 目标将代码部署到新的函数中。Deploy your code into a new function by using the azure-functions:deploy Maven target. 也可在“Maven Projects”(Maven 项目)窗口中选择“azure-functions:deploy”选项。You can also select the azure-functions:deploy option in the Maven Projects window.

    mvn azure-functions:deploy
    
  3. 成功部署函数后,请在 Azure CLI 输出中找到函数的 URL。Find the URL for your function in the Azure CLI output after the function has been successfully deployed.

    [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 by using the azure-functions:add Maven target.