dispatch.yaml 配置文件

地区 ID

REGION_ID 是 Google 根据您在创建应用时选择的区域分配的缩写代码。此代码不对应于国家/地区或省,尽管某些区域 ID 可能类似于常用国家/地区代码和省代码。对于 2020 年 2 月以后创建的应用,REGION_ID.r 包含在 App Engine 网址中。对于在此日期之前创建的现有应用,网址中的区域 ID 是可选的。

详细了解区域 ID

dispatch.yaml 可以替换路由规则。您可以使用 dispatch.yaml 根据网址中的路径或主机名向特定服务(以前称为模块)发送传入请求。

如需了解详情,请参阅请求的路由方式

一个应用只能有一个 dispatch.yaml 文件,该文件中的路由规则将应用于应用的所有服务和版本。路由规则也适用于 Cron 文件中使用的网址。

部署调度文件

dispatch.yaml 文件应与 Go 源代码位于同一目录中。

在部署调度文件之前,必须确保此文件中定义的所有服务均已部署到 App Engine。如需部署 dispatch.yaml 文件,请在包含 dispatch.yaml 的目录中运行 gcloud app deploy 命令:

gcloud app deploy dispatch.yaml
要详细了解部署命令,请参阅部署 Go 1.11 应用

语法

dispatch.yaml 文件中的根元素是 dispatch:,其中包含由以下子元素指定的路由定义列表。

您在调度文件中定义的规则必须使用 HTTP 网址格式,这种格式采用“.”表示法分隔子网域。不支持使用 HTTPS“-dot-”表示法定义的网址。

调度规则按顺序执行,并且将仅应用与网址匹配的第一项规则。

元素 说明
service

指定服务的名称,该服务将处理与 url 格式匹配的请求。请注意,服务以前称为模块。

url

url 元素中,您可在英文引号内定义网址格式,该格式可包含主机名和网址路径且长度不超过 100 个字符。对于 service 元素,请指定您希望用于处理传入请求的服务的名称,其中这些请求需要与 url 元素的网址格式匹配。

提示:您可以在 url 元素中包含 * 通配符等 Glob 格式;但是,这些格式只能在主机名的前面和网址路径的末尾使用。

一种网址格式,可包含主机名和网址路径。Glob 字符可用于匹配网址格式。Glob 字符只能在格式的开头和末尾指定。

调度文件不会路由以 /_ah/ 开头的网址路径。

示例

下面的示例调度文件会将请求路由到 https://simple-sample.uc.r.appspot.com,并将 https://simple-sample.uc.r.appspot.com/favicon.ico 等请求路由到 default 服务。所有静态内容均由 default 服务提供。像 https://simple-sample.uc.r.appspot.com/mobile/ 这样的移动请求会路由到移动前端,而像 https://simple-sample.uc.r.appspot.com/work/ 这样的工作器请求会路由到静态后端。

dispatch:
  # Default service serves the typical web resources and all static resources.
  - url: "*/favicon.ico"
    service: default

  # Default service serves simple hostname request.
  - url: "simple-sample.appspot.com/"
    service: default

  # Send all mobile traffic to the mobile frontend.
  - url: "*/mobile/*"
    service: mobile-frontend

  # Send all work to the one static backend.
  - url: "*/work/*"
    service: static-backend

如果您更倾向于使用可与大量可能的请求相匹配的宽泛路由规则,则可以定义范围更广泛的规则。例如:

# Send any path that begins with “simple-sample.appspot.com/mobile” to the mobile-frontend service.
- url: "simple-sample.appspot.com/mobile*"
  service: mobile-frontend

# Send any domain/sub-domain with a path that starts with “work” to the static backend service.
- url: "*/work*"
  service: static-backend

您也可以编写更严格的表达式:

# Matches the path "/fun", but not "/fun2" or "/fun/other".
- url: "*/fun"
  service: mobile-frontend

# Matches the hostname "customer1.myapp.com", but not "1.customer1.myapp.com".
- url: "customer1.myapp.com/*"
  service: static-backend

限制

调度文件最多可包含 20 个路由规则。指定网址字符串时,主机名和路径的长度均不能超过 100 个字符。

删除所有调度规则

要删除所有调度规则,请执行以下操作:

  1. 如下所示修改 dispatch.yaml 文件的内容:

    dispatch: []
    
  2. dispatch.yaml 文件部署到 App Engine。