成功的秘訣,在永不改變既定的目的。 —— 盧梭
Thrift簡(jiǎn)介
The Apache Thrift software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.
通過官方介紹,我們可以了解到Thrift是一個(gè)軟件框架,可以提供跨語言的服務(wù)開發(fā)。Thrift框架包含一個(gè)軟件棧,包含生成各種語言的引擎,我們通過Thrift提供的接口定義語言(IDL)來定義接口,然后引擎會(huì)自動(dòng)生成各種語言的代碼。
Windows平臺(tái)下Thrift安裝與使用
下載與安裝
下載地址:點(diǎn)我下載Thrift
建議:修改名稱為Thrift.exe,方便使用
建議:配置環(huán)境變量
在系統(tǒng)變量Path中添加Thrift路徑到環(huán)境變量中
使用
定義helloServer.thrift文件
在本例中,定義了一個(gè)用戶對(duì)象User,包含兩個(gè)字段,用戶姓名username,用戶年齡age,定義了一個(gè)接口sayHello(User user),返回用戶姓名和年齡。
namespace java cn.ac.ict.software.thrift
struct User {
1: string username,
2: i32 age,
}
service HelloWorldService {
string sayHello(1:User user)
}
定義完成helloServer.thrift文件之后我們使用命令thrift -r --gen java helloServer.thrift生成代碼,如下圖所示。
可以看到生成的代碼包含兩個(gè)類,HelloWorldService.java和User.java類,如下圖所示。
隨后,把自動(dòng)生成的代碼添加到項(xiàng)目中去,如下圖所示。
添加完代碼發(fā)現(xiàn)build會(huì)報(bào)錯(cuò),查看日志發(fā)現(xiàn)是因?yàn)闆]有添加Thrift依賴的庫文件,我使用的是gradle來開發(fā),添加Thrift的依賴就可以了,在build.gradlezhong 添加如下代碼
// https://mvnrepository.com/artifact/org.apache.thrift/libthrift
compile group: 'org.apache.thrift', name: 'libthrift', version: '0.10.0'
如果你使用的是mawen來添加依賴的話,可以添加如下代碼
<!-- https://mvnrepository.com/artifact/org.apache.thrift/libthrift -->
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
更多的添加方式,大家可以參考http://mvnrepository.com/artifact/org.apache.thrift/libthrift
客戶端代碼
客戶端主要是將User對(duì)象發(fā)送給服務(wù)端,首先,實(shí)例化TTransport ,設(shè)置IP地址、服務(wù)端口port、超時(shí)時(shí)間timeout等。然后設(shè)置協(xié)議TProtocol ,注意要和服務(wù)端保持一致。接著調(diào)用接口sayHello把User對(duì)象傳遞過去。
public class HelloClient {
private static final String SERVER_IP = "localhost";
private static final int SERVER_PORT = 8090;
private static final int TIMEOUT = 30000;
public void startClient() {
TTransport transport = null;
try {
transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
// 協(xié)議要和服務(wù)端一致
TProtocol protocol = new TBinaryProtocol(transport);
// TProtocol protocol = new TCompactProtocol(transport);
// TProtocol protocol = new TJSONProtocol(transport);
HelloWorldService.Client client = new HelloWorldService.Client(protocol);
transport.open();
User user = new User();
user.username = "haiker";
user.age = 26;
String result = client.sayHello(user);
System.out.println("Thrify client result =: " + result);
} catch (TException e) {
e.printStackTrace();
} finally {
if (null != transport) {
transport.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
HelloClient client = new HelloClient();
client.startClient();
}
}
HelloWorldImpl代碼
HelloWorldImpl代碼implents定義的接口sayHello,本例中只是簡(jiǎn)單的把姓名和年齡返回。
public class HelloWorldImpl implements HelloWorldService.Iface{
@Override
public String sayHello(User user) throws TException {
return "Hi,My name is " + user.username + " and My age is " + user.age;
}
}
服務(wù)端代碼
服務(wù)端代碼首先要實(shí)例化TProcessor,傳入我們具體的HelloWorldImpl類,本例中我們使用簡(jiǎn)單的單線程服務(wù)模型TSimpleServer來開啟一個(gè)服務(wù)。
public class HelloServer {
private static final int SERVER_PORT = 8090;
public void startServer() {
try {
System.out.println("HelloWorld TSimpleServer start ....");
TProcessor tprocessor = new HelloWorldService.Processor<>(new HelloWorldImpl());
// 簡(jiǎn)單的單線程服務(wù)模型,一般用于測(cè)試
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(tprocessor);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
TServer server = new TSimpleServer(tArgs);
server.serve();
} catch (Exception e) {
System.out.println("Server start error!!!");
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
HelloServer server = new HelloServer();
server.startServer();
}
}
總結(jié)
本文主要探討了Thrift在Windows中的安裝和簡(jiǎn)單使用,以后會(huì)繼續(xù)深入探討Thrift在Linux中的安裝與使用、Thrift的原理等。