Go 1.11 已达到支持终止期限,将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Go 1.11 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 Go 1.11 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Go 版本。
管理应用资源
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
App Engine 会生成关于应用性能和资源利用率的使用情况报告。下面列出了可更有效地管理资源的潜在策略。如需了解详情,请参阅价格页面。
查看使用情况报告
在评估应用性能时,您应该检查应用运行的实例数以及应用的资源消耗情况。
查看信息中心使用情况报告
查看“实例”页面
以下部分推荐了一些资源管理策略。
管理动态实例扩缩
缩短延迟时间
应用延迟时间会影响处理流量所需的实例数。您可以通过缩短延迟时间来减少用于处理应用的实例数。Cloud Trace 是一种实用的工具,不但可用于查看有关延迟时间的数据,还可让您了解有助于缩短延迟时间的可行更改措施。
使用 Cloud Trace 查看延迟时间后,请尝试采用下面一些策略来减少延迟时间:
- 增大频繁访问的共享数据的缓存 - 换句话说,就是使用 App Engine Memcache。此外,设置应用的 Cache-Control 标头会对服务器和浏览器缓存数据的效率产生重大影响。即使缓存几秒钟也会对应用处理流量的效率产生影响。
- 更高效地使用 App Engine Memcache - 以批量调用的方式执行 get、set、delete 等操作,而不是执行一系列单独的调用。
- 将任务用于非请求绑定功能 - 如果您的应用执行的工作可以超出面向用户的请求范围,请将此工作放入任务中!将此工作发送到任务队列而不是等待此工作完成再返回响应,这样可以显著缩短面向用户的延迟时间。随后,您可以借助任务队列更好地控制执行速率并调节负载。
- 更高效地使用 Datastore 模式的 Firestore (Datastore) - 如需了解详情,请参阅下文。
- 并行执行多个 URL Fetch 调用:
- 批量处理多个 URL Fetch 调用,而不是在单个面向用户的请求中单独处理调用,然后通过异步 URL Fetch 在离线任务中并行处理调用。
- 使用 goroutine。
- 对 HTTP 会话采用异步写入方式。
您可以使用 app.yaml
配置文件包含的数种设置,针对您的应用的特定版本,在性能和资源负载之间权衡取舍。如需获取可用自动扩缩设置的列表,请参阅扩缩元素。如需了解这些设置的效果,请观看 App Engine 新调度器设置视频。
注意:所有 Go 实例均已自动启用并发请求。
配置任务队列设置
任务队列的默认设置已针对性能进行了调整。如果采用这些默认设置,当您将多个任务同时放入队列时,这些任务可能会导致新的前端实例启动。下面提供了一些有关如何调整任务队列以节省实例小时数的建议:
- 为对延迟时间不敏感的任务设置 X-AppEngine-FailFast 标头。如果现有实例不可用,则此标头会指示调度器立即使请求失败。任务队列将重试并执行退避,直到现有实例可用于处理请求为止。但请务必注意,如果设置了 X-AppEngine-FailFast 的请求占用现有实例,则没有设置该标头的请求仍可能会导致新实例启动。
尽可能传送静态内容
静态内容的传送由专用的 App Engine 基础架构处理,因此不会消耗实例小时数。
如果您需要设置自定义标头,请使用 Blobstore API。Blob 响应的实际传送不会消耗实例小时数。
管理应用存储空间
App Engine 会根据 Datastore 中实体的大小、Datastore 索引的大小、任务队列中的任务大小以及 Blobstore 中存储的数据量来计算存储空间费用。您可以执行以下操作来确保仅存储必要的数据:
- 删除应用不再需要的任何实体或 Blob。
- 移除所有不必要的索引,以降低索引存储空间费用(请参阅下面的“管理 Cloud Datastore 使用情况”部分)。
管理 Datastore 使用情况
App Engine 会考虑 Datastore 中执行的操作次数。使用如下所列的一些策略,可以减少 Datastore 资源使用量,以及缩短向 Datastore 发送的请求的延迟时间:
- Google Cloud 控制台数据查看器会显示在您的本地 Datastore 中创建每个实体所需的写入操作次数。您可以使用此信息来了解写入每个实体的费用。如需了解如何解读此数据,请参阅了解写入费用。
- 移除所有不必要的索引,这将减少存储空间费用和实体写入费用。您可以在 Google Cloud 控制台“搜索”页面中查看当前用于您的应用的索引。
- 设计数据模型时,您或许可以通过完全避免使用自定义索引的方式来编写查询。如需详细了解 App Engine 如何生成索引,请阅读查询和索引文档。
- 尽可能将已编入索引的属性(默认值)替换为没有编入索引的属性 (
),这将减少您在放入实体时 Datastore 执行的写入操作次数。请注意,如果您日后决定确实需要能够查询未编入索引的属性,则不仅需要修改代码以再次使用已编入索引的属性,而且还必须针对所有实体运行 map reduce 以重新放入这些实体。
- 由于在 App Engine 1.5.2 和 1.5.3 版本中改进了 Datastore 查询规划器,因此与以前相比,您的查询现在可能需要较少的索引。出于性能原因,您可能仍会选择保留某些自定义索引,但建议您删除其他自定义索引,以便降低存储空间费用和实体写入费用。
- 重新配置您的数据模型,以便将查询替换为按键提取,从而节省费用并提高效率。
- 尽可能使用仅限于键的查询来取代实体查询。
- 如需缩短延迟时间,请将多个实体
get()
替换为批量 get()
。
- 使用 Datastore 游标(而不是偏移)进行分页。
- 通过 Async Datastore API 同时载入多个 Datastore RPC。
注意:Datastore 小规模操作包括用于分配 Datastore ID 的调用操作或仅限于键的查询。如需详细了解费用,请参阅价格页面。
管理其他资源
建议您务必检查错误率,并留意您可能正在进行的任何无效调用。在某些情况下,或许可以尽早发现这些无效的调用。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-04。
[[["易于理解","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-09-04。"],[[["\u003cp\u003eApp Engine provides usage reports to help you understand your application's performance and resource consumption, with strategies available for more efficient resource management.\u003c/p\u003e\n"],["\u003cp\u003eReducing application latency, which can be achieved through techniques like increasing caching and using task queues, helps minimize the number of instances required to handle traffic.\u003c/p\u003e\n"],["\u003cp\u003eAdjusting the auto-scaling settings in the \u003ccode\u003eapp.yaml\u003c/code\u003e file allows for a trade-off between performance and resource load, with options outlined in the scaling elements section.\u003c/p\u003e\n"],["\u003cp\u003eOptimizing Task Queue settings, such as setting the X-AppEngine-FailFast header, can help conserve instance hours by managing how new instances are started.\u003c/p\u003e\n"],["\u003cp\u003eEfficient Datastore usage can be achieved by removing unnecessary indexes, replacing queries with key fetches, and utilizing batch operations and cursors.\u003c/p\u003e\n"]]],[],null,["# Managing App Resources\n\nApp Engine generates usage reports about your application's\nperformance and resources utilization. Listed below are potential strategies\nfor managing your resources more efficiently. For more information,\nsee the [pricing](/appengine/pricing) page.\n\nViewing usage reports\n---------------------\n\nWhen evaluating application performance, you should check the\nnumber of instances the application is running, and how the application\nconsumes resources.\n\n[View the dashboard usage reports](https://console.cloud.google.com/appengine)\n\n[View the Instances page](https://console.cloud.google.com/appengine/instances)\n\nThe following sections suggest some strategies for managing resources.\n\nManaging dynamic instance scaling\n---------------------------------\n\n### Decreasing latency\n\nApplication latency impacts the number of instances that are required to handle your\ntraffic. By decreasing latency, you can reduce the number of instances used to\nserve your application. [Cloud Trace](/trace) is a useful tool to view\ndata about latency and understand potential changes to decrease it.\n\nAfter using Cloud Trace to view your latency, try some of the following\nstrategies to reduce latency:\n\n- **Increase caching of frequently accessed shared data** - That's another way of saying - use App Engine Memcache. Also, setting your application's cache-control headers can have a significant impact on how efficiently your data is cached by servers and browsers. Even caching things for a few seconds can have an impact on how efficiently your application serves traffic.\n- **Use App Engine Memcache more efficiently** - Use batch calls for get, set, delete, etc instead of a series of individual calls.\n- **Use tasks for non-request bound functionality** - If your application performs work that can be done beyond the scope of a user-facing request, put it in a task! Sending this work to [Task Queue](/appengine/docs/legacy/standard/go111/taskqueue) instead of waiting for it to complete before returning a response can significantly reduce user-facing latency. Task Queue can then give you much more control over execution rates and help smooth out your load.\n- **Use Firestore in Datastore mode (Datastore) more efficiently** - See below for more detail.\n- **Execute multiple URL Fetch calls in parallel** :\n - Batch together multiple URL Fetch calls instead of handling them individually inside individual user-facing requests, and handle them in an offline task in parallel via async URL Fetch.\n - Use [goroutines](http://golang.org/doc/effective_go.html#concurrency).\n- **For HTTP sessions, write asynchronously**.\n\n### Change auto-scaling performance settings\n\nThe `app.yaml`\nconfiguration file contains several settings you can use to\nadjust the trade-off between performance and resource load for a specific version\nof your app.\nFor a list of the available auto-scaling settings, see\n[scaling elements](/appengine/docs/legacy/standard/go111/config/appref#scaling_elements).\nWatch the App Engine [New Scheduler Settings](https://www.youtube.com/watch?v=eUXUY7QFfAI)\nvideo to see the effects of these settings.\n**Note:** All Go instances have concurrent requests enabled automatically.\n\n### Configuring Task Queue settings\n\nThe default settings for Task Queue are tuned for performance. With these\ndefaults, when you put several tasks into a queue simultaneously, they will\nlikely cause new Frontend Instances to start. Here are some suggestions for\nhow to tune Task Queue to conserve Instance Hours:\n\n- Set the X-AppEngine-FailFast header on tasks that are not latency sensitive. This header instructs the scheduler to immediately fail the request if an existing instance is not available. Task Queue will retry and back-off until an existing instance becomes available to service the request. However, it is important to note that when requests with X-AppEngine-FailFast set occupy existing instances, requests without that header set may still cause new instances to be started.\n\n### Serve static content where possible\n\nStatic content serving\n\n\nis handled by specialized App Engine infrastructure, which does not consume\nInstance Hours.\n\n\nIf you need to set custom headers, use the\n[Blobstore API](/appengine/docs/legacy/standard/go111/blobstore). The actual serving of\nthe Blob response does not consume Instance Hours.\n\n\nManaging application storage\n----------------------------\n\nApp Engine calculates storage costs based on the size of entities in the\nDatastore, the size of Datastore indexes, the size\nof tasks in the task queue, and the amount of data stored in Blobstore. Here are\nsome things you can do to make sure you don't store more data than necessary:\n\n- Delete any entities or blobs your application no longer needs.\n- Remove any unnecessary indexes, as discussed in the *Managing Datastore\n Usage* section below, to reduce index storage costs.\n\nManaging Datastore usage\n------------------------\n\nApp Engine accounts for the number of operations performed in\nDatastore. Here are a few strategies that can result in reduced\nDatastore resource consumption, as well as lower latency for\nrequests to Datastore:\n\n- The Google Cloud console dataviewer displays the number of write ops that were required to create every entity in your local Datastore. You can use this information to understand the cost of writing each entity. See [Understanding Write Costs](/appengine/docs/legacy/standard/java/datastore/entities#Understanding_Write_Costs) for information on how to interpret this data.\n- Remove any unnecessary indexes, which will reduce storage and entity write costs. You can see what indexes are currently serving for your application in the [Google Cloud console Search page](https://console.cloud.google.com/project/_/appengine/search).\n- When designing your data model, you might be able to write your queries in such a way so as to avoid custom indexes altogether. Read [Queries and Indexes](/appengine/docs/legacy/standard/python/datastore/queries) documentation for more information on how App Engine generates indexes.\n- Whenever possible, replace indexed properties (which are the default) with unindexed properties ( ), which reduces the number of Datastore write operations when you put an entity. Caution, if you later decide that you do need to be able to query on the unindexed property, you will need to not only modify your code to again use indexed properties, but you will have to run a [map reduce](https://github.com/googlecloudplatform/appengine-mapreduce) over all entities to reput them.\n- Due to Datastore query planner improvements in App Engine 1.5.2 and 1.5.3 releases, your queries may now require fewer indexes than they did previously. While you may still choose to keep certain custom indexes for performance reasons, you may be able to delete others, reducing storage and entity write costs.\n- Reconfigure your data model so that you can [replace queries with fetch by\n key](/appengine/docs/legacy/standard/go/datastore/reference#Get) which is cheaper and more efficient.\n- Use keys-only queries instead of entity queries when possible.\n- To decrease latency, replace multiple entity `get()`s with a batch `get()`.\n- Use Datastore cursors for pagination rather than offset.\n- Parallelize multiple Datastore RPCs via the [async datastore API](http://golang.org/doc/effective_go.html#concurrency).\n\n**Note:** Small Datastore operations include calls to allocate\nDatastore ids or keys-only queries. See the\n[pricing](/appengine/pricing#cost_resource) page for more information on costs.\n\nManaging other resources\n------------------------\n\nIt's always a good idea to make sure you are checking your error rates and looking out for any invalid calls you might be making. In some cases it might be possible to catch those calls early.\n\n\u003cbr /\u003e"]]