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 叫用來執行這些服務的程式碼,例如透過使用者要求或符合 REST 樣式的 API 呼叫。一項服務中的程式碼無法直接呼叫另一服務中的程式碼。程式碼可獨立部署至服務中,且不同服務可使用不同語言編寫,例如 Python、Java、Go 和 PHP。系統會分別管理各服務的自動調度資源流程、負載平衡和機器執行個體類型。

服務中的版本
此外,每項服務都可同時部署多個「版本」。每項服務都會提供一個預設版本,但由於各服務的每個版本都有對應位址,因此您也可以直接存取已部署的其他所有版本。這個結構可帶來諸多可能,包括對新版本進行冒煙測試、在不同版本之間進行 A/B 測試,以及簡化向前復原和復原作業。App Engine 架構針對大部分的這些項目提供輔助機制。我們會在後續各節中深入說明這些機制。

服務區隔
雖然大部分服務都各自獨立,但還是會共用部分 App Engine 資源。舉例來說,App Engine 專案中的服務會共用 Cloud Datastore、Memcache 和工作佇列資源。雖然這樣可帶來一些好處,但微服務型應用程式還是得讓不同微服務的程式碼和資料保持獨立。有些架構模式可協助減少不必要的共用情況,詳情請見後文。

專案區隔
如果您不想依賴這些模式來達到區隔效果,並想以更正式的方式強制實行區隔,那麼您可以使用多項 App Engine 專案。使用專案而非服務有好有壞,您必須視自己的情況進行取捨。除非您特別需要使用多項專案帶來的某項好處,否則建議您先在單一專案使用多項服務,因為這種做法效能較佳,管理方面的負擔也可降至最低。當然,您也可以選擇混用這兩種做法。
服務區隔和專案區隔的比較
下表提供在微服務架構中使用多項服務和多項專案的比較:
|
多項服務
|
多項專案 |
程式碼區隔 |
不同服務和版本的已部署程式碼各自獨立。 |
不同專案的已部署程式碼各自獨立,各專案中不同服務和版本的已部署程式碼亦同。 |
資料區隔
|
不同服務和版本會共用 Cloud Datastore 和 Memcache,但可使用「命名空間」做為開發人員模式以區隔資料。針對工作佇列區隔,您可以採用開發人員的佇列命名慣例,例如 user-service-queue-1 。 |
不同專案的 Cloud Datastore、Memcache 和工作佇列彼此完全獨立。 |
記錄區隔
|
每項服務 (和版本) 都有獨立記錄,不過這些記錄可以合併查看。 |
每項專案 (以及各專案的服務和版本) 都有獨立記錄,不過特定專案的所有記錄可以合併查看。不同專案的記錄無法合併查看。 |
效能負擔
|
相同專案的服務會部署在相同資料中心,因此使用 HTTP 從某項服務呼叫其他服務的延遲非常低。 |
專案可能會部署在不同的資料中心,因此 HTTP 延遲可能較高,不過 Google 提供世界一流的網路,因此延遲仍然頗低。 |
成本會計
|
各項服務的執行個體時數 (用於執行程式碼的 CPU 和記憶體) 成本不會分開;整個專案的所有執行個體時數會合併在一起。 |
不同專案的成本會分開,方便您瞭解不同微服務的成本。 |
操作者權限
|
操作者可部署程式碼、向前復原和復原版本,以及查看專案中所有服務的記錄。您無法限制對特定服務的存取權。 |
不同專案的操作者權限可分開管理。 |
要求追蹤
|
透過 Google Cloud Trace,您可以針對相同專案中的服務查看要求和所產生微服務要求的追蹤記錄,而且所有追蹤記錄會整合成一筆記錄。有了這項功能,您就能以更輕鬆的方式提高效能。 |
如果 Cloud Trace 呼叫隸屬於同一個
機構,系統就能以視覺化的方式呈現不同 GCP 專案中的 Cloud Trace 呼叫。 |
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間: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"]],["上次更新時間: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."]]