前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Thrift使用实例

Thrift使用实例

作者头像
用户5166556
发布2019-04-16 12:39:49
7270
发布2019-04-16 12:39:49
举报

首先下载thrift.exe,和相应lib包,注意版本号一定要一致, 否则编译会不识别出现错误。 可能会出现org.slf4j这个错误,那么你要把slf4j-api.jar下载下来引入到你的工程中

代码语言:javascript
复制
namespace java com.nerd.thrift.service
/**
 *
 */
service sayThriftService{
void say();
}

通过在命令行中转到 thrift-1.8.0.exe -gen java  sayThriftService 在磁盘文件夹中(com.nerd.thrift.service)可发现这个脚本相应的java代码 如下:

代码语言:javascript
复制
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);
      }
    }
    ...................省略(具体看自己的生成代码)

 先写一个Server类:

代码语言:javascript
复制
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客户端类

代码语言:javascript
复制
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。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014年07月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档