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

发布时间 2023-06-28 16:46:58作者: ED_Reagan

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

    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 在本地运行项目

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

:warning: 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.

  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