成功的秘訣乃正,在永不改變既定的目的票堵。 —— 盧梭
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.
通過(guò)官方介紹攘宙,我們可以了解到Thrift是一個(gè)軟件框架坦弟,可以提供跨語(yǔ)言的服務(wù)開(kāi)發(fā)蜗细。Thrift框架包含一個(gè)軟件棧裆操,包含生成各種語(yǔ)言的引擎,我們通過(guò)Thrift提供的接口定義語(yǔ)言(IDL)來(lái)定義接口鳄乏,然后引擎會(huì)自動(dòng)生成各種語(yǔ)言的代碼跷车。
Windows平臺(tái)下Thrift安裝與使用
下載與安裝
下載地址:點(diǎn)我下載Thrift
建議:修改名稱(chēng)為T(mén)hrift.exe,方便使用
建議:配置環(huán)境變量
在系統(tǒng)變量Path中添加Thrift路徑到環(huán)境變量中
使用
定義helloServer.thrift文件
在本例中橱野,定義了一個(gè)用戶(hù)對(duì)象User朽缴,包含兩個(gè)字段,用戶(hù)姓名username水援,用戶(hù)年齡age密强,定義了一個(gè)接口sayHello(User user),返回用戶(hù)姓名和年齡蜗元。
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è)類(lèi)奕扣,HelloWorldService.java和User.java類(lèi)薪鹦,如下圖所示。
隨后惯豆,把自動(dòng)生成的代碼添加到項(xiàng)目中去池磁,如下圖所示。
添加完代碼發(fā)現(xiàn)build會(huì)報(bào)錯(cuò)楷兽,查看日志發(fā)現(xiàn)是因?yàn)闆](méi)有添加Thrift依賴(lài)的庫(kù)文件地熄,我使用的是gradle來(lái)開(kāi)發(fā),添加Thrift的依賴(lài)就可以了芯杀,在build.gradlezhong 添加如下代碼
// https://mvnrepository.com/artifact/org.apache.thrift/libthrift
compile group: 'org.apache.thrift', name: 'libthrift', version: '0.10.0'
如果你使用的是mawen來(lái)添加依賴(lài)的話(huà)端考,可以添加如下代碼
<!-- 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
客戶(hù)端代碼
客戶(hù)端主要是將User對(duì)象發(fā)送給服務(wù)端,首先却特,實(shí)例化TTransport 扶供,設(shè)置IP地址、服務(wù)端口port核偿、超時(shí)時(shí)間timeout等诚欠。然后設(shè)置協(xié)議TProtocol ,注意要和服務(wù)端保持一致漾岳。接著調(diào)用接口sayHello把User對(duì)象傳遞過(guò)去。
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類(lèi),本例中我們使用簡(jiǎn)單的單線程服務(wù)模型TSimpleServer來(lái)開(kāi)啟一個(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的原理等巧还。