1、添加依賴 jar
<dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.8.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency>
2、編寫IDL文件 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()
}
3、生成代碼
thrift -o <output directory> -gen java Hello.thrift
生成代碼縮略圖:
4、編寫實(shí)現(xiàn)類、實(shí)現(xiàn)Hello.Iface:
縮略圖:

5、編寫服務(wù)端,發(fā)布(阻塞式IO + 多線程處理)服務(wù)。
- /**
- * 阻塞式、多線程處理
- *
- * @param args
- */
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public static void main(String[] args) {
- try {
- //設(shè)置傳輸通道,普通通道
- TServerTransport serverTransport = new TServerSocket(7911);
-
- //使用高密度二進(jìn)制協(xié)議
- TProtocolFactory proFactory = new TCompactProtocol.Factory();
-
- //設(shè)置處理器HelloImpl
- TProcessor processor = new Hello.Processor(new HelloImpl());
-
- //創(chuàng)建服務(wù)器
- TServer server = new TThreadPoolServer(
- new Args(serverTransport)
- .protocolFactory(proFactory)
- .processor(processor)
- );
-
- System.out.println("Start server on port 7911...");
- server.serve();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
6、編寫客戶端,調(diào)用(阻塞式IO + 多線程處理)服務(wù):
- public static void main(String[] args) throws Exception {
- // 設(shè)置傳輸通道 - 普通IO流通道
- TTransport transport = new TSocket("localhost", 7911);
- transport.open();
-
- //使用高密度二進(jìn)制協(xié)議
- TProtocol protocol = new TCompactProtocol(transport);
-
- //創(chuàng)建Client
- Hello.Client client = new Hello.Client(protocol);
-
- long start = System.currentTimeMillis();
- for(int i=0; i<10000; i++){
- client.helloBoolean(false);
- client.helloInt(111);
- client.helloNull();
- client.helloString("dongjian");
- client.helloVoid();
- }
- System.out.println("耗時(shí):" + (System.currentTimeMillis() - start));
-
- //關(guān)閉資源
- transport.close();
- }
現(xiàn)在已完成整個(gè)開發(fā)過程,超級無敵簡單。
其中服務(wù)端使用的協(xié)議需要與客戶端保持一致。
-------------------------------------------------------------------------------------------------------------------
上面展示了普通且常用的服務(wù)端和客戶端,下面請看非阻塞IO,即java中的NIO:
基于非阻塞IO(NIO)的服務(wù)端:
- public static void main(String[] args) {
- try {
- //傳輸通道 - 非阻塞方式
- TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
-
- //異步IO,需要使用TFramedTransport,它將分塊緩存讀取。
- TTransportFactory transportFactory = new TFramedTransport.Factory();
-
- //使用高密度二進(jìn)制協(xié)議
- TProtocolFactory proFactory = new TCompactProtocol.Factory();
-
- //設(shè)置處理器 HelloImpl
- TProcessor processor = new Hello.Processor(new HelloImpl());
-
- //創(chuàng)建服務(wù)器
- TServer server = new TThreadedSelectorServer(
- new Args(serverTransport)
- .protocolFactory(proFactory)
- .transportFactory(transportFactory)
- .processor(processor)
- );
-
- System.out.println("Start server on port 7911...");
- server.serve();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
調(diào)用非阻塞IO(NIO)服務(wù)的客戶端:
- public static void main(String[] args) throws Exception {
- //設(shè)置傳輸通道,對于非阻塞服務(wù),需要使用TFramedTransport,它將數(shù)據(jù)分塊發(fā)送
- TTransport transport = new TFramedTransport(new TSocket("localhost", 7911));
- transport.open();
-
- //使用高密度二進(jìn)制協(xié)議
- TProtocol protocol = new TCompactProtocol(transport);
-
- //創(chuàng)建Client
- Hello.Client client = new Hello.Client(protocol);
-
- long start = System.currentTimeMillis();
- for(int i=0; i<10000; i++){
- client.helloBoolean(false);
- client.helloInt(111);
- client.helloNull();
- client.helloString("360buy");
- client.helloVoid();
- }
- System.out.println("耗時(shí):" + (System.currentTimeMillis() - start));
-
- //關(guān)閉資源
- transport.close();
- }
-----------------------------------------------------------------------------------------------------------------------------------
客戶端異步調(diào)用:
- /** 調(diào)用[非阻塞IO]服務(wù),異步 */
- public static void main(String[] args) {
- try {
- //異步調(diào)用管理器
- TAsyncClientManager clientManager = new TAsyncClientManager();
- //設(shè)置傳輸通道,調(diào)用非阻塞IO。
- final TNonblockingTransport transport = new TNonblockingSocket("localhost", 7911);
- //設(shè)置協(xié)議
- TProtocolFactory protocol = new TCompactProtocol.Factory();
- //創(chuàng)建Client
- final Hello.AsyncClient client = new Hello.AsyncClient(protocol, clientManager, transport);
- // 調(diào)用服務(wù)
- System.out.println("開始:" + System.currentTimeMillis());
- client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
- public void onError(Exception exception) {
- System.out.println("錯(cuò)誤1: " + System.currentTimeMillis());
- }
- public void onComplete(helloBoolean_call response) {
- System.out.println("完成1: " + System.currentTimeMillis());
- try {
- client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
- public void onError(Exception exception) {
- System.out.println("錯(cuò)誤2: " + System.currentTimeMillis());
- }
-
- public void onComplete(helloBoolean_call response) {
- System.out.println("完成2: " + System.currentTimeMillis());
- transport.close();
- }
- });
- } catch (TException e) {
- e.printStackTrace();
- }
- }
- });
- System.out.println("結(jié)束:" + System.currentTimeMillis());
- Thread.sleep(5000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-----------------------------------------------------------------------------------------------------------------------------------
使用SSL的服務(wù)端:

調(diào)用基于SSL服務(wù)端的客戶端:
