使用 JAVA 和 Eclipse 建立您的第一個函式Create your first function with Java and Eclipse

本文示範如何使用 Eclipse IDEA 和 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 free 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:

重要

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 中, 選取 [檔案] 功能表, 然後選取 [新增-> Maven 專案]。In Eclipse, select the File menu, then select New -> Maven Project.
  2. 接受 [新增 Maven 專案] 對話方塊中的預設值,然後選取 [下一步]。Accept the defaults in the New Maven Project dialogue and select Next.
  3. 選取 [新增原型] 並新增 azure-functions-archetype 的項目。Select Add Archetype and add the entries for the azure-functions-archetype.
    • 原型群組識別碼:com.microsoft.azureArchetype Group ID: com.microsoft.azure
    • 原型構件識別碼:azure-functions-archetypeArchetype Artifact ID: azure-functions-archetype
    • 版本:使用來自中央存放庫 Eclipse Maven create 的最新版本1.22Version: Use latest version 1.22 from the central repository Eclipse Maven create
  4. 按一下 [確定] , 然後按 [下一步] 輸入如下列快照集的值 (請使用fabrikam-function-20170920120101928以外的其他 appName), 最後完成Click OK and then click Next to enter values like the following snapshot(please use a different appName other than fabrikam-function-20170920120101928), and eventually Finish. Eclipse Maven create2Eclipse Maven create2

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

注意

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 變更為 azure-functions:run -DenableDebug 並執行更新後的組態,以在偵錯模式中啟動函數應用程式。In your Run As configuration set up in the previous step, change azure-functions:run to 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.azurewebsites.net
[INFO] ------------------------------------------------------------------------

後續步驟Next steps

  • 檢閱 Java 函式開發人員指南以了解開發 Java 函式的詳細資訊。Review the Java Functions developer guide for more information on developing Java functions.
  • 使用 azure-functions:addMaven 目標,將具有不同觸發程序的其他函式新增至您的專案。Add additional functions with different triggers to your project using the azure-functions:add Maven target.