在Windows系统下安装Thrift的方法与使用讲解
2019-04-17 18:02
681 查看
安装
下载
下载地址:http://archive.apache.org/dist/thrift/0.10.0/
将thrift-0.10.0.exe放到一个文件下,如F:\thrift下,将其重命名为thrift.exe。如果不重命名,需要使用thrift-0.10.0调用thrift命令。
配置环境变量
向Path
中添加变量值,值为thrift.exe
的地址,如F:\thrift。
测试
命令行输入thrift -version
,如果输出thrift的版本即表明安装成功。
使用
编写IDL接口
HelloService.thrift
namespace java com.thrift.demo.service service HelloService{ string sayHello(1:string username) }
编译
编译之后会生成类HelloService
。
thrift -gen java HelloService.thrift
编写实现类
HelloServiceImpl.java
public class HelloServiceImpl implements HelloService.Iface { @Override public String sayHello(String username) throws TException { return "Hello Thrift Service : " + username; } }
编写服务端代码
public class HelloServer { public static final int SERVER_PORT = 8090; public void startServer() { try { System.out.println("HelloService TSimpleServer start ...."); TProcessor tprocessor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl()); // 简单的单线程服务模型,一般用于测试 TServerSocket serverTransport = new TServerSocket(SERVER_PORT); TServer.Args tArgs = new TServer.Args(serverTransport); tArgs.processor(tprocessor); tArgs.protocolFactory(new TBinaryProtocol.Factory()); TServer server = new TSimpleServer(tArgs); server.serve(); } catch (Exception e) { System.out.println("Server start error!!!"); e.printStackTrace(); } } public static void main(String[] args) { HelloServer server = new HelloServer(); server.startServer(); } }
编写客户端代码
public class HelloClient { public static final String SERVER_IP = "localhost"; public static final int SERVER_PORT = 8090; public static final int TIMEOUT = 30000; public void startClient(String userName) { TTransport transport = null; try { transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT); // 协议要和服务端一致 TProtocol protocol = new TBinaryProtocol(transport); HelloService.Client client = new HelloService.Client(protocol); transport.open(); String result = client.sayHello(userName); System.out.println("Thrify client result =: " + result); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } finally { if (null != transport) { transport.close(); } } } public static void main(String[] args) { HelloClient client = new HelloClient(); client.startClient("Michael"); } }
运行
先运行服务端,再运行客户端。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接
您可能感兴趣的文章:
相关文章推荐
- 二进制程序分析工具Pin在Windows系统中的安装和使用方法
- windows系统电脑安装使用cisco anyconnect的最新方法
- 二进制程序分析工具Pin在Windows系统中的安装和使用方法
- WINDOWS 10 下使用U盘安装Ubuntu kylin 双系统的方法,欢迎交流讨论
- 万能的系统安装方法 使用Windows通用安装器
- Win10下python3和python2同时安装并解决pip共存问题 特别说明,本文是在Windows64位系统下进行的,32位系统请下载相应版本的安装包,安装方法类似。 使用python开
- windows 2000系统下安装PPPOE协议,使用adsl拔号上网
- 小实验: 使用KVM虚拟机,安装一个windows系统
- windows系统上安装与使用Android NDK
- 更换默认启动系统的方法,适合于安装了windows 和 linux双系统的用户(转载)
- Android NDK r5 windows系统上安装与使用
- Windows 7与Fedora双系统安装方法
- Android NDK r5 windows系统上安装与使用【1】
- [精华] 不使用软盘加载驱动安装系统的方法--使用nLite集成驱动
- windows系统中安装perl模块(package)的几种方法
- Windows下免安装Oracle客户端就能使用pl/sql developer的方法
- 如何使用U盘安装操作系统,非PE安装系统的方法
- windows系统上安装与使用Android NDK
- Ubuntu 11.10与Windows双系统的硬盘安装方法
- Cassandra在Windows上安装及使用方法[转]