Python 2.7 已达到支持终止期限,并将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Python 2.7 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 Python 2.7 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Python 版本。
Google App Engine 上的微服务架构
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
微服务是指用于开发应用的架构样式。通过微服务,可将大型应用分解成多个独立的组件,其中每个组件都有各自的责任领域。在处理一个用户请求或 API 请求时,基于微服务的应用可能会调用许多内部微服务来共同生成其响应。
如果能以正确的方式实现基于微服务的应用,则可以实现以下目标:
- 定义各项微服务之间的强契约。
- 允许相互独立的部署周期,包括回滚。
- 方便在子系统上执行并发 A/B 版本测试。
- 最大限度地减少测试自动化开销和质量保证开销。
- 提高日志记录和监控的清晰度。
- 提供精细的成本核算。
- 提高应用的整体可扩缩性和可靠性。
Google App Engine 具备多种非常适合基于微服务的应用的功能。本页面介绍了在 Google App Engine 上将应用部署为基于微服务的应用时可以采用的最佳做法。
将 App Engine 服务用作微服务
在 App Engine 项目中,您可以将多项微服务分别部署为独立的服务(以前在 App Engine 中称为“模块”)。这些服务的代码彼此之间完全隔离;如需执行这些服务中的代码,唯一的方式是通过 HTTP 调用,例如用户请求或 RESTful API 调用。一项服务中的代码不能直接调用另一项服务中的代码。您可以将代码单独部署到各项服务,并且可以使用 Python、Java、Go 和 PHP 等不同的语言编写不同的服务。对于这些服务,自动扩缩、负载均衡和机器实例类型均可单独管理。

服务中的版本
每项服务还可以同时部署多个版本。对于每项服务,其中一个版本是默认服务版本,但您可以直接访问服务的任何已部署版本,因为每项服务的每个版本都有自己的地址。这种结构开创了无数种可能性,包括对新版本执行冒烟测试、在不同版本之间执行 A/B 测试以及简化了前滚和回滚操作。App Engine 框架提供多种机制以帮助执行其中大多数操作,我们将在后面的部分详细介绍这些机制。

服务隔离
虽然大部分服务都是相互隔离的,但这些服务会共享一些 App Engine 资源。例如,Cloud Datastore、Memcache 和任务队列都是 App Engine 项目中各服务之间的共享资源。虽然这种共享具有一定优势,但对基于微服务的应用来说,保持微服务之间的代码隔离性和数据隔离性非常重要。有一些架构模式可以帮助减少不必要的共享,我们将在本文后面的部分介绍这些模式。

