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
Services generated based on the Apache Thrift framework include client and server. The specific deployment mode is as follows:
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
- 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.
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.