使用 Java 和 IntelliJ 中建立第一個 Azure 函式Create your first Azure function with Java and IntelliJ

本文說明: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 free account before you begin.

設定開發環境Set up your development environment

若要使用 Java 和 IntelliJ 開發函式,請安裝下列軟體:To develop a function with Java and IntelliJ, install the following software:

重要

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 第 2 版We 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 中,選取 [建立新專案] 。In IntelliJ IDEA, select Create New Project.
  2. 在 [新增專案] 視窗中,從左窗格選取 [Maven] 。In the New Project window, select Maven from the left pane.
  3. 選取 [從 Archetype 建立] 核取方塊,並針對 azure-functions-archetype 選取 [新增 Archetype] 。Select the Create from archetype check box, and then select Add Archetype for the azure-functions-archetype.
  4. 在 [新增 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
    • 版本:使用的最新版本來中央儲存機制 建立 Maven 專案從 IntelliJ IDEA 中的原型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. 輸入目前專案的詳細資料,然後選取 [完成] 。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

注意

若要在本機執行函式並進行偵錯,請確定您已安裝 Azure Functions Core Tools 第 2 版To 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 專案] 工具列。Open the Maven Projects toolbar.

  3. 展開 [生命週期] ,然後開啟 [套件] 。Expand Lifecycle, and then open package. 解決方案會建置並封裝在新建立的目標目錄中。The solution is built and packaged in a newly created target directory.

  4. 展開 [外掛程式] > [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. 在 [執行] 功能表中選取 [編輯組態] 。On the Run menu, select Edit Configurations.

  3. 選取 (+) 以新增 [遠端] 。Select (+) to add a Remote.

  4. 完成 [名稱] 和 [設定] 欄位,然後選取 [確定] 以儲存組態。Complete the Name and Settings fields, and then select OK to save the configuration.

  5. 設定完成後,選取 [對 <遠端組態名稱> 進行偵錯] 或在鍵盤上按 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 CLI 登入,才可以將函式部署到 Azure。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 專案] 視窗中選取 [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.azurewebsites.net
    [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.