神刀安全网

thrift入门教程–java篇

最近因为工作需要,需要使用thrift作为中间语言进行开发,空闲之余写这篇文章作为笔记,也提供给新手作为入门之用。
官网对thrift解释如下:

The Apache Thrift software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.

好吧,简单来说,thrift是一种可用于RPC通信,C/S(客户端/服务器)结构的,跨语言的IDL(Interface Description Language)。跨语言使得客户端服务器可以用不同语言进行开发,能显著提高开发的效率和灵活性。

  • 安装篇
    • 笔者写这篇文章时候已经0.9.x 的版本了,下载地址: http://thrift.apache.org/download
    • 执行命令(根据自己使用版本替换文件和目录名称):
      tar xvzf thrift.tar.gz cd thrift ./configure && make sudo make install
    • 执行thrift命令,观察是否有Usage信息输出,如果有,恭喜,安装已经成功
  • 运行调试篇

    • 运行环境jdk1.8,maven,intellij idea
    • 笔者选用的文件目录树,读者也可自行安排
      thrift入门教程--java篇
      Paste_Image.png

    • 在pom.xml中添加以下依赖(thrift依赖根据使用的版本自行添加):
      <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.5.8</version> </dependency>
    • 编写demoHello.thrift代码:
      service  HelloWorldService {   string sayHello(1:string username) }
    • 将代码转换为java语言,并把代码移动到上图所示main/java(如果对java目录树比较熟悉随意)
      thrift -gen java demoHello.thrift
    • 现在已经有HelloWorldService.java文件了,接下来是接口:
      import org.apache.thrift.TException; public class HelloWorldImpl implements HelloWorldService.Iface {     public HelloWorldImpl() {       }       @Override       public String sayHello(String username) throws TException {           return "Hi," + username + " welcome to jianshu";       } }
    • 然后是服务端:

      import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TTransportException; import java.io.IOException; import java.net.ServerSocket; public class HelloServerDemo { public static void main(String[] args) throws IOException, TTransportException {   final int SERVER_PORT = 8090;   ServerSocket socket = new ServerSocket(SERVER_PORT);   TServerSocket serverTransport = new TServerSocket(socket);   HelloWorldService.Processor processor = new HelloWorldService.Processor(new HelloWorldImpl());   TServer.Args tArgs = new TServer.Args(serverTransport);   tArgs.processor(processor);   tArgs.protocolFactory(new TBinaryProtocol.Factory());   TServer server = new TSimpleServer(tArgs);    System.out.println("Running server...");   server.serve(); } }
    • 最后是客户端:
      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 HelloClientDemo { 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);       HelloWorldService.Client client = new HelloWorldService.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) {   HelloClientDemo client = new HelloClientDemo();   client.startClient("Vilarsail"); } }
    • 程序写完了,先运行server,再运行client就可以看到输出了
      Thrify client result =: Hi,Vilarsail welcome to my jianshu

最后为了支持原创,添加笔者的参考博文:
http://www.micmiu.com/soa/rpc/thrift-sample/
http://www.jianshu.com/p/0f4113d6ec4b
这两篇文章可能都会有编译不过或缺少依赖等等细小问题,读者可作为参考,如需转载请注明文章出处。

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » thrift入门教程–java篇

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址