如何限制WSO2 ESB客户端仅与目标服务器建立一个TCP连接
Hey there! Let's break down these two WSO2 ESB technical questions with practical, actionable solutions — I’ve dealt with both scenarios plenty of times in production setups.
Persistent HTTP calls essentially mean reusing the same TCP connection for multiple requests (leveraging HTTP Keep-Alive) instead of establishing a new connection every time. This cuts down on TCP handshake overhead and boosts performance, especially for frequent external system calls. Here’s how to set it up:
Configure Axis2 Transport Parameters
Navigate toESB_HOME/repository/conf/axis2/axis2.xmland modify the HTTP core NIO sender to enable keep-alive:<transportSender name="http" class="org.apache.synapse.transport.nhttp.HttpCoreNIOSender"> <parameter name="http.non.blocking" locked="false">true</parameter> <parameter name="http.keep.alive" locked="false">true</parameter> <!-- Enable persistent connections --> <parameter name="http.connection.timeout" locked="false">180000</parameter> <!-- 3 mins timeout --> <parameter name="http.socket.timeout" locked="false">180000</parameter> <!-- Keep other existing parameters as is --> </transportSender>Enforce Persistence at the Endpoint Level
Add specific properties to your external system endpoint to ensure keep-alive is active for that target:<endpoint name="ExternalSystemEndpoint"> <address uri="http://your-external-system.com/api/v1"> <property name="HTTP_KEEPALIVE" value="true" scope="axis2"/> <property name="HTTP_SC_ACCEPTED" value="202"/> <!-- Optional, if your system uses 202 responses --> </address> </endpoint>Verify the Setup
Use a tool like Wireshark to capture traffic between ESB and the external system. You should see multiple HTTP requests using the same TCP connection (no repeated three-way handshakes for each request).
By default, WSO2 ESB opens new TCP connections as load increases. To lock it to just one connection, you’ll need to tweak the connection pool settings and enforce connection reuse:
Limit Per-Host Connections in Axis2
Update theHttpCoreNIOSenderconfiguration inaxis2.xmlto set the maximum connections per target host to 1:<transportSender name="http" class="org.apache.synapse.transport.nhttp.HttpCoreNIOSender"> <!-- Existing parameters --> <parameter name="http.max.connections.per.host" locked="false">1</parameter> <!-- Critical: limit to 1 connection per host --> <parameter name="http.max.total.connections" locked="false">100</parameter> <!-- Global total connections, adjust as needed --> <parameter name="http.connection.reuse" locked="false">true</parameter> <!-- Force connection reuse --> </transportSender>Strengthen Connection Reuse in the Endpoint
Add additional properties to your endpoint to prevent accidental new connections:<endpoint name="SingleConnTargetEndpoint"> <address uri="http://your-target-server.com/service"> <property name="HTTP_KEEPALIVE" value="true" scope="axis2"/> <property name="http.connection.reuse" value="true" scope="axis2"/> <property name="CLIENT_API_NON_BLOCKING" value="true" scope="axis2"/> <!-- Optional: Maintain cookie session if the target server requires it --> <property name="COOKIE" expression="get-property('transport','Cookie')" scope="transport"/> </address> <suspendOnFailure> <initialDuration>-1</initialDuration> <progressionFactor>1.0</progressionFactor> </suspendOnFailure> </endpoint>Key Notes to Remember
- Ensure the target server also has HTTP Keep-Alive enabled — if it closes connections immediately, ESB will have to create new ones regardless of your settings.
- A single TCP connection means all requests are processed serially. Make sure your target server can handle the request volume in this mode, otherwise you’ll get request backlogs.
- Validate the connection count via the WSO2 Carbon Console’s Transport Statistics dashboard to confirm only one connection is active.
内容的提问来源于stack exchange,提问作者Hassan Butt

