This post provides a step by step guide to install thrift and write a server (in java) and a client (in php) using it.
1. Download Thrift
Basic requirements
Please go through this link for a list of prerequisite or basic requirements for thrift compiler.
Download the latest stable release of from here and extract it. OR do a svn checkout
$ svn co http://svn.apache.org/repos/asf/thrift/trunk thrift
2. Build and Install
Now go to the thrift directory and run
$ ./bootstrap.sh
$ ./configure
$ make
$ make install
this will install thrift on your system.
3. Writing a Thrift file
profileservice.thrift
namespace php ProfileService #client
namespace java test.services.profile.thrift #server
enum JobType {
P, //Permanent
T //Temporary
}
enum EmploymentStatus {
F, //Full Time
P, //Part Time
}
exception ProfileServiceException {
1: i32 code,
2: string message
}
struct Profile {
1: i32 profileId,
2: string name,
3: string birthDate,
4: string contactAddress,
5: i32 cityId,
6: double totalExperience,
7: JobType jobType,
8: EmploymentStatus employmentStatus,
9: string summary,
}
service ProfileService {
Profile getProfileById(1:i32 profileId) throws (1: ProfileServiceException e),
Profile getProfileByName(1:string name) throws (1: ProfileServiceException e),
}
4. Using the Thrift Compiler
Now its time to generate the thrift code for server and client. For java server run the command
thrift --gen java profileservice.thrift
$ ls gen-java/test/services/profile/thrift/
EmploymentStatus.java
JobType.java
Profile.java
ProfileServiceException.java
ProfileService.java
For php client run
thrift --gen php profileservice.thrift
for php, it’ll make a directory called gen-php/. For my thrift its generated the following files under the directory gen-php/profileservice/ (its based on package name or namespace provided in the .thrift file)
$ ls gen-php/profileservice/
ProfileService.php
profileservice_types.php
5. Creating a Thrift Server using Java
ProfileServiceImpl.java
package server;
import java.util.*;
import org.apache.thrift.*;
import test.services.profile.thrift.*;
class ProfileServiceImpl implements ProfileService.Iface
{
public Profile getProfileById(int profileId) throws ProfileServiceException, TException {
// your code goes here
return profile;
}
public Profile getProfileByName(String name) throws ProfileServiceException, TException {
// your code goes here
return profile;
}
}
Now write a java server for this service.
Server.java
package server;
import java.io.*;
import org.apache.thrift.protocol.*;
import org.apache.thrift.protocol.TBinaryProtocol.*;
import org.apache.thrift.server.*;
import org.apache.thrift.transport.*;
import test.services.profile.thrift.*;
public class Server
{
private void start()
{
try
{
TServerSocket serverTransport = new TServerSocket(7911);
ProfileService.Processor processor = new ProfileService.Processor(new ProfileServiceImpl());
Factory protFactory = new TBinaryProtocol.Factory(true, true);
TServer server = new TThreadPoolServer(processor, serverTransport, protFactory);
System.out.println("Starting server on port 7911 ...");
server.serve();
}catch(TTransportException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Server srv = new Server();
srv.start();
}
}
Compile all the files and start the server.
6. Creating a Thrift Client using PHP
For this tutorial I have created a folder testclient in my home directory. Now create a subfoder named src-php, and copy all the library files in this folder. You will also need to mv or cp the autogenerated thrift files (from gen-php folder) for this project into the packages folder of these library files. Here’s a screenshot of my directorys structure for this project.
testclient
..src-php
....autoload.php
....ext
....packages
......profileservice
........ProfileService.php
........profileservice_types.php
....protocol
....server
....Thrift.php
....transport
Write a php client script to connect to the thrfit ProfileService server
ProfileServiceClient.php
// Setup the path to the thrift library folder
$GLOBALS['THRIFT_ROOT'] = 'thrift';
// Load up all the thrift stuff
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
// Load the package that we autogenerated for this tutorial
require_once $GLOBALS['THRIFT_ROOT'].'/packages/profileservice/ProfileService.php';
try {
// Create a thrift connection
$socket = new TSocket('localhost', '9090');
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocol($transport);
// Create a profile service client
$client = new ProfileServiceClient($protocol);
// Open up the connection
$transport->open();
$data = $this->client->getProfileById(123);
$this->transport->close();
$this->socket->close();
print_r($data);
}
catch (TException $tx) {
// a general thrift exception
echo "ThriftException: ".$tx->getMessage()."\r\n";
}
?>
to run the client execute
php ProfileServiceClient.php