项目隔离
如果您不想依赖这些模式来实现隔离,而是希望更正式地实施分离,则可以使用多个 App Engine 项目。使用多个项目(而非多项服务)有利有弊,您需要根据自己的情况进行权衡。除非您对因使用多个项目而带来的某项优势有特定需求,否则最好先在单个项目中使用多项服务,因为这样性能会更好,管理开销也将减至最低。当然,您也可以选择混用这两种方法。
服务隔离与项目隔离的对比
下表展示了在微服务架构中使用多项服务与使用多个项目之间的对比:
|
多项服务 |
多个项目 |
代码隔离 |
在服务和版本之间,部署的代码完全独立。 |
在项目之间以及每个项目的服务和版本之间,部署的代码完全独立。 |
数据隔离 |
Cloud Datastore 和 Memcache 可在各服务和版本之间共享,但命名空间可用作一种开发者模式来隔离数据。对于任务队列隔离,可以使用开发者队列名称约定,例如 user-service-queue-1 。 |
在各项目之间,Cloud Datastore、Memcache 和任务队列完全独立。 |
日志隔离 |
每项服务(和每个版本)都具有独立的日志,可统一查看所有这些日志。 |
每个项目(以及每个项目的服务和版本)都具有独立的日志,但同一项目的所有日志可同时查看。无法统一查看多个项目的日志。 |
性能开销 |
同一项目的服务部署在同一个数据中心内,因此使用 HTTP 从一项服务调用另一项服务的延迟时间非常短。 |
项目可能部署在不同的数据中心内,因此 HTTP 延迟时间可能会更长,但由于 Google 拥有世界一流的网络,因此延迟时间仍然相当短。
|
成本核算
|
各服务的实例小时费用(用于运行代码的 CPU 和内存)未分开核算;整个项目的所有实例小时都集中在一起。 |
不同项目的费用是分开核算的,因此不同微服务的费用一目了然。 |
操作员权限 |
操作员可以部署代码、前滚和回滚版本,并查看项目的所有服务的日志。无法限制对特定服务的访问权限。 |
可在不同项目中单独控制操作员的访问权限。 |
请求跟踪 |
借助 Google Cloud Trace,您可以通过一个组合跟踪记录,集中查看某个请求及其引发的对同一项目中多项服务发出的微服务请求。此功能可帮助您轻松优化性能。 |
如果多个 GCP 项目属于同一组织,您可以跨这些项目直观呈现 Cloud Trace 调用。
|
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 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\u003eMicroservices architecture allows large applications to be broken down into independent parts, each with specific responsibilities, which communicate to fulfill user or API requests.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine supports microservices through separate services, enabling code isolation, independent deployment cycles, and the use of different programming languages.\u003c/p\u003e\n"],["\u003cp\u003eWithin each service, multiple versions can exist concurrently, facilitating A/B testing, rollbacks, and simplified deployments.\u003c/p\u003e\n"],["\u003cp\u003eWhile services within an App Engine project share resources like Cloud Datastore and Memcache, developers can use namespaces and naming conventions to maintain data isolation, or use separate App Engine projects for more enforced isolation.\u003c/p\u003e\n"],["\u003cp\u003eUsing multiple projects offers strong separation of resources, cost accounting and operator permissions, but using multiple services in the same project is recommended for better performance and lower overhead, unless project specific isolation is required.\u003c/p\u003e\n"]]],[],null,["# Microservices Architecture on Google App Engine\n\n*Microservices* refers to an architectural style for\ndeveloping applications. Microservices allow a large application to be\ndecomposed into independent constituent parts, with each part having its own\nrealm of responsibility. To serve a single user or API request,\na microservices-based application can call many internal microservices\nto compose its response.\n\nA properly implemented microservices-based application can achieve the\nfollowing goals:\n\n- Define strong contracts between the various microservices.\n- Allow for independent deployment cycles, including rollback.\n- Facilitate concurrent, A/B release testing on subsystems.\n- Minimize test automation and quality-assurance overhead.\n- Improve clarity of logging and monitoring.\n- Provide fine-grained cost accounting.\n- Increase overall application scalability and reliability.\n\nGoogle App Engine has a number of features that are well-suited for a\nmicroservices-based application. This page outlines best practices to use\nwhen deploying your application as a microservices-based application on Google\nApp Engine.\n\nApp Engine Services as microservices\n------------------------------------\n\nIn an App Engine project, you can deploy multiple microservices as separate\n*[services](/appengine/docs/legacy/standard/python/an-overview-of-app-engine)* , previously known as\n*modules* in App Engine. These services have full isolation of code; the only\nway to execute code in these services is through an HTTP invocation, such as a\nuser request or a RESTful API call. Code in one service can't directly call code\nin another service. Code can be deployed to services independently, and\ndifferent services can be written in different languages, such as Python, Java,\nGo, and PHP. Autoscaling, load balancing, and machine instance types are all\nmanaged independently for services.\n\nVersions within services\n------------------------\n\nFurthermore, each service can have multiple *versions* deployed simultaneously.\nFor each service, one of these versions is the default serving version, though\nit is possible to directly access any deployed version of a service as each\nversion of each service has its own address. This structure opens up myriad\npossibilities, including smoke testing a new version, A/B testing between\ndifferent versions, and simplified roll-forward and rollback operations. The App\nEngine framework provides mechanisms to assist with most of these items. We'll\ncover these mechanisms in more detail in upcoming sections.\n\nService isolation\n-----------------\n\nThough mostly isolated, services share some App Engine resources. For example,\nCloud Datastore, Memcache, and Task Queues are all shared resources between services\nin an App Engine project. While this sharing has some advantages,\nit's important for a microservices-based application to maintain code- and\ndata-isolation between microservices. There are architecture patterns that help\nmitigate unwanted sharing. We'll describe these patterns later in this article.\n\nProject isolation\n-----------------\n\nIf you don't want to rely on these patterns to achieve isolation and you want a\nmore formal enforcement of separation, you can use multiple App Engine projects.\nThere are pros and cons to using projects instead of services, and you must\nbalance the tradeoffs depending on your situation. Unless you have a specific\nneed for one of the advantages offered by using multiple projects, it's best to\nstart with using multiple services within a single project because performance\nwill be better and the administrative overhead will be minimized. Of course, you\ncan also choose some hybrid of the two approaches.\n\nComparison of service isolation and project isolation\n-----------------------------------------------------\n\nThe following table provides a comparison between using multiple services and\nmultiple projects in a microservices architecture:\n\nWhat's next\n-----------\n\n- Understand how to [create and name dev, test, qa, staging, and production environments with microservices in App Engine](/appengine/docs/legacy/standard/python/creating-separate-dev-environments).\n- Learn the [best practices for designing APIs to communicate between microservices](/appengine/docs/legacy/standard/python/designing-microservice-api).\n- Learn the [best practices for microservice performance](/appengine/docs/legacy/standard/python/microservice-performance).\n- Learn how to [Migrate an existing monolithic application to one with microservices](/appengine/docs/legacy/standard/python/microservice-migration).\n- Understand if microservices are ideal for your situation. On his personal blog, Google Solution Architect Preston Holmes has [published a post about some of the drawbacks](http://www.ptone.com/dablog/2015/07/microservices-may-be-the-new-premature-optimization/) he sees in microservices."]]