开始开发在 App Engine 柔性环境中运行的 Ruby on Rails 应用。 您创建的应用将在支持所有 Google 产品的同一基础架构上运行,因此您可以放心,这些应用能顺畅扩容,以满足所有用户(无论是几个还是几百万个)的需求。
本教程假设您熟悉 Rails 网页开发,并将引导您 在新的 Rails 应用中设置 Cloud SQL for PostgreSQL 。此外,本教程还提供了有关如何将现有 Rails 应用配置为使用 Cloud SQL for PostgreSQL 的参考信息。
本教程支持并需要 Ruby 3.0 或更高版本。
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud的新用户, 请创建一个账号,以便在 真实场景中评估我们产品的性能。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud SQL Admin API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud SQL Admin API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
准备 Cloud SQL for PostgreSQL 实例
为本教程设置 Cloud SQL for PostgreSQL 实例。
创建 PostgreSQL 实例。 在本教程中,该实例的名称为
rails-cloudsql-instance。在该实例中创建数据库。 在本教程中,生产数据库的名称为
cat_list_production。
为 Rails 设置本地环境
要为本教程设置本地环境,请执行以下操作:
如需详细了解如何安装 Rails 及其依赖项,请参阅 官方 Rails 入门 指南。
满足前提条件后,您可以使用 Cloud SQL for PostgreSQL 创建和部署 Rails 应用。以下部分将引导您完成配置、Cloud SQL for PostgreSQL 连接以及应用部署。
创建新应用以列出猫咪
运行
rails new命令以创建新的 Rails 应用。此应用将在 Cloud SQL for PostgreSQL 中存储一个猫咪列表。rails new cat_sample_app转到包含生成的 Rails 应用的目录。
cd cat_sample_app
在本地运行应用
要在本地计算机上运行新 Rails 应用,请执行以下操作:
启动本地 Web 服务器:
bundle exec bin/rails server在浏览器中,转到 http://localhost:3000/
示例应用会显示 Rails 徽标以及 Rails 和 Ruby 版本。
为猫咪列表生成基架
为名为 Cat 的资源生成基架,该资源用于生成一个猫咪列表,其中包含猫咪的名字和年龄。
生成基架。
bundle exec rails generate scaffold Cat name:string age:integer该命令会为
Cat资源生成模型、控制器和视图。invoke active_record create db/migrate/20230922063608_create_cats.rb create app/models/cat.rb invoke test_unit create test/models/cat_test.rb create test/fixtures/cats.yml invoke resource_route route resources :cats invoke scaffold_controller create app/controllers/cats_controller.rb invoke erb create app/views/cats create app/views/cats/index.html.erb create app/views/cats/edit.html.erb create app/views/cats/show.html.erb create app/views/cats/new.html.erb create app/views/cats/_form.html.erb create app/views/cats/_cat.html.erb invoke resource_route invoke test_unit create test/controllers/cats_controller_test.rb create test/system/cats_test.rb invoke helper create app/helpers/cats_helper.rb invoke test_unit invoke jbuilder create app/views/cats/index.json.jbuilder create app/views/cats/show.json.jbuilder create app/views/cats/_cat.json.jbuilder打开文件
config/routes.rb可查看以下生成的内容。将
root 'cats#index'添加到文件中。保存文件并关闭。
按照之前所述的方式对 Rails 应用进行测试。
将 Cloud SQL for PostgreSQL 与 App Engine 搭配使用
Cloud SQL for PostgreSQL 是一种全托管式数据库服务,用于设置、 维护、管理和控制关系型 PostgreSQL 数据库, Google Cloud您可以在 Rails 应用中象使用其他任何关系型数据库一样使用 Cloud SQL。
设置 Cloud SQL for PostgreSQL
要开始在您的生产环境 Rails 应用中使用 Cloud SQL,请执行以下操作:
将
pg和appenginegem 添加到Gemfile文件中。bundle add pg bundle add appengineRails
Gemfile包含以下额外的gem条目:打开
config/database.yml文件,以将 Rails 应用配置为连接到 Cloud SQL。系统将显示 SQLite 的以下样板数据库设置:为 App Engine 生产环境配置 Cloud SQL 实例连接名称。
检索实例连接名称。
gcloud sql instances describe rails-cloudsql-instance复制
connectionName旁边的值。
将
database.yml生产数据库配置修改为以下内容:其中: