国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

在Windows系統下安裝Thrift的方法與使用講解

2024-07-14 08:43:12
字體:
來源:轉載
供稿:網友

安裝

下載

下載地址:http://archive.apache.org/dist/thrift/0.10.0/

將thrift-0.10.0.exe放到一個文件下,如F:/thrift下,將其重命名為thrift.exe。如果不重命名,需要使用thrift-0.10.0調用thrift命令。

配置環境變量

Path中添加變量值,值為thrift.exe的地址,如F:/thrift。

測試

命令行輸入thrift -version,如果輸出thrift的版本即表明安裝成功。

使用

編寫IDL接口

HelloService.thrift

namespace java com.thrift.demo.service service HelloService{  string sayHello(1:string username)}

編譯

編譯之后會生成類HelloService

thrift -gen java HelloService.thrift

編寫實現類

HelloServiceImpl.java

public class HelloServiceImpl implements HelloService.Iface { @Override public String sayHello(String username) throws TException { return "Hello Thrift Service : " + username; }}

編寫服務端代碼

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());  // 簡單的單線程服務模型,一般用于測試  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(); }}

編寫客戶端代碼

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);  // 協議要和服務端一致  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"); }}

運行

先運行服務端,再運行客戶端。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VeVb武林網的支持。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳朔县| 治县。| 额济纳旗| 邛崃市| 治多县| 黄平县| 崇州市| 和田市| 宝山区| 山丹县| 囊谦县| 黄浦区| 苍溪县| 大安市| 阳春市| 贵港市| 昌乐县| 宜阳县| 辰溪县| 武宁县| 梨树县| 台湾省| 遂川县| 八宿县| 芜湖市| 葵青区| 望都县| 罗定市| 宜兴市| 鹿泉市| 萍乡市| 阜阳市| 广饶县| 平遥县| 迭部县| 玉门市| 天气| 林甸县| 江北区| 武宣县| 揭东县|