歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Thrift使用實例

首先下載thrift.exe,和相應lib包,注意版本號一定要一致,否則編譯會不識別出現錯誤。

可能會出現org.slf4j這個錯誤,那麼你要把slf4j-api.jar下載下來引入到你的工程中

namespace java com.nerd.thrift.service
/**
 *
 */
service sayThriftService{
void say();
}

通過在命令行中轉到 thrift-1.8.0.exe -gen java  sayThriftService

在磁盤文件夾中(com.nerd.thrift.service)可發現這個腳本相應的java代碼

如下:

public class sayThriftService {

  /**
  *
  */
  public interface Iface {

    public void say() throws org.apache.thrift.TException;

  }

  public interface AsyncIface {

    public void say(org.apache.thrift.async.AsyncMethodCallback<AsyncClient.say_call> resultHandler) throws org.apache.thrift.TException;

  }

  public static class Client extends org.apache.thrift.TServiceClient implements Iface {
    public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {
      public Factory() {}
      public Client getClient(org.apache.thrift.protocol.TProtocol prot) {
        return new Client(prot);
      }
      public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
        return new Client(iprot, oprot);
      }
    }
    ...................省略(具體看自己的生成代碼)

--------------------------------------分割線 --------------------------------------

Golang通過Thrift框架完美實現跨語言調用 http://www.linuxidc.com/Linux/2013-09/90748.htm

Hadoop的Thrift server配置 http://www.linuxidc.com/Linux/2013-09/90641.htm

利用ruby通過Thrift接口批量插入HBase http://www.linuxidc.com/Linux/2013-04/82880.htm

在Linux(CentOS)上安裝Thrift+Scribe http://www.linuxidc.com/Linux/2012-12/76340.htm

使用Java快速入門Thrift http://www.linuxidc.com/Linux/2012-07/64933.htm

--------------------------------------分割線 --------------------------------------

先寫一個Server類:

package com.nerd.clq;

import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TServerSocket;

import com.nerd.clq.thrift.sayThriftService;
import com.nerd.clq.thrift.sayThriftService.Iface;

public class Server implements sayThriftService.Iface{
 private static TServer server;
 @Override
 public void say() throws TException {
  System.out.println(System.currentTimeMillis());
 }
 
 public static void main(String[] args) throws TException {
  Server server1 = new Server();
  TServerSocket serverTransport = new TServerSocket(8080);
                TProtocolFactory proFactory = new TBinaryProtocol.Factory();
                sayThriftService.Processor<Iface> processor = new sayThriftService.Processor<Iface>(server1);
                Args arg = new Args(serverTransport) {
  }.protocolFactory(proFactory).processor(processor);
  server = new TThreadPoolServer(arg);
  //啟動服務(先啟動這個類,然後啟動client類)
  server.serve();
 }
}

client客戶端類

package com.nerd.clq;


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 com.nerd.clq.thrift.sayThriftService;


public class Client {
 public static void main(String[] args) throws TException {
    TTransport transport = new TSocket("localhost", 8080);
    TProtocol protocol = new TBinaryProtocol(transport);
    sayThriftService.Client client = new sayThriftService.Client(protocol);
    transport.open();
    client.say();
    transport.close();
 }
 
}

 服務器編寫的一般步驟:
1. 創建Handler
2. 基於Handler創建Processor
3. 創建Transport
4. 創建Protocol方式
5. 基於Processor, Transport和Protocol創建Server
6. 運行Server

客戶端編寫的一般步驟:
1. 創建Transport
2. 創建Protocol方式
3. 基於Transport和Protocol創建Client
4. 運行Client的方法

創建Transport的時候,一般都需要創建相應的Socket。

Copyright © Linux教程網 All Rights Reserved