使用 Ruby 创建和部署 HTTP Cloud Functions 函数

使用 Ruby 创建和部署 HTTP Cloud Functions 函数

本指南将引导您完成使用 Ruby 运行时编写 Cloud Functions 函数的过程。Cloud Functions 函数有两种类型:

  • HTTP 函数,通过标准 HTTP 请求调用。
  • 事件驱动型函数,用于处理来自云基础设施的事件,例如 Pub/Sub 主题中收到消息或 Cloud Storage 存储桶发生更改。

如需了解详情,请参阅编写 HTTP 函数编写事件驱动型函数

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Cloud Functions、Cloud Build、Artifact Registry、Cloud Run 和 Cloud Logging API。

    启用 API

  5. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  6. 确保您的 Google Cloud 项目已启用结算功能

  7. 启用 Cloud Functions、Cloud Build、Artifact Registry、Cloud Run 和 Cloud Logging API。

    启用 API

  8. 安装并初始化 Google Cloud SDK
  9. 使用以下命令更新和安装 gcloud 组件。
    gcloud components update
  10. 准备开发环境。

    转到 Ruby 设置指南

创建函数

  1. 在本地系统上为函数代码创建一个目录:

    Linux 或 Mac OS X

    mkdir ~/helloworld
    cd ~/helloworld
    

    Windows

    mkdir %HOMEPATH%\helloworld
    cd %HOMEPATH%\helloworld
    
  2. helloworld 目录中创建一个 app.rb 文件,其中包含以下内容:

    require "functions_framework"
    require "cgi"
    require "json"
    
    FunctionsFramework.http "hello_http" do |request|
      # The request parameter is a Rack::Request object.
      # See https://www.rubydoc.info/gems/rack/Rack/Request
      name = request.params["name"] ||
             (request.body.rewind && JSON.parse(request.body.read)["name"] rescue nil) ||
             "World"
      # Return the response body as a string.
      # You can also return a Rack::Response object, a Rack response array, or
      # a hash which will be JSON-encoded into a response.
      "Hello #{CGI.escape_html name}!"
    end

    此示例函数会获取 HTTP 请求中提供的姓名并返回问候语;或者,如果没有提供姓名,则返回“Hello World!”。

指定依赖项

Ruby 依赖项可通过 bundler 进行管理,并在名为 Gemfile 的文件中表示。

部署您的函数时,Cloud Functions 会使用 bundler 下载并安装 GemfileGemfile.lock 中声明的依赖项。

Gemfile 会列出函数所需的软件包以及所有可选版本限制条件。对于 Cloud Functions 函数,其中一个软件包必须是 functions_framework gem。

在此练习中,在包含函数代码的 app.rb 文件所在的目录中创建名为 Gemfile 的文件,其中包含以下内容:

source "https://rubygems.org"
gem "functions_framework", "~> 0.7"

运行以下命令安装 functions_framework gem 和其他依赖项:

bundle install

在本地构建和测试函数

在部署函数之前,您可以在本地构建和测试该函数。

  1. 运行以下命令以使用 functions-framework-ruby 可执行文件启动一个运行 hello_http 函数的本地 Web 服务器:

    bundle exec functions-framework-ruby --target hello_http
    
  2. 在浏览器中访问 http://localhost:8080,或从其他窗口运行 curl localhost:8080,以测试函数。

    如需了解详情,请参阅向本地函数发送请求

请参阅 Ruby Functions 框架文档中的测试函数

部署函数

如需部署函数,请在 helloworld 目录中运行以下命令:

  gcloud functions deploy ruby-http-function \
    --gen2 \
    --runtime=ruby32 \
    --region=REGION \
    --entry-point=hello_http \
    --source=. \
    --trigger-http \
    --allow-unauthenticated

REGION 替换为要部署函数的 Google Cloud 区域的名称(例如 us-west1)。

通过可选的 --allow-unauthenticated 标志,您可以在不进行身份验证的情况下访问函数。

测试已部署的函数

  1. 函数部署后,请记下 gcloud functions deploy 命令输出中的 uri 属性,或使用以下命令检索该属性:

        gcloud functions describe ruby-http-function \
          --region=REGION
    

    REGION 替换为您在其中部署函数的 Google Cloud 区域的名称(例如 us-west1)。

  2. 在浏览器中访问此网址。该函数将返回一条“Hello World!”消息。

查看函数日志

您可以使用 Cloud Logging 界面或通过 Google Cloud CLI 查看函数的日志。

使用命令行工具查看日志

如需使用 gcloud CLI 查看函数的日志,请使用 logs read 命令:

    gcloud functions logs read \
      --gen2 \
      --limit=10 \
      --region=REGION \
      ruby-http-function

REGION 替换为您在其中部署函数的 Google Cloud 区域的名称(例如 us-west1)。

输出类似以下内容:

LEVEL: I
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.477
LOG: Default STARTUP TCP probe succeeded after 1 attempt for container "hello__http-1" on port 8080.

LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.451
LOG: I, [2023-06-01T00:09:41.451784 #1]  INFO -- : FunctionsFramework: Serving function "hello_http" on port 8080...

LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.364
LOG: I, [2023-06-01T00:09:41.363923 #1]  INFO -- : FunctionsFramework: Starting server...

LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.363
LOG: I, [2023-06-01T00:09:41.363855 #1]  INFO -- : FunctionsFramework: Looking for function name "hello_http"...

LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.354
LOG: I, [2023-06-01T00:09:41.354150 #1]  INFO -- : FunctionsFramework: Loading functions from "./app.rb"...

使用日志记录信息中心查看日志

要使用日志记录信息中心查看函数的日志,请打开 Cloud Functions 概览页面,点击列表中的函数名称,然后点击日志标签页。