使用 IntelliJ IDEA 开发 Azure Function APP (Java)

1.1 前提

1.2 安装插件并登录

在 IntelliJ IDEA 中安装 Azure Toolkit 插件并登录,具体步骤如下:

  1. 在 IntelliJ IDEA Settings/Perfernced 中选择 Plug。在 MarketPlace 中找到 Azure Toolkit for IntelliJ,并点击 install。在安装完成后点击 Restart 插件可被使用。

    install_plug_step

  2. 要登录 Azure account,首先打开侧边栏中的 Azure Explore ,然后点击菜单顶部 (或者在 IDEA 菜单中,选择 Tools > Azure > Azure Sign In) Azure Sign In 图标。

    The IntelliJ Azure Sign In command.

  3. Azure Sign In 窗口选择 OAuth 2.0 然后点击 Sign In。如果使用其他登录选项,请参考 Sign-in instructions for the Azure Toolkit for IntelliJ

    The Azure Sign In window with device login selected.

  4. 在浏览器中登录 Azure account,然后返回到 IntelliJ。在 Select Subscription 对话框中选择您想要使用的订阅,并点击 Select

    img

    1.3 创建本地项目

    使用 Azure Toolkit 通过 IntelliJ 创建本地的 Azure Function 项目,具体步骤如下:

    1. 打开 IntelliJ IDEA 您会看到 Welcome 对话框,选择 New Project 打开新项目创建指导,并选择 Azure Functions

      Create function project.

    2. 选择 Http Trigger,然后点击 Next 随后根据创建指引完善在后续页面中的配置信息。确认您项目的地址,随后点击 Finish。IntelliJ IDEA 将会创建并在窗口中打开您的项目。

      Create function project finish.

1.3 在本地运行项目

在本地运行项目,跟随以下步骤:

⚠️ Important

You must have the JAVA_HOME environment variable set correctly to the JDK directory that is used during code compiling using Maven. Make sure that the version of the JDK is at least as high as the Java.version setting.

您必须将 JAVA_HOME 环境变量正确设置为使用 Maven 编译代码时使用的 JDK 目录。确保JDK的版本至少与 Java.version 的设置一样高。

  1. 导航到 src/main/java/org/example/functions/HttpTriggerFunction.java 来查看生成的代码。在第 24 行旁边,你会发现有一个绿色的 Run 按钮。点击它并选择运行 Run 'Functions-azur...'。你会看到你的函数应用正在本地运行,并有一些日志。
    Local run project.

    Local run project output.

  2. 你可以通过从浏览器访问显示的端点来尝试该功能,如 http://localhost:7071/api/HttpExample?name=Azure

    Local run function test result.

  3. 该日志也会显示在你的IDEA中。点击 Stop 按钮,停止该功能应用。

    Local run function test log.

1.4 在本地 Debug 项目

在本地 Debug 项目,具体步骤如下:

  1. 选择 Debug 按钮在工具栏中,如果您未找到工具栏,请通过在菜单中选择 View > Apperance > Toolbar 以激活菜单栏。

    Local debug function app button.

  2. 点击文件 src/main/java/org/example/functions/HttpTriggerFunction.java 的第 31 行,添加一个断点。再次访问端点 http://localhost:7071/api/HttpTrigger-Java?name=Azure,你会发现断点被击中。然后,你可以尝试更多的调试功能,如 Step、Watch、and Evaluation。点击 Stop 按钮,停止调试会话。

    Local debug function app break.

1.5 部署您的项目到 Azure 上

部署您的 Azure Function App 到 Azure 上具体步骤如下:

  1. 右键点击 IntelliJ 项目资源管理器中的你的项目。然后选择 Azure > Deploy > to Azure Functions

  2. 如果你还没有任何 Function App,请在 Function line 点击+。键入功能应用的名称,并选择适当的平台。这里你可以接受默认值。点击 OK,你创建的新功能应用将被自动选中。点击 Run 来部署你的功能。

    Create function app in Azure.

    Deploy function app to Azure log.

1.6 通过 IDEA 管理 Function App

要在 IDEA 中用 Azure Explorer 管理你的 Function App,请遵循以下步骤:

  1. 点击 Function App,你会看到列出的所有 Function App。

    View function apps in explorer.

  2. 点击选择你的一个功能应用程序,然后点击右键,选择 Show Properties,打开详细页面。

    Show function app properties.

  3. 在你的 HttpTrigger-Java Function App 上点击右键,然后选择 Trigger Function in Browser。你会看到浏览器被打开,里面有触发器的URL。

    Screenshot shows a browser with the U R L.

1.7 在项目中添加更多的 Functions

要在你的项目中添加更多的函数,请按照以下步骤进行:

  1. org.example.function 包上点击右键,选择 New > Azure Function Class

    Add functions to the project entry.

  2. 在创建函数类的向导中填写类名 HttpTest 并选择 HttpTrigger,然后点击 OK 创建。通过这种方式,你可以随心所欲地创建新的函数。

    Screenshot shows the Create Function Class dialog box.

    Add functions to the project output.

1.8 清理 Functions

在你的 IDEA 中使用 Azure Explorer 选择你的一个功能应用,然后右键单击并选择 Delete。这个命令可能需要几分钟的时间来运行。当它完成后,状态会在 Azure Explorer 中刷新。

Screenshot shows Delete selected from a context menu.

Information

This article is translated from the official Azure Function documentation: https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-maven-intellij

posted @ 2023-06-28 16:36  ED_Reagan  阅读(39)  评论(0编辑  收藏  举报