Antipola: Memanggil proxy dalam proxy menggunakan kode kustom atau sebagai target
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Anda sedang melihat dokumentasi Apigee dan Apigee hybrid.
Lihat dokumentasi
Apigee Edge.
Apigee memungkinkan Anda memanggil satu proxy API dari proxy API lain. Fitur ini berguna terutama jika
Anda memiliki proxy API yang berisi kode yang dapat digunakan kembali dan dapat digunakan oleh proxy API lainnya.
Antipola
Memanggil satu proxy API dari proxy lain menggunakan HTTPTargetConnection di endpoint target atau kode JavaScript kustom akan menghasilkan hop jaringan tambahan.
Memanggil proxy 2 dari proxy 1 menggunakan HTTPTargetConnection
Contoh kode berikut memanggil proxy 2 dari proxy 1 menggunakan HTTPTargetConnection:
Untuk memahami mengapa hal ini memiliki kelemahan bawaan, kita perlu memahami rute yang diambil permintaan
seperti yang diilustrasikan oleh diagram di bawah:
Gambar 1: Alur kode
Seperti yang digambarkan dalam diagram, permintaan melintasi beberapa komponen terdistribusi, termasuk
Router dan Message Processor.
Dalam contoh kode di atas, memanggil proxy 2 dari proxy 1 berarti permintaan harus dirutekan
melalui rute tradisional (Router > MP) saat runtime. Hal ini akan mirip dengan memanggil API
dari klien sehingga membuat beberapa hop jaringan yang menambah latensi. Hop ini
tidak diperlukan mengingat permintaan proxy 1 telah menjangkau MP.
Dampak
Memanggil satu proxy API dari proxy API lain akan menyebabkan hop jaringan yang tidak perlu, yaitu
permintaan harus diteruskan dari satu Message Processor ke Message Processor lainnya.
Praktik terbaik
Gunakan fitur rantai proxy
untuk memanggil satu Proxy API dari proxy lainnya. Rantai proxy lebih
efisien karena menggunakan koneksi lokal untuk mereferensikan endpoint target (Proxy API lain).
Contoh kode menunjukkan rantai proxy menggunakan LocalTargetConnection dalam definisi endpoint Anda:
Proxy API yang dipanggil akan dieksekusi dalam Message Processor yang sama; sehingga, proxy API ini menghindari
hop jaringan seperti yang ditunjukkan pada gambar berikut:
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-04 UTC."],[[["\u003cp\u003eApigee allows invoking one API proxy from another, which is useful for reusing code, but it should be done efficiently.\u003c/p\u003e\n"],["\u003cp\u003eUsing \u003ccode\u003eHTTPTargetConnection\u003c/code\u003e or custom JavaScript to invoke one API proxy from another causes extra network hops, increasing latency.\u003c/p\u003e\n"],["\u003cp\u003eInvoking one API proxy from another creates unnecessary network hops because requests are routed through the Router and Message Processor again.\u003c/p\u003e\n"],["\u003cp\u003eProxy chaining using \u003ccode\u003eLocalTargetConnection\u003c/code\u003e is the best practice for invoking API proxies, as it executes within the same Message Processor and avoids network hops.\u003c/p\u003e\n"]]],[],null,["# Antipattern: Invoking a proxy within a proxy using custom code or as a target\n\n*You're viewing **Apigee** and **Apigee hybrid** documentation.\nView [Apigee Edge](https://docs.apigee.com/api-platform/antipatterns/proxy-within-proxy) documentation.*\n\nApigee lets you invoke one API proxy from another API proxy. This feature is useful especially if\nyou have an API proxy that contains reusable code that can be used by other API proxies.\n\nAntipattern\n-----------\n\nInvoking one API proxy from another either using `HTTPTargetConnection` in the target endpoint or\ncustom JavaScript code results in additional network hop.\n\n### Invoke proxy 2 from proxy 1 using `HTTPTargetConnection`\n\nThe following code sample invokes proxy 2 from proxy 1 using `HTTPTargetConnection`: \n\n```world-of-warcraft-toc\n\u003c!-- /antipatterns/examples/2-1.xml --\u003e\n\u003cHTTPTargetConnection\u003e\n \u003cURL\u003ehttps://api-test.example.com/proxy2\u003c/URL\u003e\n\u003c/HTTPTargetConnection\u003e\n```\n\n### Invoke proxy 2 from proxy 1 from the JavaScript code\n\nThe next code sample invokes proxy 2 from proxy 1 using JavaScript: \n\n```gdscript\n\u003c!-- /antipatterns/examples/2-2.xml --\u003e\nvar response = httpClient.send('https://api-test.example.com/proxy2);\nresponse.waitForComplete();\n```\n\n### Code Flow\n\nTo understand why this has an inherent disadvantage, we need to understand the route a request\ntakes as illustrated by the diagram below:\n**Figure 1**: Code flow\n\nAs depicted in the diagram, a request traverses multiple distributed components, including the\nRouter and the Message Processor.\n\nIn the code samples above, invoking proxy 2 from proxy 1 means that the request has to be routed\nthrough the traditional route (Router \\\u003e MP) at runtime. This would be akin to invoking an API\nfrom a client thereby making multiple network hops that add to the latency. These hops are\nunnecessary considering that proxy 1 request has already *reached* the MP.\n\nImpact\n------\n\nInvoking one API proxy from another API proxy incurs unnecessary network hops, that is the\nrequest has to be passed on from one Message Processor to another Message Processor.\n\nBest practice\n-------------\n\n- Use the [proxy chaining](/apigee/docs/api-platform/fundamentals/connecting-proxies-other-proxies) feature for invoking one API Proxy from another. Proxy chaining is more efficient as it uses local connection to reference the target endpoint (another API Proxy).\n\n The code sample shows proxy chaining using `LocalTargetConnection` in your endpoint\n definition: \n\n ```world-of-warcraft-toc\n \u003c!-- /antipatterns/examples/2-3.xml --\u003e\n \u003cLocalTargetConnection\u003e\n \u003cAPIProxy\u003eproxy2\u003c/APIProxy\u003e\n \u003cProxyEndpoint\u003edefault\u003c/ProxyEndpoint\u003e\n \u003c/LocalTargetConnection\u003e\n ```\n\n The invoked API Proxy gets executed within the same Message Processor; as a result, it avoids\n the network hop as shown in the following figure:\n **Figure 2**: Code flow with proxy chaining\n\nFurther reading\n---------------\n\n- [Proxy chaining](/apigee/docs/api-platform/fundamentals/connecting-proxies-other-proxies)"]]