Skip to content

Http proxy

Regunath B edited this page Sep 6, 2013 · 12 revisions

The Http proxy leverages the Phantom extensibility support as described in Phantom architecture. A simpler example for Http proxy is available on the Getting Started and Examples page. A typical Http proxy for accessing upstream services (say JSON/REST or XML/REST) requires the following capabilities:

  • TCP socket listener - that can receive Http requests. This is available as TCPNettyServer in the runtime module.
  • Http Codecs - interpret Http protocol semantics. This is readily available as Netty Http codecs
  • Request processing - processes Http commands and relays it to the target server. This is available in the channel-handler-http module
  • Resilience and fault tolerance integration - to support timeouts, synchronous and asynchronous invocation and fallbacks. This is available in the task-http module.

Follow the instructions below to configure your Http proxy:

  • Create a file by name spring-proxy-listener-config.xml and configure the TCP socket listener, the Channel pipeline factory, Http proxy handler, and the Phantom registry and repository beans (or) simply copy this file from sample-http-proxy Maven module. Note: the path src/main/resources/external must be preserved as the location for this file. The following bean configuration is worth noting:
<bean id="httpRequestHandler" class="com.flipkart.phantom.runtime.impl.server.netty.handler.http.HttpChannelHandler" scope="prototype">
    <property name="repository" ref="httpProxyRepository"/>
    <property name="defaultProxy" value="defaultProxy" />
</bean>

This implies that all Http requests handled by the "httpRequestHandler" is routed to a proxy by name "defaultProxy". However, it may be desirable to have separate proxy configurations by request-method (GET/POST) or by URI parameters that identify an action/route. In order to do this, you will need to use com.flipkart.phantom.runtime.impl.server.netty.handler.http.MethodRoutingHttpChannelHandler or customize the com.flipkart.phantom.runtime.impl.server.netty.handler.http.RoutingHttpChannelHandler to map each request-method/ action/route to an independent proxy like:

<bean id="httpRequestHandler" class="com.flipkart.phantom.runtime.impl.server.netty.handler.http.MethodRoutingHttpChannelHandler" scope="prototype">
    <property name="repository" ref="httpProxyRepository"/>
    <property name="defaultProxy" value="defaultProxy"/>
    <property name="proxyMap">
        <map>
            <entry key="GET" value="listingProxy" />
            <entry key="POST" value="updateProxy" />
        </map> 
    </property>
</bean>
  • Create a file by name spring-proxy-handler-config.xml in the same location and configure the proxy beans like:
<!-- default http proxy -->
<bean id="defaultProxy" class="com.flipkart.phantom.http.impl.SimpleHttpProxy">
    <property name="name" value="defaultProxy" />
    <property name="pool" ref="listingConnectionPool" />
</bean>

<!-- http proxy for GET-->
<bean id="getHttpProxy" class="com.flipkart.phantom.http.impl.SimpleHttpProxy">
    <property name="name" value="listingProxy" />
    <property name="pool" ref="listingConnectionPool" />
</bean>
<!-- http proxy for POST-->
<bean id="postHttpProxy" class="com.flipkart.phantom.http.impl.SimpleHttpProxy">
    <property name="name" value="updateProxy" />
    <property name="pool" ref="updateConnectionPool" />
</bean>

<!-- http connection pool for GET -->
<bean id="listingConnectionPool" class="com.flipkart.phantom.http.impl.HttpConnectionPool">
    <property name="host" value="www.flipkart.com" />
    <property name="port" value="80" />
    <property name="connectionTimeout" value="10000" />
    <property name="operationTimeout" value="1000" />
</bean>
<!-- http connection pool for POST -->
<bean id="updateConnectionPool" class="com.flipkart.phantom.http.impl.HttpConnectionPool">
    <property name="host" value="www.flipkart.com" />
    <property name="port" value="80" />
    <property name="connectionTimeout" value="10000" />
    <property name="operationTimeout" value="2000" />
</bean>

The following configuration options and features are worth noting:

  • Each proxy uses a pool configuration. Internally, this creates a Http connection pool for each configuration
  • The Http host and port of the target server are configurable on the pool
  • Http connection timeout may be configured
  • Operation timeout is a very important configuration to ensure fail-fast behavior, prevent resource locking and trigger the fallback behavior.

Full sample listener and proxy handler configurations are available here : Routing Listener Configuration , Routing proxy handler configuration