Skip to content

Getting started and Examples

Regunath B edited this page May 23, 2016 · 12 revisions

This page is a quick introduction to get you started writing your first proxy on Phantom.

Motivation for creating Phantom and design overview is described in this Proxies for resilience and fault tolerance in SOA blog post.

Phantom Examples

The examples that follow explore the various uses of Phantom.

Example: Http proxy in 5 minutes

This is the simplest example and requires no coding effort. Please refer to Maven Dependencies to setup the requisite repository location in your Maven build settings. The sample Http proxy simply forwards Http requests from clients (say browser) to a target server and relays the response received. You may configure different timeout parameters (more on this in later examples).

Follow these steps to setup and run your first Phantom Http proxy:

  • git clone Phantom
  • Run mvn clean install in sample-http-proxy directory. This might take a while to download Phantom, Trooper and their dependencies from the various Maven repositories.
  • Start the proxy in sample-http-proxy directory:
java -Dlog4j.configurationFile=file:<absolute path to log4j2.xml> -cp "./target/lib/*" \
org.trpr.platform.runtime.impl.bootstrap.BootstrapLauncher \
./src/main/resources/external/bootstrap.xml
  • A successful start will display a message like below on the console:
*************************************************************************
 Trooper __
      __/  \         Runtime Nature : SERVER
   __/  \__/         Component Container : com.flipkart.phantom.runtime.impl.spring.ServiceProxyComponentContainer
  /  \__/  \         Startup Time : 2,320 ms
  \__/  \__/         Host Name:ReguMac.local
     \__/
*************************************************************************
19:17:34.367 [main] INFO  o.t.p.r.i.bootstrap.spring.Bootstrap - ** Trooper Bootstrap complete **
  • Point your browser to http://localhost:8080. You should see the web-site configured in Http proxy config file located at: sample-http-proxy/src/main/resources/external/spring-proxy-handler-config.xml
  • The monitoring console is available at : http://localhost:8081/admin/dashboard. This is the Hystrix console for deployed handlers and commands - in case of this sample, it is just one.
  • The configuration console is available at : http://localhost:8081/admin/configuration with details like Proxy port, target host, port and the Http connection & operation timeout properties. You may change the host to another website, save changes and refresh http://localhost:8080 - you should see contents of the website that the config is now pointing to. Further options exist to control threadpools and proxy level timeouts (Hystrix properties). These are covered in more detailed examples.

Example: Thrift proxy in 5 minutes

This is a simple Thrift proxy and requires no coding effort. Please refer to Maven Dependencies to setup the requisite repository location in your Maven build settings. The sample Thrift proxy simply forwards Thrift requests from clients to a target server where an "Arithmetic" service is deployed and relays the response received. The Arithmetic service's Thrift IDL is as follows:

namespace java thrift  
typedef i64 long
typedef i32 int
service ArithmeticService {  
    long add(1:int num1, 2:int num2),
    long multiply(1:int num1, 2:int num2),
}

The sample-thrift-proxy module has the following:

  • An implementation of the Arithmetic service as described by the IDL above : Service Interface Implementation , Sample Server. Note - the Server is set to run on port: 8082
  • An implementation of an Arithmetic client that invokes methods on the service a number of times : Arithmetric client. Note - the client is pointing to proxy port: 8080
  • Phantom configuration to deploy a proxy for the Arithmetic service : sample-thrift-proxy/src/main/resources/external/spring-proxy-handler-config.xml. Note: the proxy is set to run on port: 8080 and its configuration points to the actual target Thrift service set to run on port: 8082

Follow these steps to setup and run your first Phantom Thrift proxy:

  • git clone Phantom
  • Run mvn clean install in sample-thrift-proxy directory. This might take a while to download Phantom, Trooper and their dependencies from the various Maven repositories.
  • Next start up the target Thrift server in sample-thrift-proxy directory on localhost by running:
java -cp "./target/*:./target/lib/*" thrift.Server
  • Start the proxy in sample-thrift-proxy directory:
java -Dlog4j.configurationFile=file:<absolute path to log4j2.xml> -cp "./target/*:./target/lib/*" \
org.trpr.platform.runtime.impl.bootstrap.BootstrapLauncher \
./src/main/resources/external/bootstrap.xml
  • A successful start will display a message like below on the console:
*************************************************************************
 Trooper __
      __/  \         Runtime Nature : SERVER
   __/  \__/         Component Container : com.flipkart.phantom.runtime.impl.spring.ServiceProxyComponentContainer
  /  \__/  \         Startup Time : 2,320 ms
  \__/  \__/         Host Name:ReguMac.local
     \__/
*************************************************************************
19:17:34.367 [main] INFO  o.t.p.r.i.bootstrap.spring.Bootstrap - ** Trooper Bootstrap complete **
  • The configuration console is available at : http://localhost:8081/admin/configuration with details like Proxy port, Thrift service, target host, port and the Thrift timeout properties.
  • Now run the Arithmetic client in sample-thrift-proxy directory to generate a number of requests to the proxy:
java -cp "./target/*:./target/lib/*" thrift.ArithmeticClient
  • You should be able to see Thrift method calls invoked by the client showing up on the monitoring console available at : http://localhost:8081/admin/dashboard. This is the Hystrix console for deployed Thrift services - in case of this sample, it is just one.

More Phantom Examples

Refer to Writing your own proxy using Phantom for customizing protocol handlers supported by Phantom - such as Http and Thrift. Also has guidelines for writing your own proxy and handlers for other standard (or) custom protocols.