Java 8 已达到支持终止期限,并将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Java 8 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署,也无法部署。现有的 Java 8 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Java 版本。
配置文件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
服务的每个版本都在 .yaml
文件中定义,此文件提供相应服务和版本的名称。YAML 文件通常与其定义的服务同名,但这不是必需的。如果您要部署一项服务的多个版本,则可以在同一目录中创建多个 yaml 文件,每个文件对应一个版本。
通常,您可以为每项服务创建一个目录,以包含相应服务的 YAML 文件和关联的源代码。可选的应用级配置文件(dispatch.yaml
、cron.yaml
、index.yaml
和 queue.yaml
)包含在顶级应用目录中。以下示例展示了三项服务。在 service1
和 service2
中,源文件与 YAML 文件处于同一级别。在 service3
中,有两个版本的 YAML 文件。

对于小型简单项目,应用的所有文件都可以位于一个目录中:

每个 YAML 文件都必须包含版本参数。如需定义默认服务,您可以在文件中明确声明参数 service: default
或者不作声明。
每项服务的配置文件都定义了特定服务/版本的扩缩类型和实例类别。使用的扩缩参数取决于您指定的扩缩类型。如果未指定扩缩类型,则默认为自动扩缩。appengine-web.xml
参考文档部分介绍了扩缩和实例类别设置。
对于每项服务,您还可以指定相应设置,以将网址请求映射到特定脚本以及识别静态文件,进而提高服务器效率。这些设置也包含在 yaml 文件中,并在 appengine-web.xml
参考文档部分进行了说明。
default 服务
每个应用都有一项默认服务。您可以在 appengine-web.xml
中使用 service: default
设置定义默认服务,但并不强制要求这样做。与服务相关的所有配置参数都可以应用于默认服务。
可选配置文件
以下配置文件可以对应用于应用中所有服务的可选功能进行控制:
要将这些配置文件的更新部署到 App Engine,请从这些文件所在的目录中运行以下命令:
gcloud app deploy [CONFIG_FILE]
示例
以下示例演示了如何为拥有两项服务的应用配置各种 WAR 目录结构的文件:一项服务为处理 Web 请求的默认服务,另一项为用于后端处理的服务(名为 my-service
)。
假设顶级 EAR 目录是“my-application”,请定义文件 my-application/META-INF/appengine-application.xml
:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<appengine-application xmlns="http://appengine.google.com/ns/1.0">
<application>my-application</application>
</appengine-application>
为 my-application/default
和 my-application/my-service
这两个服务创建 WAR 目录。
现在,在每个指定服务参数的 WAR 文件中创建一个 appengine-web.xml
文件。该文件必须包含该服务的版本名称。如需定义默认服务,您可以在文件中明确声明参数 <service>default</service>
或者不作声明。以下是定义默认服务的文件 my-application/default/WEB-INF/appengine-web.xml
:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>my-application</application>
<module>default</module>
<version>uno</version>
<threadsafe>true</threadsafe>
</appengine-web-app>
文件 my-application/my-service/WEB-INF/appengine-web.xml
定义将处理后台请求的服务:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>my-application</application>
<module>my-service</module>
<version>uno</version>
<threadsafe>true</threadsafe>
<manual-scaling>
<instances>5</instances>
</manual-scaling>
</appengine-web-app>
最后,定义用于枚举服务的文件 my-application/META-INF/application.xml
。请注意,默认服务应该是列出的首项服务。
<?xml version="1.0"
encoding="UTF-8"?>
<application
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/application_5.xsd"
version="5">
<description>GAE Java SuperFun app</description>
<display-name>SuperFun</display-name>
<!-- Services -->
<!-- The default service should be listed first -->
<module>
<web>
<web-uri>default</web-uri>
<context-root>default</context-root>
</web>
</module>
<module>
<web>
<web-uri>my-service</web-uri>
<context-root>my-service</context-root>
</web>
</module>
</application>
App Engine 将忽略 <context-root>
元素,因此 HTTP 客户端在为服务指定网址时无需将此类元素添加到网址路径之前。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-20。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-20。"],[[["\u003cp\u003eService versions are defined in \u003ccode\u003e.yaml\u003c/code\u003e files, which specify the service name and version, and multiple versions of a service can be defined by creating multiple YAML files in the same directory.\u003c/p\u003e\n"],["\u003cp\u003eEach service's configuration file defines its scaling type and instance class, with automatic scaling as the default if not specified, and these configurations are found in the \u003ccode\u003eappengine-web.xml\u003c/code\u003e reference section.\u003c/p\u003e\n"],["\u003cp\u003eEvery application has a single default service, which can be designated explicitly with \u003ccode\u003eservice: default\u003c/code\u003e or by omitting the service parameter in the \u003ccode\u003eappengine-web.xml\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eOptional configuration files, such as \u003ccode\u003edispatch.yaml\u003c/code\u003e, \u003ccode\u003equeue.yaml\u003c/code\u003e, \u003ccode\u003eindex.yaml\u003c/code\u003e, and \u003ccode\u003ecron.yaml\u003c/code\u003e, are used to manage routing, queues, indexes, and scheduled tasks, and are deployed using the \u003ccode\u003egcloud app deploy [CONFIG_FILE]\u003c/code\u003e command.\u003c/p\u003e\n"],["\u003cp\u003eIn an example of an application with two services, the default service and \u003ccode\u003emy-service\u003c/code\u003e, each WAR directory contains an \u003ccode\u003eappengine-web.xml\u003c/code\u003e file that defines the specific parameters for the service and the \u003ccode\u003eapplication.xml\u003c/code\u003e file enumerates the services, with the default service listed first.\u003c/p\u003e\n"]]],[],null,["# Configuration Files\n\nEach version of a service is defined in a `.yaml` file, which gives the name of\nthe service and version. The YAML file usually takes the same name as the\nservice it defines, but this is not required. If you are deploying several\nversions of a service, you can create multiple yaml files in the same directory,\none for each version.\n\nTypically, you create a directory for each service, which contains the service's\nYAML files and associated source code. Optional application-level configuration\nfiles (`dispatch.yaml`, `cron.yaml`, `index.yaml`, and `queue.yaml`) are\nincluded in the top level app directory. The example below shows three services.\nIn `service1` and `service2`, the source files are at the same level as the YAML\nfile. In `service3`, there are YAML files for two versions.\n\nFor small, simple projects, all the app's files can live in one directory:\n\nEvery YAML file must include a version parameter. To define the default service,\nyou can explicitly include the parameter `service: default` or leave the service\nparameter out of the file.\n\nEach service's configuration file defines the scaling type and instance class\nfor a specific service/version. Different scaling parameters are used depending\non which type of scaling you specify. If you do not specify scaling, automatic\nscaling is the default. The scaling and instance class settings are described in\nthe [`appengine-web.xml`\nreference](/appengine/docs/legacy/standard/java/config/appref#scaling_elements)\nsection.\n\nFor each service you can also specify settings that map URL requests to specific\nscripts and identify static files for better server efficiency. These settings\nare also included in the yaml file and are described in the\n[`appengine-web.xml` reference](/appengine/docs/legacy/standard/java/config/appref)\nsection.\n\n### The default service\n\nEvery application has a single default service. You can define the default\nservice in the `appengine-web.xml` with the setting\n`service: default`, but it isn't necessary to do this. All configuration\nparameters relevant to services can apply to the default service.\n\n### Optional configuration files\n\nThese configuration files control optional features that apply to all the\nservices in an app:\n\n- [`dispatch.yaml`](/appengine/docs/legacy/standard/java/reference/dispatch-yaml) overrides routing default rules by sending incoming requests to a specific service based on the path or hostname in the URL.\n- [`queue.yaml`](/appengine/docs/legacy/standard/java/config/queueref) configures both push queues and pull queues.\n- [`index.yaml`](/appengine/docs/legacy/standard/java/configuring-datastore-indexes-with-index-yaml) specifies which indexes your app needs if using Datastore queries.\n- [`cron.yaml`](/appengine/docs/legacy/standard/java/config/cronref) configures regularly scheduled tasks that operate at defined times or regular intervals.\n\nTo deploy updates of these configuration files to App Engine, run the\nfollowing command from the directory where they are located: \n\n gcloud app deploy [CONFIG_FILE]\n\n### An example\n\nHere is an example of how you would configure the various files in a WAR\ndirectory structure for an application that has two services: a default service\nthat handles web requests, plus another service (named `my-service`) for backend\nprocessing.\n\nAssuming that the top-level EAR directory is \"my-application,\" define the file\n`my-application/META-INF/appengine-application.xml`: \n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?\u003e\n \u003cappengine-application xmlns=\"http://appengine.google.com/ns/1.0\"\u003e\n \u003capplication\u003emy-application\u003c/application\u003e\n \u003c/appengine-application\u003e\n\nCreate WAR directories for the two services: `my-application/default` and `my-application/my-service`.\n\nNow create an `appengine-web.xml` file in each WAR that specifies the parameters\nfor the service. The file must include a version name for the service. To define\nthe default service, you can explicitly include the `\u003cservice\u003edefault\u003c/service\u003e`\nparameter or leave it out of the file. Here is the file\n`my-application/default/WEB-INF/appengine-web.xml` that defines the default\nservice: \n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?\u003e\n \u003cappengine-web-app xmlns=\"http://appengine.google.com/ns/1.0\"\u003e\n \u003capplication\u003emy-application\u003c/application\u003e\n \u003cmodule\u003edefault\u003c/module\u003e\n \u003cversion\u003euno\u003c/version\u003e\n \u003cthreadsafe\u003etrue\u003c/threadsafe\u003e\n \u003c/appengine-web-app\u003e\n\nThe file `my-application/my-service/WEB-INF/appengine-web.xml` defines the service that will handle background requests: \n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?\u003e\n \u003cappengine-web-app xmlns=\"http://appengine.google.com/ns/1.0\"\u003e\n \u003capplication\u003emy-application\u003c/application\u003e\n \u003cmodule\u003emy-service\u003c/module\u003e\n \u003cversion\u003euno\u003c/version\u003e\n \u003cthreadsafe\u003etrue\u003c/threadsafe\u003e\n \u003cmanual-scaling\u003e\n \u003cinstances\u003e5\u003c/instances\u003e\n \u003c/manual-scaling\u003e\n \u003c/appengine-web-app\u003e\n\nFinally, define the file `my-application/META-INF/application.xml` that\nenumerates the services. Note that the default service should be the first\nservice listed. \n\n \u003c?xml version=\"1.0\"\n encoding=\"UTF-8\"?\u003e\n\n \u003capplication\n xmlns=\"http://java.sun.com/xml/ns/javaee\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee\n http://java.sun.com/xml/ns/javaee/application_5.xsd\"\n version=\"5\"\u003e\n\n \u003cdescription\u003eGAE Java SuperFun app\u003c/description\u003e\n \u003cdisplay-name\u003eSuperFun\u003c/display-name\u003e\n\n \u003c!-- Services --\u003e\n \u003c!-- The default service should be listed first --\u003e\n \u003cmodule\u003e\n \u003cweb\u003e\n \u003cweb-uri\u003edefault\u003c/web-uri\u003e\n \u003ccontext-root\u003edefault\u003c/context-root\u003e\n \u003c/web\u003e\n \u003c/module\u003e\n \u003cmodule\u003e\n \u003cweb\u003e\n \u003cweb-uri\u003emy-service\u003c/web-uri\u003e\n \u003ccontext-root\u003emy-service\u003c/context-root\u003e\n \u003c/web\u003e\n \u003c/module\u003e\n\n \u003c/application\u003e\n\nApp Engine will ignore the `\u003ccontext-root\u003e` elements, so HTTP clients need not\nprepend it to the URL path when addressing a service."]]