<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rahul Goyal on Medium]]></title>
        <description><![CDATA[Stories by Rahul Goyal on Medium]]></description>
        <link>https://medium.com/@goyal.1234rahul?source=rss-18bc018e7597------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*TMMVHvUS1T98CD4y15owWA.jpeg</url>
            <title>Stories by Rahul Goyal on Medium</title>
            <link>https://medium.com/@goyal.1234rahul?source=rss-18bc018e7597------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 07:55:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@goyal.1234rahul/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My first Apache Thrift Service]]></title>
            <link>https://medium.com/@goyal.1234rahul/my-first-apache-thrift-service-6be3e8bf650f?source=rss-18bc018e7597------2</link>
            <guid isPermaLink="false">https://medium.com/p/6be3e8bf650f</guid>
            <category><![CDATA[thrift]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[apache]]></category>
            <category><![CDATA[apache-thrift]]></category>
            <dc:creator><![CDATA[Rahul Goyal]]></dc:creator>
            <pubDate>Tue, 23 Apr 2019 02:35:34 GMT</pubDate>
            <atom:updated>2019-04-23T02:35:34.534Z</atom:updated>
            <cc:license>https://creativecommons.org/publicdomain/mark/1.0/</cc:license>
            <content:encoded><![CDATA[<p>Recently I came across a Micro Service which made use of <a href="https://www.geeksforgeeks.org/operating-system-remote-procedure-call-rpc/">Remote Procedure Call (RPC)</a> for Client-Server Communication. I had only read about it during my College course but never had any hands-on Experience working on a Service. This increased my curiosity and I started to look further. What I found was a Multithreaded <strong>“</strong><a href="https://thrift.apache.org/"><strong>Apache Thrift Service</strong></a><strong>”</strong> running on port 20000!</p><p>Thrift is a brainchild of Facebook conceived in 2007. This was the result of the company culture which gave the developers the flexibility to chose any programming language to develop services while Thrift provided the inter-application communication channel. You can read more about <a href="https://medium.com/techlabs-emag/introduction-to-apache-thrift-32bf78722b34">Apache Thrift </a>and <a href="https://thrift-tutorial.readthedocs.io/en/latest/intro.html">its Architecture here.</a> Even if you do not understand the architecture completely, towards the end of this article things will be more clear.</p><p>In this article, I am going to describe step by step process to Create simple Thrift Service which implements various functions of a <strong>Calculator</strong>. A lot of websites has the same example, but something or the other was missing in each of them.</p><p><strong>Step 1: Install Thrift on your system.</strong></p><p>If you have a Mac, then use homebrew to install which takes care of all the dependencies! #MakesLifeEasy</p><pre>$brew install thrift<br></pre><p>others, please follow the <a href="https://thrift-tutorial.readthedocs.io/en/latest/installation.html">installation here</a>.</p><p><strong>Step 2: Verify the installation.</strong></p><pre>$thrift -version<br>Thrift version 0.12.0</pre><p><strong>Step 3: Create a Project Directory.</strong></p><pre>$mkdir <strong>calculator-thrift-service<br></strong>$cd <strong>calculator-thrift-service</strong></pre><p><strong>Step 4: Define the Prototype of the Calculator Service: <em>*.thrift</em> File</strong></p><p>Similar to how we define the API Routes for a service, this file contains the list of methods with their arguments which can be called by a Client.</p><p>Let&#39;s look at the service definition of our Calculator Service.</p><ol><li>Create a file</li></ol><pre><strong>calculator-thrift-service$ </strong>touch <strong>calculator_service.thrif</strong></pre><p>2. Define the functions that we want to expose as APIs</p><pre>typedef i32 int</pre><pre>service CalculatorService<br>{</pre><pre>  int add(1:int a, 2:int b),<br>  int subtract(1:int a, 2:int b)<br>}</pre><p><strong>Step 4: Code Generation</strong></p><p>Time to do <strong><em>Thrift Magic</em></strong> to generate the Service Code in the language of our choice. I am going to choose <strong>Ruby</strong> <strong>for both Server and Client implementation.</strong></p><pre><strong>$thrift --gen rb calculator_service.thrift<br></strong>$<strong>ls<br></strong>calculator_service.thrift    <strong>gen-rb</strong></pre><blockquote>execute `<strong>gem install thrift</strong>` if the above step throws gem missing error.</blockquote><p>But you are free to choose any pair of language from the list of supported languages. That’s why Thrift was born in the first place right!</p><blockquote>Thrift supports many language : C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml.</blockquote><p><strong>Step 5: Understanding the Generated Code</strong></p><p><em>gen-rb directory </em>has the generated code. You will find three files of which calculattor_service.rb is what should interest us.</p><p>There will be multiple classes inside `<strong>CalculatorService` </strong>module but let’s look at the classes that are important.</p><ol><li><strong>Processor Class: </strong>This acts as a <strong>Controller Class for Service</strong> which has the skeleton already implemented.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zmCaeearMGO5U9RizQy2fw.png" /></figure><p>But what this does not have, is the<strong> <em>Logic to add/subtract two number</em></strong>. So, we need to plug a `<strong>ServiceHandler</strong>` module which Line Number: 52 and 59 can call to get the result. We will be defining these in the next section.</p><p>2. <strong>Client Class: </strong>This has the Interfaces defined to communicate with the Thrift Server over the Transport Protocol, Server IP, and Port.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZOtgm84XNLd324JZJQZ71g.png" /></figure><p>All that we need to do, is define another module which will call these interfaces `(Line:14, 29)` with the actual parameters. We will define the <strong>Client Module</strong> in the next sections.</p><p>This might be little confusion, but keep calm and move to the next step. You can come back to this module again to understand better :)</p><p><strong>Step 6: Writing the ServerHandler file.</strong></p><ol><li>Create the Handler File.</li></ol><pre>calculator-thrift-service$touch <strong>calculator-server.rb</strong></pre><p>2. Define the handlers with the login for each of the function / API that we had earlier defined in the calculator_service.thrift file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-9kgxft-4ZeOWSTIsQjadw.png" /></figure><p>The above code can be accessed <a href="https://github.com/rahul26goyal/calculator-thrift-service">here on GitHub.</a></p><p><strong>Step 7: Write the script to start the actual Thrift Server.</strong></p><ol><li>Create the server file.</li></ol><pre>calculator-thrift-service$touch calculator-server.rb</pre><p>2. Import the required files and define the server process.</p><p>I have added comments wherever possible so that the code is easier to understand even for a non-ruby programmer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*spcU0CrC3tHCMAoHauYCMg.png" /></figure><p>The above code can be accessed <a href="https://github.com/rahul26goyal/calculator-thrift-service">here on GitHub.</a></p><p><strong>Step 8: Start the server !! Action…</strong></p><pre>calculator-thrift-service$ <strong>ruby calculator_server.rb</strong></pre><pre>CalculatorServiceHandler initialize</pre><pre>Starting the CalculatorService server on port 9090...</pre><p>We are done setting up the Server which is running <strong>locally</strong> on the machine listening to port <strong>9090</strong>..let’s move to Client implementation now.</p><p><strong>Step 9: Define the Client App</strong></p><p>As said earlier, I will be writing the Client Code in Ruby. So, I will be using the same code that was generated in Step 4.</p><ol><li>Create the client app file.</li></ol><pre>$touch calculator_client_app.rb</pre><p>2. Write a simple client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HuuSkCwvRxL3cshNKpyMaA.png" /></figure><p>The above code can be accessed <a href="https://github.com/rahul26goyal/calculator-thrift-service">here on GitHub.</a></p><blockquote>If you want to define the client in a different language, then you need to repeat the Step 4 again.</blockquote><pre>thrift --gen java -o java-client calculator_service.thrift</pre><p><strong>Step 10: Lets test end-to-end…</strong></p><ol><li>The service should be running. Step 8</li><li>Run the client:<em> </em><strong><em>calculator_client_app.rb</em></strong></li></ol><pre><strong>calculator-thrift-service$ ruby calculator_client_app.rb</strong></pre><pre>start testing...</pre><pre>add result:::50</pre><pre>subtract result:::30</pre><pre><strong>calculator-thrift-service$</strong></pre><p><strong>Server log:</strong></p><pre><strong>calculator-thrift-service$</strong> <strong>ruby calculator_server.rb</strong></pre><pre>CalculatorServiceHandler initialize</pre><pre>Starting the CalculatorService server on port 9090...</pre><pre>request received to add two numbers..</pre><pre>request received to subtract two numbers..</pre><p>And I am done with my part, now it is your turn to extend this application with multiplication and division functionality :) #GoBackToStep2</p><p>Now, let a take a look at the Architecture Diagram of Apache Thrift and try to understand each component that we wrote in this article.</p><p>The diagram is taken from <a href="https://www.manning.com/books/programmers-guide-to-apache-thrift">Programmer’s Guide to Apache Thrift</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*6PfOfkWSDRoXKJwwEEaSTg.png" /></figure><p>This is <strong>My First Article</strong> on Medium, hope you guys like it.</p><p>Do leave your<strong> comments and feedback</strong>!</p><p>#KeepLearningKeepCoding</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6be3e8bf650f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>