1024programmer Blog Thrift_Hacker angel Ma Xiaopei’s blog

Thrift_Hacker angel Ma Xiaopei’s blog

Reference


Apache Thrift – scalable span Language Services Development Framework

Apache Thrift is Facebook An efficient framework for remote service calls that supports multiple programming languages. This article will introduce the architecture, development and deployment of Apache Thrift in detail from the perspective of Java developers, and give corresponding Java examples for different transport protocols and service types. At the same time, it will introduce the implementation of Thrift asynchronous client in detail, and finally put forward the points that need to be paid attention to when using Thrift matter.

Preface:

There are many popular service invocation methods, such as Web Service based on SOAP message format, RESTful service based on JSON message format, etc. . The data transmission methods used include XML, JSON, etc. However, XML is relatively large in size and low in transmission efficiency, and JSON is small in size and novel, but it is not perfect enough. This article will introduce Apache Thrift, a remote service invocation framework developed by Facebook, which uses interface description language to define and create services, supports scalable cross-language service development, and includes code generation engines that can be used in multiple languages, such as C++, Java , Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, etc. to create efficient and seamless services. The data transmission is in binary format, which is smaller than XML and JSON. For high concurrency and large data volume And a multilingual environment is more advantageous. This article will introduce the use of Thrift in detail, and provide rich example codes to explain and explain, helping users to quickly build services.

A simple Thrift instance

This article first introduces a simple Thrift implementation example, so that readers can quickly and intuitively understand what Thrift is and how to use Thrift to build services.

Create a simple service Hello. First write the script file Hello.thrift according to Thrift’s grammar specification, the code is as follows:

Listing 1. Hello.thrift
namespace java service.demo service Hello{ string helloString(1:string para) i32 helloInt(1:i32 para) bool helloBoolean(1:bool para) void helloVoid() string helloNull() }<  /t.jar
  • Add libthrift-0.6.1-snapshot.jar, slf4j-api-1.5 .8.jar, slf4j-log4j12-1.5.8.jar and log4j-1.2.14.jar are imported into the eclipse development environment
  • Download the thrift compilation tool, which can compile thrift script files into java files , download address: http://apache.etoak.com//thrift/0.6. 0/thrift-0.6.1.exe
  • Create a Hello.thrift script file, the specific code is as described in the previous chapter, Enter the directory where thrift-0.6.1.exe is located, execute the command "thrift-0.6.1.exe -gen java x:\Hello.thrift", you can see the gen-java directory under the current running drive letter, enter the directory to see to the generated Java code. For more commands of thrift, please refer to the help command that comes with thrift
  • Write server and client code to complete thrift installation and deployment
  • Services generated based on the Apache Thrift framework include client and server. The specific deployment mode is as follows:

    Figure 5. Deployment Diagram

    Figure 5. Deployment diagram

    From the figure, we can see that when the client and server are deployed, they need to use common jar packages and java files, as shown in the figure "Common file" area, where Hello.java is compiled from Hello.thrift. On the server side, the service must implement the Hello.Iface interface and include the server startup code HelloServiceServer.java. On the client side, include the code HelloServiceClient.java for the client to invoke the service. The client and server implement remote service calls through the API provided by Hello.java.

    Summary

    This article introduces the installation, deployment and architecture of Apache Thrift, and introduces how to use Apache Thrift to build in different situations through a large number of examples At the same time, it focuses on the construction of Thrift asynchronous client, hoping to bring some help to readers.

    References

    Learning

    n:0px; padding:5px 0px 5px 11px; border:0px; outline:0px; font-size:undefined; vertical-align:baseline; height:22px; clear:left"> Apache Thrift official website: You can download Thrift tools and source code.

  • Thrift Features and Non-features: Thrift's features and deficiencies.
  • Introduction to Apache Thrift: introduces Thrift architecture, protocol, transport layer and server types, and other methods of building services (eg: REST) ​​for comparative analysis.
  • Thrift installation and deployment: Thrift installation and deployment instructions
  • Thrift: Scalable Cross-Language Services Implementation: Thrift official document, detailing the design of Thrift
  • Thrift API: About Apache Thrift 0.6.1 build server and client API manual
  • Thrift example: Simple application example of Thrift
  • Fully async Thrift client in Java: About Thrift Introduction to asynchronous clients
  • developerWorks Java technology zone: There are hundreds of articles on all aspects of Java programming.
  • This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/thrift_hacker-angel-ma-xiaopeis-blog/

    author: admin

    Previous article
    Next article

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Contact Us

    Contact us

    181-3619-1160

    Online consultation: QQ交谈

    E-mail: [email protected]

    Working hours: Monday to Friday, 9:00-17:30, holidays off

    Follow wechat
    Scan wechat and follow us

    Scan wechat and follow us

    Follow Weibo
    Back to top
    首页
    微信
    电话
    搜索