练习 - 在本地运行和测试 Azure 函数

已完成

在前面的单元中,你了解了如何从 Maven 原型创建无服务器 Web 服务 Azure 函数。 你还了解了如何在 Cloud Shell 中生成和运行函数,以及如何配置 shell 环境以测试函数。

在此练习中,你应用已学习的知识,在 Cloud Shell 中打开 HTTP 端口来测试函数。 然后,在 Cloud Shell 中生成并运行函数,并创建 API URL 以使用 Web 浏览器测试函数。

打开用于测试的 HTTP 端口

在远程测试函数之前,需要打开一个端口,以便将 HTTP 请求映射到函数。 此操作生成一个公共 URL,你之后在此练习中使用该 URL 来测试函数。

  1. 在 Azure Cloud Shell 中,使用以下 cURL 命令打开用于测试的 HTTP 端口:

    curl -X POST http://localhost:8888/openPort/7071
    
  2. 当该端口打开时,看到类似于以下示例的 JSON 响应:

    {"message":"Port 7071 is open","url":"https://gateway.westus.console.azure.com/n/cc-12345678/cc-12345678/proxy/7071/"}
    

    该 JSON 响应连接到单个行中,如前面的示例中所示。 而下面的示例展示的是格式化后的 JSON 响应:

    {
        "message": "Port 7071 is open",
        "url": "https://gateway.westus.console.azure.com/n/cc-12345678/cc-12345678/proxy/7071/"
    }
    

    此示例应能帮助你检查响应中包含的数据。

  3. 复制 Cloud Shell 实例的 JSON 响应中的 URL。 之后在本练习中,使用该 URL 在 Web 浏览器中测试函数。

在 Cloud Shell 中生成并运行函数

打开用于测试的端口后,便可生成并运行函数。

  1. 在 Azure Cloud Shell 中,切换到应用程序的根文件夹。 例如:

    cd ~/event-reporting
    
  2. 请使用以下 Maven 命令来清理项目目录并生成函数:

    mvn clean package
    

    Maven 显示生成进程的运行状态。 首次生成函数时,Maven 下载几十个支持文件,这些文件是 pom.xml 和 host.json 文件中列出的依赖项。 以下摘录显示了成功的生成的简化示例:

    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Azure Java Functions 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    . . .
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 8.509 s
    [INFO] Finished at: 2020-01-01T04:55:05+00:00
    [INFO] Final Memory: 57M/306M
    [INFO] ------------------------------------------------------------------------
    
  3. Maven 完成构建并打包函数后,使用以下 Maven 命令运行函数:

    mvn azure-functions:run
    

    Maven 显示启动进程的运行状态。 以下摘录显示了成功的启动的简化示例:

    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Azure Java Functions 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- azure-functions-maven-plugin:1.4.1:run (default-cli) @ event-reporting ---
    [INFO] Azure Function App's staging directory found at: /home/user/event-reporting/target/azure-functions/event-reporting-20200101063700664
    [INFO] Azure Functions Core Tools found.
    
                      %%%%%%
                     %%%%%%
                @   %%%%%%    @
              @@   %%%%%%      @@
           @@@    %%%%%%%%%%%    @@@
         @@      %%%%%%%%%%        @@
           @@         %%%%       @@
             @@      %%%       @@
               @@    %%      @@
                    %%
                    %
    
    Azure Functions Core Tools (2.7.2184 Commit hash: 5afacc827c2848e4debc23bb96604f1ffce09cc7)
    Function Runtime Version: 2.0.12961.0
    . . .
    Hosting environment: Production
    Content root path: /home/user/event-reporting/target/azure-functions/event-reporting-20200101063700664
    Now listening on: http://0.0.0.0:7071
    Application started. Press Ctrl+C to shut down.
    
    Http Functions:
    
            HttpExample: [GET,POST] http://localhost:7071/api/HttpExample
    
    

    无服务器函数正在侦听 HTTP 请求。

  4. 复制 HTTP 端口后面的 URL 部分,例如:

    /api/HttpExample
    

    你在本练习的下一部分中使用该 URL 摘录在 Web 浏览器中测试函数。

