thrift是一個(gè)軟件框架,用來(lái)進(jìn)行可擴(kuò)展且跨語(yǔ)言的服務(wù)的開(kāi)發(fā)。它結(jié)合了功能強(qiáng)大的軟件堆棧和代碼生成引擎,以構(gòu)建在 c++, java, python, php, ruby, erlang, perl, haskell, c#, cocoa, javascript, node.js, smalltalk, and ocaml 等等編程語(yǔ)言間無(wú)縫結(jié)合的、高效的服務(wù)。
thrift最初由facebook開(kāi)發(fā),07年四月開(kāi)放源碼,08年5月進(jìn)入apache孵化器。thrift允許你定義一個(gè)簡(jiǎn)單的定義文件中的數(shù)據(jù)類(lèi)型和服務(wù)接口。以作為輸入文件,編譯器生成代碼用來(lái)方便地生成rpc客戶(hù)端和服務(wù)器通信的無(wú)縫跨編程語(yǔ)言。
首先環(huán)境介紹一下:
1.intellij idea 2017.1
2.thrift-0.9.3
相信大家在看我這篇文章的時(shí)候已經(jīng)對(duì)thrift通信框架已有所調(diào)研,這里就不再贅述了,直接進(jìn)入正題:
<1>創(chuàng)建helloworld.thrift
namespace java com.thrift.demo
1
2
3
|
service helloworldservice{ string sayhello( 1 :string username) } |
<2>利用thrift生成helloworld.java文件,cmd指令下進(jìn)入thrift當(dāng)前目錄下輸入命令
1
|
thrift.exe -gen java helloworld.thrift |
java為要生成文件的類(lèi)型,helloworld.thrift為前面的文件。
<3>創(chuàng)建idea 下的maven項(xiàng)目,其中的好處就不一一說(shuō)明了,最重要的一條就是可以在pom.xml文件中添加dependency,能夠在項(xiàng)目中自行下載庫(kù)文件,方便協(xié)同開(kāi)發(fā)中出現(xiàn)的開(kāi)發(fā)包不對(duì)應(yīng)的情況。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version> 3.1 . 0 </version> </dependency> <dependency> <groupid>jstl</groupid> <artifactid>jstl</artifactid> <version> 1.2 </version> </dependency> <dependency> <groupid>org.apache.thrift</groupid> <artifactid>libthrift</artifactid> <version> 0.9 . 3 </version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version> 1.7 . 5 </version> </dependency> |
上述pom引入相應(yīng)的依賴(lài)項(xiàng)就可以讓它自行下載。
<4>項(xiàng)目的結(jié)構(gòu)圖當(dāng)前如下所示:
file--project structure--modules,在main文件夾下新建java文件夾并設(shè)為soueces類(lèi)型(因?yàn)樵趕ources文件下可以新建java class文件)
同時(shí)將thrift生成的helloworld.java文件復(fù)制到該目錄下
<5>實(shí)現(xiàn)接口iface
java代碼:helloworldimpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.jmust.thrift.demo; import org.apache.thrift.texception; /** * created by administrator on 2017/3/31. */ public class helloworldimpl implements helloworldservice.iface { public helloworldimpl() { } @override public string sayhello(string username) throws texception { return "hi," +username+ "welcome to my blog http://www.cnblogs.com/zfygiser" ; } } |
<6>服務(wù)端tsimpleserver
java代碼:helloserver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/** * created by administrator on 2017/3/31. */ package com.jmust.thrift.demo; import org.apache.thrift.tprocessor; import org.apache.thrift.protocol.tbinaryprotocol; import org.apache.thrift.server.tserver; import org.apache.thrift.server.tsimpleserver; import org.apache.thrift.transport.tserversocket; public class helloserver { public final static int server_port = 7099 ; private static string server_ip = "localhost" ; public void startserver() { try { system.out.println( "helloworld server start..." ); tserversocket servertransport = new tserversocket(server_port); tserver.args args = new tserver.args(servertransport); tprocessor process = new helloworldservice.processor( new helloworldimpl()); tbinaryprotocol.factory portfactory = new tbinaryprotocol.factory( true , true ); args.processor(process); args.protocolfactory(portfactory); tserver server = new tsimpleserver(args); server.serve(); } catch (exception e) { system.out.println( "server start error" ); e.printstacktrace(); } } public static void main(string[] args) { helloserver server = new helloserver(); server.startserver(); } } |
<7>編寫(xiě)客戶(hù)端代碼
java代碼:client.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.jmust.thrift.demo; import org.apache.thrift.protocol.tbinaryprotocol; import org.apache.thrift.protocol.tprotocol; import org.apache.thrift.transport.tsocket; import org.apache.thrift.transport.ttransport; /** * created by administrator on 2017/4/1. */ public class client { public static final int server_port = 7099 ; public static final string server_ip = "localhost" ; public void startclient(string username) { ttransport ttransport = null ; try { ttransport = new tsocket(server_ip, server_port); //協(xié)議要和服務(wù)端一致 tprotocol protocol = new tbinaryprotocol(ttransport); helloworldservice.client client = new helloworldservice.client(protocol); ttransport.open(); string result = client.sayhello(username); system.out.println( "thrift client result=" + result); } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args) { client client = new client(); client.startclient( "zfy" ); } } |
客戶(hù)端測(cè)試成功,截圖如下:
以上所述是小編給大家介紹的java thrift服務(wù)器和客戶(hù)端創(chuàng)建實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/zfygiser/archive/2017/04/01/6651645.html