Apigee 提供极大的灵活性,让您能够基于此模式构建复杂的行为。例如,您可以添加政策来控制 API 在将客户端请求发送到后端服务之前处理客户端请求的方式,或者在将从后端收到的响应转发到客户端之前对其进行处理。您可以使用服务调出政策调用其他服务,通过添加 JavaScript 代码添加自定义行为,甚至可以创建不调用后端服务的 API 代理。
反模式
从技术上讲,您可以使用服务调出在没有目标端点路由的 API 代理中调用后端服务,但这会导致外部服务性能的分析数据丢失。
在您不需要将请求消息转发到 TargetEndpoint 的情况下,不包含目标路由的 API 代理非常有用。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"]],["最后更新时间 (UTC):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)"]]