您好,登錄后才能下訂單哦!
本文借鑒自
http://www.jianshu.com/p/0f4113d6ec4b
(下面稱簡書教程)
首先上官網下載代碼
https://thrift.apache.org/download
下載源碼thrift-0.9.3.tar.gz
解壓之后放在路徑C:\thrift-0.9.3\thrift-0.9.3
并下載windows執行版thrift-0.9.3.exe
放在路徑C:\thrift-0.9.3下
下載apache ant項目,用于打jar包
下載路徑
http://ant.apache.org/bindownload.cgi
解壓之后放在路徑C:\apache-ant-1.9.7-bin\apache-ant-1.9.7
配置環境變量
ANT_HOME : C:\apache-ant-1.9.7-bin\apache-ant-1.9.7
把C:\apache-ant-1.9.7-bin\apache-ant-1.9.7\bin\ant.bat復制到路徑C:\thrift-0.9.3\thrift-0.9.3\lib\java下
在cmd中運行ant.bat,會生成jar包libthrift-0.9.3 在路徑C:\thrift-0.9.3\thrift-0.9.3\lib\java\build
在路徑C:\thrift-0.9.3中創建一個文本文件
復制代碼
namespace java com.winwill.thrift
enum RequestType {
SAY_HELLO, //問好
QUERY_TIME, //詢問時間}struct Request {
1: required RequestType type; // 請求的類型,必選
2: required string name; // 發起請求的人的名字,必選
3: optional i32 age; // 發起請求的人的年齡,可選
}
exception RequestException {
1: required i32 code;
2: optional string reason;
}
// 服務名
service HelloWordService {
string doAction(1: Request request) throws (1:RequestException qe);
// 可能拋出異常。
}
保存為Test.thrift
在cmd中進入路徑C:\thrift-0.9.3
執行命令thrift-0.9.3 -gen java Test.thrift
會在路徑C:\thrift-0.9.3下生成一個文件夾gen-java
在Eclipse中創建工程TestThrift
按照簡書教程生成package
com.winwill.thrift
把上面生成的gen-java中的代碼復制到package中
并在package中創建代碼,由于可能跟簡書教程使用的版本不同,簡書教程中的某些寫法無法編譯,
經過修改后使用如下代碼
1.服務端
package com.winwill.thrift;
import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.TException;
import java.util.Date;
public class HelloWordServiceImpl implements com.winwill.thrift.HelloWordService.Iface {
// 實現這個方法完成具體的邏輯。
public String doAction(com.winwill.thrift.Request request) throws com.winwill.thrift.RequestException, TException {
System.out.println("Get request: " + request);
if (StringUtils.isBlank(request.getName()) || request.getType() == null) {
throw new com.winwill.thrift.RequestException();
}
String result = "Hello, " + request.getName();
if (request.getType() == com.winwill.thrift.RequestType.SAY_HELLO) {
result += ", Welcome!";
} else {
result += ", Now is " + new Date().toLocaleString();
}
return result;
}
}
2.啟動服務
package com.winwill.thrift;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TJSONProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TFastFramedTransport;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportFactory;
import org.slf4j.*;
import java.net.ServerSocket;
public class HelloWordServer {
public static void main(String[] args) throws Exception {
int port = 7912;
String transport_type = "buffered";
String protocol_type = "binary";
String server_type = "thread-pool";
String domain_socket = "";
// ServerSocket socket = new ServerSocket(7912);
// Protocol factory
TProtocolFactory tProtocolFactory = null;
if (protocol_type.equals("json")) {
tProtocolFactory = new TJSONProtocol.Factory();
} else if (protocol_type.equals("compact")) {
tProtocolFactory = new TCompactProtocol.Factory();
} else {
tProtocolFactory = new TBinaryProtocol.Factory();
}
TTransportFactory tTransportFactory = null;
if (transport_type.equals("framed")) {
tTransportFactory = new TFramedTransport.Factory();
} else if (transport_type.equals("fastframed")) {
tTransportFactory = new TFastFramedTransport.Factory();
} else { // .equals("buffered") => default value
tTransportFactory = new TTransportFactory();
}
TServerSocket serverTransport = new TServerSocket(new TServerSocket.ServerSocketTransportArgs().port(port));;
com.winwill.thrift.HelloWordService.Processor processor = new com.winwill.thrift.HelloWordService.Processor(new HelloWordServiceImpl());
TServer.Args tServerArgs = new TServer.Args(serverTransport);
tServerArgs.processor(processor);
tServerArgs.protocolFactory(tProtocolFactory);
tServerArgs.transportFactory(tTransportFactory);
TServer server = new TSimpleServer(tServerArgs);
System.out.println("Running server...");
server.serve();
}
}
3.客戶端請求
package com.winwill.thrift;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class HelloWordClient {
public static void main(String[] args) throws Exception {
TTransport transport = new TSocket("【此處使用服務器ip地址】", 7912);
TProtocol protocol = new TBinaryProtocol(transport);
// 創建client
com.winwill.thrift.HelloWordService.Client client = new com.winwill.thrift.HelloWordService.Client(protocol);
transport.open(); // 建立連接
// 第一種請求類型
com.winwill.thrift.Request request = new com.winwill.thrift.Request()
.setType(com.winwill.thrift.RequestType.SAY_HELLO).setName("winwill2012").setAge(24);
System.out.println(client.doAction(request));
// 第二種請求類型
request.setType(com.winwill.thrift.RequestType.QUERY_TIME).setName("winwill2012");
System.out.println(client.doAction(request));
transport.close(); // 請求結束,斷開連接
}
}
在一臺服務器上測試啟動服務
輸出Running server...
在另一臺機器啟動客戶端
輸出
Hello, winwill2012, Welcome!
Hello, winwill2012, Now is 2016-10-13 17:06:47
此時服務器端輸出
Get request: Request(type:SAY_HELLO, name:winwill2012, age:24)
Get request: Request(type:QUERY_TIME, name:winwill2012, age:24)
說明已經成功連接啦
本文所用到的工具和工程已經打包上傳到云盤,歡迎大家下載
鏈接:http://pan.baidu.com/s/1jHQXSma 密碼:65uh
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。