在 Web 浏览器中测试函数

在本练习前面的部分中,你已打开用于测试的 HTTP 端口;通过此端口,可在 Web 浏览器中测试应用程序。 为此,请按照以下步骤操作。

  1. 构造函数的 API 的 URL:

    1. 检索之前在本练习的“打开用于测试的 HTTP 端口”部分中复制的 URL,例如:

      https://gateway.westus.console.azure.com/n/cc-12345678/cc-12345678/proxy/7071/
      
    2. 追加之前在本练习的“在 Cloud Shell 中生成并运行函数”部分中复制的函数 API URL 摘录,例如:

      https://gateway.westus.console.azure.com/n/cc-12345678/cc-12345678/proxy/7071/api/HttpExample
      
    3. 追加一个将名称传递到 API URL 的查询字符串,例如:

      https://gateway.westus.console.azure.com/n/cc-12345678/cc-12345678/proxy/7071/api/HttpExample?name=Bob
      
    4. 复制这个构造完整的 URL,供以下步骤使用。

  2. 在 Web 浏览器中打开一个新的选项卡,并将上述步骤中构造完整的 URL 粘贴到地址栏中。

  3. 在指示 Web 浏览器请求该 URL 时,你根据查询字符串中传递的名称,看到一个个性化的纯文本消息返回到 Web 浏览器中。 例如:

    Hello, Bob
    

    在 Azure Cloud Shell 中,应会看到类似于以下示例的状态消息,该消息指示已成功处理请求:

    [1/1/20 7:08:11 AM] Executing HTTP request: {
    [1/1/20 7:08:11 AM]   "requestId": "12345678-1234-1234-1234-123456789abc",
    [1/1/20 7:08:11 AM]   "method": "GET",
    [1/1/20 7:08:11 AM]   "uri": "/api/HttpExample"
    [1/1/20 7:08:11 AM] }
    [1/1/20 7:08:12 AM] Executing 'Functions.HttpExample' (Reason='This function was programmatically called via the host APIs.', Id=12345678-1234-1234-1234-123456789abc)
    [1/1/20 7:08:12 AM] Java HTTP trigger processed a request.
    [1/1/20 7:08:12 AM] Function "HttpExample" (Id: 12345678-1234-1234-1234-123456789abc) invoked by Java Worker
    [1/1/20 7:08:12 AM] Host lock lease acquired by instance ID '00000000000000000000000052DF09EB'.
    [1/1/20 7:08:12 AM] Executed 'Functions.HttpExample' (Succeeded, Id=12345678-1234-1234-1234-123456789abc)
    [1/1/20 7:08:13 AM] Executed HTTP request: {
    [1/1/20 7:08:13 AM]   "requestId": "12345678-1234-1234-1234-123456789abc",
    [1/1/20 7:08:13 AM]   "method": "GET",
    [1/1/20 7:08:13 AM]   "uri": "/api/HttpExample",
    [1/1/20 7:08:13 AM]   "identities": [
    [1/1/20 7:08:13 AM]     {
    [1/1/20 7:08:13 AM]       "type": "WebJobsAuthLevel",
    [1/1/20 7:08:13 AM]       "level": "Admin"
    [1/1/20 7:08:13 AM]     }
    [1/1/20 7:08:13 AM]   ],
    [1/1/20 7:08:13 AM]   "status": 200,
    [1/1/20 7:08:13 AM]   "duration": 1759
    [1/1/20 7:08:13 AM] }
    

如果没有看到任何错误,则说明已成功在本地测试函数!

在继续操作之前,请切换回 Cloud Shell,并按 Ctrl + C 关闭测试服务器。

在下一个单元中,了解如何将函数部署到 Azure Functions。