Apache Thrift and Spring Boot

Apache Thrift

  • Thrift is now an open-source tool from the Apache Software Foundation.
  • Data types and service interfaces for the complex services are defined in a simple definition file. Thrift compiler uses this file to generate RPC servers and clients.
  • Thrift supports a wide range of language support.
  • Thrift is lightweight and language-independent.
  • Thrift was created on Facebook and according to the technical requirements.

Thrift is simple and as a developer, we don’t need to write any code for serialization or data transport. To keep things simple thrift data types don’t introduce any special dynamic types or wrapper objects. Base types for Thrift are :

  • bool value boolean value, true or false
  • byte value signed byte
  • i16 value 16-bit signed integer
  • i32 value 32-bit signed integer
  • i64 value 64-bit signed integer
  • double value 64-bit floating-point number
  • string text/binary string

The other types are Structs, Containers, Exceptions, Services

For our tutorial, we will create two applications Client and Server. But before we start, we have to download the Apache Thrift compiler. If you are working on windows, please download the link for windows.

Apache Thrift download link and Maven dependency

After the download, we are ready to create our studentservice. thrift file. The namespace here is important here as we will use the generated classes within our applications.

studentservice.thrift file

We put some basic data types and our student data consists of id, studentName, and studentid fields. Our service has 4 methods that save and get our student data and also we have the ping method.

We will also place a thrift compiler inside our project with a studentservice.thrift file. We will open the command prompt and run the code to generate the classes “thrift-0.15.0.exe -r — gen java studentservice.thrift”. In this way, we are going to generate the classes needed for our server application.

Thrift compiler and our thrift file

After we add the dependencies to our pom.xml file, we are going to run maven-clean and maven compile commands. We will create a StudentServiceImpl class that implements the StudentService.Iface. This interface is autogenerated by Thrift Compiler. You can go and check for it, from the mythrift package.

StudentServiceImpl class

We implement the methods according to our needs. To demonstrate the data we have only defined the getList() method. We defined a student with the name Lucy. Now we are ready to code our ServerApplication. We will be using port 9479 for incoming data.

ServerApplication class

We have completed the server application. and now we are ready to go with the client application. We will again use our thrift compiler to generate our resource codes. Just repeat the same steps as we did in our server application.

Thrift compiler client side

Now we are ready to create the ClientApplication.

ClientApplication

The TTransport provides a simple abstraction for reading and writing operations. If you are familiar with java networking the methods are similar.

  • open
  • close
  • read
  • write
  • flush

We are going to use port 9479 to connect the server and we will call our student methods from the server. Remember we created a student with the name Lucy and her studentid was 157112. After we run our client application you should see the results as shown.

Client output

Thanks for reading. The source file for our tutorial can be found on GitHub

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store