在Windows系統(tǒng)下安裝Thrift的方法與使用講解
安裝
下載
下載地址:http://archive.apache.org/dist/thrift/0.10.0/
將thrift-0.10.0.exe放到一個(gè)文件下,如F:\thrift下,將其重命名為thrift.exe。如果不重命名,需要使用thrift-0.10.0調(diào)用thrift命令。
配置環(huán)境變量
向Path
中添加變量值,值為thrift.exe
的地址,如F:\thrift。
測(cè)試
命令行輸入thrift -version
,如果輸出thrift的版本即表明安裝成功。
使用
編寫(xiě)IDL接口
HelloService.thrift
namespace java com.thrift.demo.service service HelloService{ string sayHello(1:string username) }
編譯
編譯之后會(huì)生成類(lèi)HelloService
。
thrift -gen java HelloService.thrift
編寫(xiě)實(shí)現(xiàn)類(lèi)
HelloServiceImpl.java
public class HelloServiceImpl implements HelloService.Iface { @Override public String sayHello(String username) throws TException { return "Hello Thrift Service : " + username; } }
編寫(xiě)服務(wù)端代碼
public class HelloServer { public static final int SERVER_PORT = 8090; public void startServer() { try { System.out.println("HelloService TSimpleServer start ...."); TProcessor tprocessor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl()); // 簡(jiǎn)單的單線(xià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(); } } public static void main(String[] args) { HelloServer server = new HelloServer(); server.startServer(); } }
編寫(xiě)客戶(hù)端代碼
public class HelloClient { public static final String SERVER_IP = "localhost"; public static final int SERVER_PORT = 8090; public static final int TIMEOUT = 30000; public void startClient(String userName) { TTransport transport = null; try { transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT); // 協(xié)議要和服務(wù)端一致 TProtocol protocol = new TBinaryProtocol(transport); HelloService.Client client = new HelloService.Client(protocol); transport.open(); String result = client.sayHello(userName); System.out.println("Thrify client result =: " + result); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } finally { if (null != transport) { transport.close(); } } } public static void main(String[] args) { HelloClient client = new HelloClient(); client.startClient("Michael"); } }
運(yùn)行
先運(yùn)行服務(wù)端,再運(yùn)行客戶(hù)端。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
java定義通用返回結(jié)果類(lèi)ResultVO使用示例詳解
這篇文章主要為大家介紹了java定義通用返回結(jié)果類(lèi)ResultVO使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09idea啟動(dòng)與jar包啟動(dòng)中使用resource資源文件路徑的問(wèn)題
這篇文章主要介紹了idea啟動(dòng)與jar包啟動(dòng)中使用resource資源文件路徑的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07實(shí)例講解Java的MyBatis框架對(duì)MySQL中數(shù)據(jù)的關(guān)聯(lián)查詢(xún)
這里我們來(lái)以實(shí)例講解Java的MyBatis框架對(duì)MySQL中數(shù)據(jù)的關(guān)聯(lián)查詢(xún),包括一對(duì)多、多對(duì)一的關(guān)聯(lián)查詢(xún)以及自身關(guān)聯(lián)映射的方法等,需要的朋友可以參考下2016-06-06Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式
這篇文章主要介紹了Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08關(guān)于@Configuration的作用說(shuō)明
這篇文章主要介紹了關(guān)于@Configuration的作用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01淺談常用Java數(shù)據(jù)庫(kù)連接池(小結(jié))
這篇文章主要介紹了淺談常用Java數(shù)據(jù)庫(kù)連接池(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Java實(shí)現(xiàn)文件分片上傳接口的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)文件分片上傳的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-07-07idea中使用SonarLint進(jìn)行代碼規(guī)范檢測(cè)及使用方法
這篇文章主要介紹了idea中使用SonarLint進(jìn)行代碼規(guī)范檢測(cè),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08Java 實(shí)現(xiàn)并發(fā)的幾種方式小結(jié)
這篇文章主要介紹了Java 實(shí)現(xiàn)并發(fā)的幾種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05