Sunday, February 9, 2014

Apache Thrift: Getting started

Apache Thrift [1] is one of the most popular and innovative software framework I've used.

Thrift file ( IDL) defines the server interface and allows the server and the client to talk through RPC. That same thrift file can generate various server and/or client code enabling scalable cross-language development.

I've hosted the sample code on Github (Uses Maven, and surefire tests)   [2]

To get started, install thrift in your computer [3].

Then you need to create a "Thrift file"( it basically defines the interface, i:e Interface Definition).
Here's my simple Calculator.thrift file.

namespace java lk.swithana.calculator
 
service lk.swithana.calculator.server.CalculatorService
{
    i32 add  (1:i32 a, 2:i32 b),
    i32 sub  (1:i32 a, 2:i32 b),
    i32 div  (1:i32 a, 2:i32 b),
    i32 mult (1:i32 a, 2:i32 b)
}

This defines the calculator interface.
If you've installed Thrift correctly, you should run:

thrift --gen java Calculator.thrift 

This would generate the necessary Java Beans for the server and the client.

NOTE that it only creates the beans, now you have to create the server and the client using those beans.
I've already completed the server and the client in the code in github.

Refer to the CalculatorBasicTestCase java test class to see how it works.

Note: This is a VERY BASIC thrift file. You can/should add type definitions and other Thrift features to make it better. Here's a good guide on writing simple to complex thrift files.[4]

[1] http://thrift.apache.org/
[2] https://github.com/swsachith/SimpleThriftCalc
[3] http://thrift.apache.org/docs/install/
[4] http://diwakergupta.github.io/thrift-missing-guide/


No comments:

Post a Comment