使用Java快速入门的Apache Thrift
根據(jù)官方網(wǎng)站 Apache Thrift的說法,該軟件框架用于可擴(kuò)展的跨語言服務(wù)開發(fā),它結(jié)合了軟件堆棧和代碼生成引擎,以構(gòu)建可在C ++,Java,Python,PHP,Ruby, Erlang,Perl,Haskell,C#,Cocoa,JavaScript,Node.js,Smalltalk,OCaml和Delphi等語言。 圖片由維基百科提供
在Windows中安裝Apache Thrift
安裝節(jié)儉可能是一個(gè)令人厭煩的過程。 但是對(duì)于Windows,編譯器可以作為預(yù)構(gòu)建的exe使用。 下載thrift.exe并將其添加到您的環(huán)境變量中。
編寫Thrift定義文件(.thrift文件)
一旦習(xí)慣了,編寫Thrift定義文件就變得非常容易。 我發(fā)現(xiàn)本教程非常有用。
定義文件示例(add.thrift)
namespace java com.eviac.blog.samples.thrift.server // defines the namespace typedef i32 int //typedefs to get convenient names for your typesservice AdditionService { // defines the service to add two numbersint add(1:int n1, 2:int n2), //defines a method }編譯Thrift定義文件
要編譯.thrift文件,請(qǐng)使用以下命令。
thrift --gen <language> <Thrift filename>在我的示例中,命令是
thrift --gen java add.thrift執(zhí)行完該命令后,您將在gen-java目錄中找到對(duì)構(gòu)建RPC客戶端和服務(wù)器有用的源代碼。 在我的示例中,它將創(chuàng)建一個(gè)名為AdditionService.java的Java代碼。
編寫服務(wù)處理程序
服務(wù)處理程序類是實(shí)現(xiàn)AdditionService.Iface接口所必需的。
示例服務(wù)處理程序(AdditionServiceHandler.java)
package com.eviac.blog.samples.thrift.server;import org.apache.thrift.TException;public class AdditionServiceHandler implements AdditionService.Iface {@Overridepublic int add(int n1, int n2) throws TException {return n1 + n2;}}編寫一個(gè)簡(jiǎn)單的服務(wù)器
以下是啟動(dòng)簡(jiǎn)單的節(jié)儉服務(wù)器的示例代碼。 要啟用多線程服務(wù)器,請(qǐng)取消注釋示例代碼的注釋部分。
示例服務(wù)器(MyServer.java)
package com.eviac.blog.samples.thrift.server;import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TServerTransport; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TServer.Args; import org.apache.thrift.server.TSimpleServer;public class MyServer {public static void StartsimpleServer(AdditionService.Processor<AdditionServiceHandler> processor) {try {TServerTransport serverTransport = new TServerSocket(9090);TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));// Use this for a multithreaded server// TServer server = new TThreadPoolServer(new// TThreadPoolServer.Args(serverTransport).processor(processor));System.out.println("Starting the simple server...");server.serve();} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {StartsimpleServer(new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler()));}}寫客戶
以下是使用AdditionService提供的服務(wù)的示例Java客戶端代碼。
客戶端代碼示例(AdditionClient.java)
package com.eviac.blog.samples.thrift.client;import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException;public class AdditionClient {public static void main(String[] args) {try {TTransport transport;transport = new TSocket("localhost", 9090);transport.open();TProtocol protocol = new TBinaryProtocol(transport);AdditionService.Client client = new AdditionService.Client(protocol);System.out.println(client.add(100, 200));transport.close();} catch (TTransportException e) {e.printStackTrace();} catch (TException x) {x.printStackTrace();}}}運(yùn)行服務(wù)器代碼(MyServer.java)。 它應(yīng)該輸出以下內(nèi)容,并將監(jiān)聽請(qǐng)求。
Starting the simple server...然后運(yùn)行客戶端代碼(AdditionClient.java)。 它應(yīng)該輸出以下內(nèi)容。
300參考: EVIAC博客上的JCG合作伙伴 Pavithra Siriwardena的Apache Thrift with Java快速入門 。
翻譯自: https://www.javacodegeeks.com/2012/07/apache-thrift-with-java-quickstart.html
總結(jié)
以上是生活随笔為你收集整理的使用Java快速入门的Apache Thrift的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑如何打开1433端口映射(电脑怎么打
- 下一篇: 剪刀手爱德华结局深层寓意(剪刀手爱德华剧