RPC-Thrift简短实例

作者: 老生住长亭 | 来源:发表于2018-03-30 20:56 被阅读20次
    1. Thrift 需要实体类作用介绍
      TSimpleServer – 简单的单线程服务模型,常用于测试
      TThreadedServer – 多线程服务模型,使用阻塞式IO,每个请求创建一个线程。(java 不支持)
      TThreadPoolServer – 多线程服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
      TThreadedSelectorServer 允许你用多个线程来处理网络I/O。它维护了两个线程池,一个用来处理网络I/O,另一个用来进行请求的处理
      TNonblockingServer – 多线程服务模型,使用非阻塞式IO(需使用TFramedTransport数据传输方式),只有一个线程来处理消息
      THsHaServer - 半同步半异步的服务模型,一个单独的线程用来处理网络I/O,一个worker线程池用来进行消息的处理

    2.程序的开篇鼻祖Helloword实例

    2.1 Thrift 定义文件:
    namespace java com.interfaces
    service Hello {
    string say(1:string param)
    }

    2.2 执行命令,生成Hello接口文件:

    thrift -out ~/studyproject/rpc-thrift/src/main/java/ -gen java Hello.thrift

    2.3 实现Hello接口:

    Impl:
    package com.example;

    import org.apache.thrift.TException;

    /**

    • Created by botter on 10/6/16.
      */
      public class HelloServiceImpl implements Hello.Iface {
      @Override
      public String say(String param) throws TException {
      return "hello " + param;
      }
      }

    2.4 Server端代码:

    import com.example.Hello;
    import com.example.HelloServiceImpl;
    import org.apache.thrift.TProcessor;
    import org.apache.thrift.server.TServer;
    import org.apache.thrift.server.TSimpleServer;
    import org.apache.thrift.transport.TServerSocket;
    import org.apache.thrift.transport.TServerTransport;
    import org.apache.thrift.transport.TTransportException;

    /**

    • Created by botter on 10/6/16.
      */
      public class Server {

      public static void main(String[] args) {

       try {
           TServerTransport tServerSocket = new TServerSocket(9000);
      
           TServer.Args args1 = new TServer.Args(tServerSocket);
      
           TProcessor tProcessor = new Hello.Processor(new HelloServiceImpl());
      
           TServer tServer = new TSimpleServer(args1.processor(tProcessor));
      
           tServer.serve();
       } catch (TTransportException e) {
           e.printStackTrace();
       }
      

      }
      }

    2.5 Client端代码:
    import com.example.Hello;
    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;

    /**

    • Created by botter on 10/6/16.
      */
      public class Client {

      public static void main(String[] args) {
      TTransport tTransport = new TSocket("localhost", 9000);
      try {
      tTransport.open();
      TProtocol tProtocol = new TBinaryProtocol(tTransport);
      Hello.Client client = new Hello.Client(tProtocol);
      String foo = client.say("TOM");
      System.out.println("return :" + foo);
      tTransport.close();
      } catch (TTransportException e) {
      e.printStackTrace();
      } catch (TException e) {
      e.printStackTrace();
      }
      }
      }

    相关文章

      网友评论

      本文标题:RPC-Thrift简短实例

      本文链接:https://www.haomeiwen.com/subject/vqcwcftx.html