Java thrift服務(wù)器和客戶端創(chuàng)建實(shí)例代碼
Thrift是一個軟件框架,用來進(jìn)行可擴(kuò)展且跨語言的服務(wù)的開發(fā)。它結(jié)合了功能強(qiáng)大的軟件堆棧和代碼生成引擎,以構(gòu)建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 等等編程語言間無縫結(jié)合的、高效的服務(wù)。
Thrift最初由facebook開發(fā),07年四月開放源碼,08年5月進(jìn)入apache孵化器。thrift允許你定義一個簡單的定義文件中的數(shù)據(jù)類型和服務(wù)接口。以作為輸入文件,編譯器生成代碼用來方便地生成RPC客戶端和服務(wù)器通信的無縫跨編程語言。
首先環(huán)境介紹一下:
1.IntelliJ IDEA 2017.1
2.thrift-0.9.3
相信大家在看我這篇文章的時候已經(jīng)對thrift通信框架已有所調(diào)研,這里就不再贅述了,直接進(jìn)入正題:
<1>創(chuàng)建HelloWorld.thrift
namespace java com.thrift.demo
service HelloWorldService{ string sayHello(1:string username) }
<2>利用thrift生成HelloWorld.java文件,cmd指令下進(jìn)入thrift當(dāng)前目錄下輸入命令
thrift.exe -gen java HelloWorld.thrift
java為要生成文件的類型,HelloWorld.thrift為前面的文件。
<3>創(chuàng)建IDEA 下的maven項(xiàng)目,其中的好處就不一一說明了,最重要的一條就是可以在pom.xml文件中添加dependency,能夠在項(xiàng)目中自行下載庫文件,方便協(xié)同開發(fā)中出現(xiàn)的開發(fā)包不對應(yīng)的情況。
<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)的依賴項(xiàng)就可以讓它自行下載。
<4>項(xiàng)目的結(jié)構(gòu)圖當(dāng)前如下所示:
File--Project Structure--Modules,在main文件夾下新建java文件夾并設(shè)為Soueces類型(因?yàn)樵赟ources文件下可以新建java class文件)
同時將thrift生成的HelloWorld.java文件復(fù)制到該目錄下
<5>實(shí)現(xiàn)接口Iface
java代碼:HelloWorldImpl.java
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
/** * 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>編寫客戶端代碼
java代碼:Client.java
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"); } }
客戶端測試成功,截圖如下:
以上所述是小編給大家介紹的Java thrift服務(wù)器和客戶端創(chuàng)建實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Vue+Java 通過websocket實(shí)現(xiàn)服務(wù)器與客戶端雙向通信操作
- Java利用TCP協(xié)議實(shí)現(xiàn)客戶端與服務(wù)器通信(附通信源碼)
- Java實(shí)現(xiàn)UDP通信過程實(shí)例分析【服務(wù)器端與客戶端】
- java UDP通信客戶端與服務(wù)器端實(shí)例分析
- java Tcp通信客戶端與服務(wù)器端實(shí)例
- java模擬TCP通信實(shí)現(xiàn)客戶端上傳文件到服務(wù)器端
- java模擬客戶端向服務(wù)器上傳文件
- java實(shí)現(xiàn)客戶端向服務(wù)器發(fā)送文件
- Java實(shí)現(xiàn)文件上傳服務(wù)器和客戶端
- java實(shí)現(xiàn)上傳文件到服務(wù)器和客戶端
- Java Socket編程服務(wù)器響應(yīng)客戶端實(shí)例代碼
- java多線程實(shí)現(xiàn)服務(wù)器端與多客戶端之間的通信
- 教你怎么用java實(shí)現(xiàn)客戶端與服務(wù)器一問一答
相關(guān)文章
springboot項(xiàng)目數(shù)據(jù)庫密碼如何加密
在我們?nèi)粘i_發(fā)中,我們可能很隨意把數(shù)據(jù)庫密碼直接明文暴露在配置文件中,今天就來聊聊在springboot項(xiàng)目中如何對數(shù)據(jù)庫密碼進(jìn)行加密,感興趣的可以了解一下2021-07-07SpringCloud讓微服務(wù)實(shí)現(xiàn)指定程序調(diào)用
這篇文章主要介紹了SpringCloud讓微服務(wù)實(shí)現(xiàn)指定程序調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06SpringBoot利用切面注解及反射實(shí)現(xiàn)事件監(jiān)聽功能
這篇文章主要介紹了springboot事件監(jiān)聽,通過利用切面、注解、反射實(shí)現(xiàn),接下來將對這幾種方式逐一說明,具有很好的參考價(jià)值,希望對大家有所幫助2022-07-07springboot集成swagger3與knife4j的詳細(xì)代碼
這篇文章主要介紹了springboot集成swagger3與knife4j,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08啟用Spring事務(wù)管理@EnableTransactionManagement示例解析
這篇文章主要為大家介紹了啟用Spring事務(wù)管理@EnableTransactionManagement示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Java獲取漢字拼音的全拼和首拼實(shí)現(xiàn)代碼分享
這篇文章主要介紹了Java獲取漢字拼音的全拼和首拼實(shí)現(xiàn)代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06