How to create an Apache Thrift Service – Tutorial


Say you come up with a wonderful idea such as a really great phone service. You would want this phone service to be available to the APIs in various languages. Whether people are using Python, C++, Java or any other programming language, the users should be able to use your service. Also, you would want the users to be able to access globally. In such scenarios, you should create the Thrift Service. Thrift lets you create a generic interface which can be implemented on the server. The clients of this generic interface can be automatically generated in all kinds of languages.

Let us get started! Here we are going to create a very simple service that just prints the server time.

Step 0: Install Thrift

This step is not required if you are using CloudxLab. You can just log in to the web console. In case you want to set up Apache Thrift on your own machine please follow these instructions:

Step 1: Create the interface definition

Let us create a file with name Example.thrift with the following code in it:

Step 2: Generate the server and client side code in python

At this point, you will observe a folder with the name “gen-py” created in your current directory. Inside gen-py, you would notice that a folder with name “Example” has been created with all sorts of code.

Step3: Create Server

First, create a directory with the name “server” and go into that directory:

Inside this folder create a file with the name and the following contents:

Notice that we are implementing service by the way of the class ExampleHandler.

Step 4: Now start the server:

Step 5: Create a client

Let the server run and open a new terminal. In the new terminal follow the instructions from here onwards. Create a folder with the name “client”. Inside that folder create a file with name and the following code:

Step 6: Run the client

It should print the current time such as:


It is an extremely simple example. You can extend it to add more functions and objects.

The code for the whole project is available here: