Apigee 提供極大的彈性,可讓您在這個模式上建構複雜的行為。舉例來說,您可以新增政策來控制 API 處理用戶端要求的方式,然後再將要求傳送至後端服務,或是在將從後端服務收到的回應轉寄給用戶端前,先加以操控。您可以使用服務呼叫政策來叫用其他服務,也可以透過新增 JavaScript 程式碼來新增自訂行為,甚至可以建立不叫用後端服務的 API 代理程式。
反模式
在沒有前往目標端點路徑的 API Proxy 中,使用服務呼叫來叫用後端服務在技術上是可行的,但會導致外部服務的效能分析資料遺失。
如果您不需要將要求訊息轉送至 TargetEndpoint,不含目標路徑的 API Proxy 就非常實用。而是由 ProxyEndpoint 執行所有必要的處理作業。舉例來說,ProxyEndpoint 可以從查詢 API 服務的鍵/值儲存庫擷取資料,並在未呼叫後端服務的情況下傳回回應。
[[["容易理解","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-05 (世界標準時間)。"],[[["\u003cp\u003eThis documentation covers API proxies within Apigee and Apigee hybrid, which are managed facades for backend services, consisting of a ProxyEndpoint and a TargetEndpoint.\u003c/p\u003e\n"],["\u003cp\u003eWhile technically possible, using service callouts to invoke a backend service in an API proxy without a defined target endpoint results in the loss of analytics data regarding the performance of the external service.\u003c/p\u003e\n"],["\u003cp\u003eAn API proxy can be configured with a "null route" to create a "no target" proxy that does not invoke a backend service, but this configuration should not include service callouts.\u003c/p\u003e\n"],["\u003cp\u003eBest practice dictates that if an API proxy interacts with a single external service, it should define that service as the target endpoint, rather than relying on a no-target configuration with service callouts.\u003c/p\u003e\n"],["\u003cp\u003eService callouts are intended to support mashup scenarios, not to replace target endpoint invocation, as such, invoking a service callout in a no-target proxy should be avoided.\u003c/p\u003e\n"]]],[],null,["# Antipattern: Use Service Callout Policy to invoke a backend service in a No Target API Proxy\n\n*You're viewing **Apigee** and **Apigee hybrid** documentation.\nView [Apigee Edge](https://docs.apigee.com/api-platform/antipatterns/service-callout-no-target) documentation.*\n\n\nAn API proxy is a managed facade for backend services. A basic API proxy configuration consists of a\n[ProxyEndpoint](/apigee/docs/api-platform/fundamentals/understanding-apis-and-api-proxies)\n(defining the URL of the API proxy) and a\n[TargetEndpoint](/apigee/docs/api-platform/fundamentals/understanding-apis-and-api-proxies)\n(defining the URL of the backend service).\n\n\nApigee offers a lot of flexibility for building sophisticated behavior on top of this pattern.\nFor example, you can add policies to control the way the API processes a client request before sending\nit to the backend service, or manipulate the response received from the backend service before\nforwarding it to the client. You can invoke other services using\n[service callout policies](/apigee/docs/api-platform/reference/policies/service-callout-policy),\nadd custom behavior by adding [Javascript code](/apigee/docs/api-platform/reference/policies/javascript-policy),\nand even create an API proxy that doesn't invoke a backend service.\n\nAntipattern\n-----------\n\n\nUsing service callouts to invoke a backend service in an API proxy with no routes to a target endpoint\nis technically feasible, but results in the loss of analytics data about the performance of the\nexternal service.\n\n\nAn API proxy that does not contain target routes can be useful in cases where you do not need to forward\nthe request message to the TargetEndpoint. Instead, the ProxyEndpoint performs all necessary\nprocessing. For example, the ProxyEndpoint could retrieve data from a lookup to the API service's\nkey/value store and return the response without invoking a backend service.\n\n\nYou can define a [null Route](/apigee/docs/api-platform/reference/api-proxy-configuration-reference#proxyendpoint-nullroutes)\nin an API proxy, as shown here: \n\n```text\n\u003cRouteRule name=\"noroute\"/\u003e\n```\n\n\nA proxy using a null route is a \"no target\" proxy, because it does not invoke a target backend service.\n\n\nIt is technically possible to add a service callout to a no target proxy to invoke an external service, as shown in the example below: \n\n```world-of-warcraft-toc\n\u003c!-- /antipatterns/examples/service-callout-no-target-1.xml --\u003e\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?\u003e\n\u003cProxyEndpoint name=\"default\"\u003e\n \u003cDescription/\u003e\n \u003cFaultRules/\u003e\n \u003cPreFlow name=\"PreFlow\"\u003e\n \u003cRequest\u003e\n \u003cStep\u003e\n \u003cName\u003eServiceCallout-InvokeBackend\u003c/Name\u003e\n \u003c/Step\u003e\n \u003c/Request\u003e\n \u003cResponse/\u003e\n \u003c/PreFlow\u003e\n \u003cPostFlow name=\"PostFlow\"\u003e\n \u003cRequest/\u003e\n \u003cResponse/\u003e\n \u003c/PostFlow\u003e\n \u003cFlows/\u003e\n \u003cHTTPProxyConnection\u003e\n \u003cBasePath\u003e/no-target-proxy\u003c/BasePath\u003e\n \u003cProperties/\u003e\n \u003cVirtualHost\u003esecure\u003c/VirtualHost\u003e\n \u003c/HTTPProxyConnection\u003e\n \u003cRouteRule name=\"noroute\"/\u003e\n\u003c/ProxyEndpoint\u003e\n```\n\n\nHowever, the proxy cannot provide analytics information about the external service behavior (such as processing time or error rates), making it difficult to assess the performance of the external service.\n\nImpact\n------\n\n- Analytics information on the interaction with the external service ( error codes, response time, target performance, etc.) is unavailable\n- Any specific logic required before or after invoking the service callout is included as part of the overall proxy logic, making it harder to understand and reuse.\n\nBest Practice\n-------------\n\n\nIf an API proxy interacts with only a single external service, the proxy should follow the basic\ndesign pattern, where the backend service is defined as the target endpoint of the API proxy. A proxy\nwith no routing rules to a target endpoint should not invoke a backend service using the ServiceCallout policy.\n\n\nThe following proxy configuration implements the same behavior as the example above, but follows best\npractices: \n\n```carbon\n\u003c!-- /antipatterns/examples/service-callout-no-target-2.xml --\u003e\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?\u003e\n\u003cProxyEndpoint name=\"default\"\u003e\n \u003cDescription/\u003e\n \u003cFaultRules/\u003e\n \u003cPreFlow name=\"PreFlow\"\u003e\n \u003cRequest/\u003e\n \u003cResponse/\u003e\n \u003c/PreFlow\u003e\n \u003cPostFlow name=\"PostFlow\"\u003e\n \u003cRequest/\u003e\n \u003cResponse/\u003e\n \u003c/PostFlow\u003e\n \u003cFlows/\u003e\n \u003cHTTPProxyConnection\u003e\n \u003cBasePath\u003e/simple-proxy-with-route-to-backend\u003c/BasePath\u003e\n \u003cProperties/\u003e\n \u003cVirtualHost\u003esecure\u003c/VirtualHost\u003e\n \u003c/HTTPProxyConnection\u003e\n \u003cRouteRule name=\"default\"\u003e\n \u003cTargetEndpoint\u003edefault\u003c/TargetEndpoint\u003e\n \u003c/RouteRule\u003e\n\u003c/ProxyEndpoint\u003e\n```\n\n\nUse Service callouts to support mashup scenarios, where you want to invoke external services before\nor after invoking the target endpoint. Service callouts are not meant to replace target endpoint\ninvocation.\n\nFurther reading\n---------------\n\n- [Understanding APIs and API proxies](/apigee/docs/api-platform/fundamentals/understanding-apis-and-api-proxies)\n- [How to configure Route Rules](/apigee/docs/api-platform/reference/api-proxy-configuration-reference#proxyendpoint-howtoconfigurerouterules)\n- [Null Routes](/apigee/docs/api-platform/reference/api-proxy-configuration-reference#proxyendpoint-nullroutes)\n- [Service Callout policy](/apigee/docs/api-platform/reference/policies/service-callout-policy)"]]