我用的thrift版本是thrift-0.9.1.tar.gz,在虚拟机上解压并安装后,将java接口类文件复制到windows10中myeclipse10的工程src包目录下,发现有报错的,
报错内容如下,我用下划线标记出来了,即找不到getScheme()方法,也找不到testBit()方法。我进入到源码查看,发现这两个方法都有啊,为什么点不出来呢?
public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
schemes.get(oprot.getScheme()).getScheme().write(oprot, this);
}
public boolean isSetSuccess() {
return EncodingUtils.testBit(__isset_bitfield, __SUCCESS_ISSET_ID);
}
原因:我建了一个java项目,里面全是大数据相关(例如hbase,hive,kafka当然也包括thrift),所以各自用到的jar包都放在该项目的lib目录下,所以有时会导致jar包冲突,比如
hive的jar包 hive-exec-0.9.0.jar里面也有thrift相关类,而且包级结构一模一样,他虽然有org.apache.thrift.protocol.TProtocol这个类,但是并没有getScheme()方法。所以它 和libthrift-0.9.1冲突了
解决方法:1.去掉hive-exec-0.9.0.jar(不建议) 2.另建一个java项目,导入thrift相关jar包,然后再导入接口类文件。至此就OK啦
下面贴上安装过程和我的代码
安装过程:
1.使用yum安装准备环境:#yum -y install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel openssl openssl-devel
2.安装ant和ivy环境,解压缩apache-ant-1.8.2-bin.tar.gz并重命名为ant
#tar -zxvf apache-ant-1.8.2-bin.tar.gz
#mv apache-ant-1.8.2 ant
3.配置环境变量,通过vi编辑器编辑:#vi /etc/profile ,打开vi编辑器后,按下i键进入插入编辑模式,将以下内容编辑到文本末尾:ANT_HOME=/home/lvyuan/ant
PATH=PATH:$ANT_HOME/bin ,按esc键进入命令行模式,输入 :wq(保存并退出),是环境变量立即生效 # source /etc/profile
4.解压缩apache-ivy-2.2.0-bin.tar.gz并重命名为ivy
#tar -zxvf apache-ivy-2.2.0-bin.tar.gz
#mv apache-ivy-2.2.0 ivy
5.拷贝ivy下的核心jar到ant的lib目录中:#cp /home/lvyuan/ivy/ivy-2.2.0.jar /home/lvyuan/ant/lib/
6.安装thrift,解压缩thrift-0.9.1.tar.gz并重命名为thrift
#tar -zxvf thrift-0.9.1.tar.gz
#mv thrift-0.9.1 thrift
7.进入thrift目录,进行编译和安装
#./configure --with-boost=/usr/local
#make
#make install
8.编写thrift的idl接口文件,并生成java接口:在thrift根目录下#vi rtserver.thrift
namespace java org.thrift.idl
struct CommonRet{
1:string operationCode;
2:string mobile;
3:string mode;
4:string status;
5:string time
}
struct MsgObject{
1:string operationCode;
2:string mobile;
3:string mode;
4:string type;
5:string time;
6:map
service IProtocalService{
bool commonAnswer(1:CommonRet commonRet);
bool upData(1:MsgObject msgObject)
}
10.搭建开发环境:新建一个java工程,将thri目录下/lib/java/build子目录下的jar和/lib/java/build/lib下的jar导入环境变量。将生成的gen-java下的类拷贝到对应的src下的包中
11.接口类代码我就不贴了(接口类代码即gen-java目录下的java文件)
import org.apache.thrift.TException;
import org.thrift.idl.CommonRet;
import org.thrift.idl.IProtocalService;
import org.thrift.idl.MsgObject;
/**
* 实现服务端,第一步:首先定义一个类,实现生成的接口
* @author 远
*
*/
public class ProtocalService implements IProtocalService.Iface{
@Override
public boolean commonAnswer(CommonRet commonRet) throws TException {
System.out.println("Todo commonAnswer");
return false;
}
@Override
public boolean upData(MsgObject msgObject) throws TException {
System.out.println("Todo upData");
return false;
}
}
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TServerSocket;
import org.thrift.idl.IProtocalService;
import org.thrift.idl.IProtocalService.Iface;
/**
* 第二步:实现同步服务端
* @author 远
*
*/
public class SyncServer {
private void start(){
try {
TServerSocket serverTransport=new TServerSocket(8080);
IProtocalService.Processor
new ProtocalService());
TThreadPoolServer.Args args=new TThreadPoolServer.Args(serverTransport);
//高效率的、密集的二进制编码格式进行数据传输
args.protocolFactory(new TCompactProtocol.Factory());
args.transportFactory(new TFramedTransport.Factory());
args.processorFactory(new TProcessorFactory(processor));
args.maxWorkerThreads(10000);
args.minWorkerThreads(50);
TThreadPoolServer server=new TThreadPoolServer(args);
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SyncServer srv=new SyncServer();
srv.start();
}
}
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransportException;
import org.thrift.idl.CommonRet;
import org.thrift.idl.IProtocalService;
/**
* 实现客户端
* @author 远
*
*/
public class SyncClient {
private IProtocalService.Client client;
public void init() throws TTransportException{
TFramedTransport transport=new TFramedTransport(new TSocket("localhost", 8080));
TCompactProtocol protocol=new TCompactProtocol(transport);
client=new IProtocalService.Client(protocol);
transport.open();
}
public IProtocalService.Client getClient() {
return client;
}
public void setClient(IProtocalService.Client client) {
this.client = client;
}
public static void main(String[] args) {
SyncClient cli=new SyncClient();
try {
cli.init();
CommonRet commonRet=new CommonRet();
boolean b=cli.getClient().commonAnswer(commonRet);
System.out.println(b);
} catch (Exception e) {
e.printStackTrace();
}
}
}