thrift servr

1.同步阻塞

      CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
          new CaculatorServiceHandler());
      TServerTransport serverTransport = new TServerSocket(8888);
      Args targs = new Args(serverTransport);
      targs.processor(processor);
      targs.transportFactory(new TFramedTransport.Factory());
      targs.protocolFactory(new TCompactProtocol.Factory());
      TServer server = new TSimpleServer(targs);
      System.out.println("Starting the TSimpleServer...");
      server.serve();

      CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
          new CaculatorServiceHandler());
      TServerTransport serverTransport = new TServerSocket(8888);
      org.apache.thrift.server.TThreadPoolServer.Args targs = new org.apache.thrift.server.TThreadPoolServer.Args(serverTransport);
      targs.processor(processor);
      targs.transportFactory(new TFramedTransport.Factory());
      targs.protocolFactory(new TCompactProtocol.Factory());
      TThreadPoolServer server = new TThreadPoolServer(targs);
      System.out.println("Starting the TThreadPoolServer...");
      server.serve();

      TTransport transport = new TFramedTransport(new TSocket("localhost", 8888));
      transport.open();
      TProtocol protocol = new TCompactProtocol(transport);
      CaculatorService.Client client = new CaculatorService.Client(protocol);
      transport.close();

2.同步非阻塞

      CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
          new CaculatorServiceHandler());
      TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(8888);
      org.apache.thrift.server.TNonblockingServer.Args targs = new org.apache.thrift.server.TNonblockingServer.Args(serverTransport);
      targs.processor(processor);
      targs.transportFactory(new TFramedTransport.Factory());
      targs.protocolFactory(new TCompactProtocol.Factory());
      TNonblockingServer server = new TNonblockingServer(targs);
      System.out.println("Starting the TNonblockingServer...");
      server.serve();

      CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
          new CaculatorServiceHandler());
      TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(8888);
      org.apache.thrift.server.THsHaServer.Args targs = new org.apache.thrift.server.THsHaServer.Args(serverTransport);
      targs.processor(processor);
      targs.transportFactory(new TFramedTransport.Factory());
      targs.protocolFactory(new TCompactProtocol.Factory());
      TServer server = new THsHaServer(targs);
      System.out.println("Starting the THsHaServer...");
      server.serve();
      CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
          new CaculatorServiceHandler());
      TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(8888);
      org.apache.thrift.server.TThreadedSelectorServer.Args targs = new org.apache.thrift.server.TThreadedSelectorServer.Args(
          serverTransport);
      targs.processor(processor);
      targs.transportFactory(new TFramedTransport.Factory());
      targs.protocolFactory(new TCompactProtocol.Factory());
      TServer server = new TThreadedSelectorServer(targs);
      System.out.println("Starting the TThreadedSelectorServer...");
      server.serve();

      TTransport transport = new TFramedTransport(new TSocket("localhost", 8888));
      transport.open();
      TProtocol protocol = new TCompactProtocol(transport);
      CaculatorService.Client client = new CaculatorService.Client(protocol);
      transport.close();

3.异步,注册回调

    try {
      TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
      TAsyncClientManager clientManager = new TAsyncClientManager();
      TNonblockingSocket transport = new TNonblockingSocket("localhost", 8888);
      CaculatorService.AsyncClient client = new CaculatorService.AsyncClient(protocolFactory, clientManager, transport);
      Random random = new Random();
      for (int i = 0; i < 10; ++i) {
        int x = random.nextInt(1000), y = random.nextInt(1000);
        System.out.println("" + x + " + " + y + " = ?");
        try {
          client.add(x, y, new MyCallback());
          Thread.sleep(100);
        } catch (TException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      transport.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

class MyCallback implements AsyncMethodCallback<CaculatorService.AsyncClient.add_call> {

  @Override
  public void onComplete(add_call arg0) {
    // TODO Auto-generated method stub
    try {
      System.out.println(arg0.getResult());
    } catch (TException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  @Override
  public void onError(Exception arg0) {
    // TODO Auto-generated method stub
    System.out.println(arg0.toString());
  }
  
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Thrift is a framework for building cross-language services. It allows you to define data types and service interfaces in a simple definition file, and then generate code in various languages to implement those interfaces. In Python, you can use the Thrift library to create both client and server applications. To use Thrift in Python, you first need to install the Thrift library. You can do this using pip: ``` pip install thrift ``` Once installed, you can start using Thrift in your Python code. Here's a simple example to get you started: 1. Define your data types and service interface in a Thrift IDL file (e.g., `example.thrift`): ``` namespace py example struct MyStruct { 1: required string name 2: optional i32 age } service MyService { MyStruct getStruct(1: string id) } ``` 2. Generate Python code from the IDL file using the `thrift` compiler: ``` thrift --gen py example.thrift ``` 3. Implement the service interface in Python: ```python from example import MyService, ttypes class MyHandler: def getStruct(self, id): # Implementation code goes here return ttypes.MyStruct(name="John", age=25) handler = MyHandler() processor = MyService.Processor(handler) # Run the server transport = TSocket.TServerSocket(port=9090) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) server.serve() ``` 4. Create a client to interact with the server: ```python from example import MyService, ttypes transport = TSocket.TSocket("localhost", 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = MyService.Client(protocol) transport.open() struct = client.getStruct("123") print(struct.name) print(struct.age) transport.close() ``` This is just a basic example to give you an idea of how to use Thrift with Python. You can find more details and advanced usage in the Thrift documentation.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值