CentOS安装thrift
1.安装jdk
2.安装ant
3.安装 ivy
a、下载apache ivy:
http://labs.renren.com/apache-mirror//ant/ivy/2.2.0/apache-ivy-2.2.0-bin.tar.gz b、tar xzvf apache-ivy-2.2.0-bin.tar.gz c、cp ivy-2.2.0.jar to ANT_HOME/lib
d、goto apache-ivy-2.2.0/src/example/hello-ivy, and run ant,也就是在目录apache-ivy-2.2.0/src/example/hello-ivy,运行ant
如果看到:
BUILD SUCCESSFUL
Total time: 29 seconds
就代表成了
在centos里再执行这一句应没问题:sudo yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel
哈哈
4. 安装thrift
a>下载thrift:http://mirror.bjtu.edu.cn/apache//thrift/0.8.0/thrift-0.8.0.tar.gz
b>tar -zxvf thrift-0.8.0.tar.gz
c>cd thrift-0.8.0
d> ./configure --with-boost=/usr/local
e>make
f>make install
g>在命今行内输入 thrift,如果有提示,应就可以了
如果遇到"Error: libcrypto required" 错误,请安装libssl-dev
5.构建thrift java 服务器端和客户端
a>新建testJava.thrift文件:
namespace java Test service Something{ i32 ping()
}
b>testJava.thrift目录执行如下命令:thrift –gen java testJava.thrift ,生成的源文件在./gen-java/目录下
c>进入gen-java目录
d>编写SomethingImpl.java
packageTest; importorg.apache.thrift.TException; classSomethingImpl implements Something.Iface { publicSomethingImpl() { } publicintping() throws TException { System.out.println("Recieve ping from client..."); return0; }
}
e> Server.java
packageTest; importjava.io.IOException; importorg.apache.thrift.protocol.TBinaryProtocol; importorg.apache.thrift.protocol.TBinaryProtocol.Factory; importorg.apache.thrift.server.TServer; importorg.apache.thrift.server.TSimpleServer; importorg.apache.thrift.server.TThreadPoolServer; importorg.apache.thrift.transport.TServerSocket; importorg.apache.thrift.transport.TTransportException; publicclassServer { privatevoidstart() { try{ TServerSocket serverTransport = new TServerSocket(7911); Something.Processor processor = new Something.Processor(new SomethingImpl()); Factory protFactory = new TBinaryProtocol.Factory(true, true); //TServer server = new TThreadPoolServer(processor, serverTransport,protFactory); //TServer server = new TSimpleServer(new Args(serverTransport).processor(processor)); TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor)); System.out.println("Starting server on port 7911 ..."); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } publicstaticvoidmain(String args[]) { Server srv = new Server(); srv.start(); }
}
f> Client.java
packageTest; importjava.io.IOException; importorg.apache.thrift.*; importorg.apache.thrift.protocol.*; importorg.apache.thrift.transport.*; publicclassClient { publicstaticvoidmain(String [] args) { try{ TTransport transport = new TSocket("localhost", 7911); TProtocol protocol = new TBinaryProtocol(transport); Something.Client client = new Something.Client(protocol); transport.open(); System.out.println("Client calls ping()"); client.ping(); transport.close(); } catch (TException x) { x.printStackTrace(); } }
}
g>要求如下几个包支持libthrift-0.8.0.jar log4j-1.2.14.jar slf4j-api-1.5.8.jar slf4j-log4j12-1.5.8.jar,所以可以如下命令编译:
javac -classpath ./:../lib/libthrift-0.8.0.jar:../lib/log4j-1.2.14.jar:../lib/slf4j-api-1.5.8.jar:../lib/slf4j-log4j12-1.5.8.jar *.java
h> 启动服务器。退到gen-java目录,输入java -classpath ./:../lib/libthrift-0.8.0.jar:../lib/log4j-1.2.14.jar:../lib/slf4j-api-1.5.8.jar:../lib/slf4j-log4j12-1.5.8.jar Test/Server,屏幕显示如下:
Starting server on port 7911 ...
i> 启动客户端。在同一目录下输入java -classpath ./:../lib/libthrift-0.8.0.jar:../lib/log4j-1.2.14.jar:../lib/slf4j-api-1.5.8.jar:../lib/slf4j-log4j12-1.5.8.jar Test/Client,屏幕显示如下:
Client calls ping()
这时服务器端的输出多了一行:
Recieve ping from client...
成功了!!!!!!!!!!!!!!!!!!!!!!!!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。