Go 1.11 已达到支持终止期限,将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Go 1.11 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 Go 1.11 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Go 版本。
配置预热请求以提高性能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在将应用代码加载到新创建的实例时,您可以使用预热请求来缩短请求与响应的延迟时间。
App Engine 经常需要将应用代码加载到全新实例中。以下情况下可能会加载实例:
- 重新部署应用版本时。
- 由于请求产生的负载超过当前运行中实例集的容量而创建新实例时。
- 维护和维修底层基础架构或物理硬件时。
将应用代码加载到新实例会产生加载请求。加载请求可能会延长用户的请求延迟时间,但您可以使用预热请求来避免此延时。预热请求会在任何实际请求到达新实例之前,先将应用的代码加载到新实例中。
如果您为应用启用了预热请求,则 App Engine 会尝试检测您的应用何时需要新实例,并发出预热请求来对新实例进行初始化。但是,此类检测尝试并不适用于所有情况。因此,即使应用中启用了预热请求,您也可能会遇到加载请求。例如,如果您的应用目前没有任何流量,则对应用的第一个请求始终会是加载请求,而不是预热请求。
与任何其他发送至 App Engine 应用的请求一样,预热请求也会使用实例小时数。在大多数启用了预热请求的情况下,您不会注意到实例小时数的增加,因为您的应用只是在预热请求(而非加载请求)中进行初始化。如果您决定执行更多工作,例如在预热请求期间预先缓存,则使用的实例小时数可能会增加。如果您将 min_idle_instances
设置为大于 0
,则在这些实例首次启动时,您可能会遇到预热请求,但在此之后这些实例将保持可用。
启用预热请求
预热请求由 App Engine 调度器使用,该调度器根据用户提供的配置来控制实例的自动扩缩。启用预热请求后,App Engine 会向 /_ah/warmup
发出 GET
请求。您可以针对此请求实现处理程序,以执行预先缓存应用数据等应用特定任务。
如果调度程序判断需要更多实例,便会启动实例。由于调度器使用预热请求来启动实例,因此即使停用了预热请求,它们也可能会出现在日志中。
请注意,系统不一定会调用预热请求。在某些情况下,系统会改为发送加载请求:例如,如果实例是第一个被启动的实例,或者流量出现急剧上升。但是,如果启用了预热请求,系统将“尽力”尝试向已经预热的实例发送请求。
要启用预热请求,请在 app.yaml
文件中的 inbound_services
指令下添加 warmup
元素,例如:
inbound_services:
- warmup
创建处理程序
创建一个处理程序,用于处理发送到 /_ah/warmup
的请求。您的处理程序应执行应用所需的任何预热逻辑。
如未另行说明,那么本页面中的内容已根据知识共享署名 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\u003eWarmup requests reduce latency by loading your app's code into a new instance before live requests arrive, avoiding delays caused by loading requests.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine uses warmup requests to initialize new instances when needed, such as during redeployments, increased traffic, or infrastructure maintenance.\u003c/p\u003e\n"],["\u003cp\u003eWarmup requests are triggered by App Engine issuing \u003ccode\u003eGET\u003c/code\u003e requests to \u003ccode\u003e/_ah/warmup\u003c/code\u003e, and you can implement handlers for this endpoint to perform tasks like pre-caching.\u003c/p\u003e\n"],["\u003cp\u003eEnabling warmup requests involves adding the \u003ccode\u003ewarmup\u003c/code\u003e element under the \u003ccode\u003einbound_services\u003c/code\u003e directive in your \u003ccode\u003eapp.yaml\u003c/code\u003e file, which activates the feature.\u003c/p\u003e\n"],["\u003cp\u003eWhile warmup requests aim to prevent loading requests, they are not guaranteed in all situations, such as when an instance is the very first one being started or if there is a sudden surge in traffic.\u003c/p\u003e\n"]]],[],null,["# Configuring Warmup Requests to Improve Performance\n\nYou can use warmup requests to reduce request and response latency during the\ntime when your app's code is being loaded to a newly created instance.\n\nApp Engine frequently needs to load your app's code into a fresh\ninstance. Loading an instance can happen in the following situations:\n\n- When you redeploy a version of your app.\n- When new instances are created due to the load from requests exceeding the capacity of the current set of running instances.\n- When maintenance and repairs of the underlying infrastructure or physical hardware occur.\n\nLoading your app's code to a new instance can result in [loading\nrequests](/appengine/docs/legacy/standard/go111/how-instances-are-managed#loading_requests).\nLoading requests can result in increased request latency for your users, but you\ncan avoid this latency using *warmup requests*. Warmup requests load your\napp's code into a new instance before any live requests reach that instance.\n\nIf warmup requests are enabled for your application, App Engine attempts\nto detect when your application needs a new instance and initiates a warmup\nrequest to initialize a new instance. However, these detection attempts do not\nwork in every case. As a result, you might encounter loading requests, even if\nwarmup requests are enabled in your app. For example, if your app is serving no\ntraffic, the first request to the app will always be a loading request, not a\nwarmup request.\n\nWarmup requests use instance hours like any other request to your\nApp Engine application. In most cases where warmup requests are enabled,\nyou won't notice an increase in instance hours because your application is\nsimply initializing in a warmup request instead of a loading request. Your\ninstance hour usage can increase if you decide to do more work, such as\npre-caching during a warmup request. If you set\n[`min_idle_instances`](/appengine/docs/legacy/standard/go111/config/appref#min_idle_instances)\nto greater than `0`, you might encounter warmup requests when those instances\nfirst start, but they will remain available after that time.\n\nEnabling warmup requests\n------------------------\n\nWarmup requests are used by the App Engine scheduler, which controls the\nauto scaling of instances based on user-supplied configuration. With warmup\nrequests enabled, App Engine issues `GET` requests to `/_ah/warmup`. You\ncan implement handlers for this request to perform application-specific tasks,\nsuch as pre-caching application data.\n\nThe scheduler starts up instances when it determines that more instances are\nneeded. Warmup requests may appear in\n[logs](/appengine/docs/legacy/standard/go111/writing-application-logs#viewing_logs)\neven if they are disabled because the scheduler uses them to start instances.\n\nNote that warmup requests are not guaranteed to be called. In some situations\nloading requests are sent instead: for example, if the instance is the first one\nbeing started up, or if there is a steep ramp-up in traffic. However, there\nwill be a \"best effort\" attempt to send requests to already warmed-up instances\nif warmup requests are enabled.\n\nTo enable warmup requests, add the `warmup` element under the\n[`inbound_services`](/appengine/docs/legacy/standard/go111/config/appref#inbound_services)\ndirective in your `app.yaml` file, for example: \n\n inbound_services:\n - warmup\n\n\u003cbr /\u003e\n\nCreating your handler\n---------------------\n\nCreate a handler that will process the requests that are sent to `/_ah/warmup`.\nYour handler should perform any warmup logic that is needed by your app.\n\n\n\n // Sample warmup demonstrates usage of the /_ah/warmup handler.\n package main\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"log\"\n \t\"net/http\"\n \t\"os\"\n \t\"time\"\n\n \t\"cloud.google.com/go/storage\"\n )\n\n var startupTime time.Time\n var client *storage.Client\n\n func main() {\n \t// Perform required setup steps for the application to function.\n \t// This assumes any returned error requires a new instance to be created.\n \tif err := setup(context.Background()); err != nil {\n \t\tlog.Fatalf(\"setup: %v\", err)\n \t}\n\n \t// Log when an appengine warmup request is used to create the new instance.\n \t// Warmup steps are taken in setup for consistency with \"cold start\" instances.\n \thttp.HandleFunc(\"/_ah/warmup\", func(w http.ResponseWriter, r *http.Request) {\n \t\tlog.Println(\"warmup done\")\n \t})\n \thttp.HandleFunc(\"/\", indexHandler)\n\n \tport := os.Getenv(\"PORT\")\n \tif port == \"\" {\n \t\tport = \"8080\"\n \t\tlog.Printf(\"Defaulting to port %s\", port)\n \t}\n\n \tlog.Printf(\"Listening on port %s\", port)\n \tif err := http.ListenAndServe(\":\"+port, nil); err != nil {\n \t\tlog.Fatal(err)\n \t}\n }\n\n // setup executes per-instance one-time warmup and initialization actions.\n func setup(ctx context.Context) error {\n \t// Store the startup time of the server.\n \tstartupTime = time.Now()\n\n \t// Initialize a Google Cloud Storage client.\n \tvar err error\n \tif client, err = storage.NewClient(ctx); err != nil {\n \t\treturn err\n \t}\n\n \treturn nil\n }\n\n // indexHandler responds to requests with our greeting.\n func indexHandler(w http.ResponseWriter, r *http.Request) {\n \tif r.URL.Path != \"/\" {\n \t\thttp.NotFound(w, r)\n \t\treturn\n \t}\n \tuptime := time.Since(startupTime).Seconds()\n \tfmt.Fprintf(w, \"Hello, World! Uptime: %.2fs\\n\", uptime)\n }\n\n\n\u003cbr /\u003e"]]