Thrift在windows7下的安装与实践
本文借鉴自
http://www.jianshu.com/p/0f4113d6ec4b
(下面称简书教程)
首先上官网下载代码
https://thrift.apache.org/download
下载源码thrift-0.9.3.tar.gz
解压之后放在路径C:\thrift-0.9.3\thrift-0.9.3
并下载windows执行版thrift-0.9.3.exe
放在路径C:\thrift-0.9.3下
下载apache ant项目,用于打jar包
下载路径
http://ant.apache.org/bindownload.cgi
解压之后放在路径C:\apache-ant-1.9.7-bin\apache-ant-1.9.7
配置环境变量
ANT_HOME : C:\apache-ant-1.9.7-bin\apache-ant-1.9.7
把C:\apache-ant-1.9.7-bin\apache-ant-1.9.7\bin\ant.bat复制到路径C:\thrift-0.9.3\thrift-0.9.3\lib\java下
在cmd中运行ant.bat,会生成jar包libthrift-0.9.3 在路径C:\thrift-0.9.3\thrift-0.9.3\lib\java\build
在路径C:\thrift-0.9.3中创建一个文本文件
复制代码
namespace java com.winwill.thriftenum RequestType { SAY_HELLO, //问好 QUERY_TIME, //询问时间}struct Request { 1: required RequestType type; // 请求的类型,必选 2: required string name; // 发起请求的人的名字,必选 3: optional i32 age; // 发起请求的人的年龄,可选 } exception RequestException { 1: required i32 code; 2: optional string reason;}// 服务名service HelloWordService { string doAction(1: Request request) throws (1:RequestException qe); // 可能抛出异常。}
保存为Test.thrift
在cmd中进入路径C:\thrift-0.9.3
执行命令thrift-0.9.3 -gen java Test.thrift
会在路径C:\thrift-0.9.3下生成一个文件夹gen-java
在Eclipse中创建工程TestThrift
按照简书教程生成package
com.winwill.thrift
把上面生成的gen-java中的代码复制到package中
并在package中创建代码,由于可能跟简书教程使用的版本不同,简书教程中的某些写法无法编译,
经过修改后使用如下代码
1.服务端
package com.winwill.thrift; import org.apache.commons.lang3.StringUtils;import org.apache.thrift.TException; import java.util.Date; public class HelloWordServiceImpl implements com.winwill.thrift.HelloWordService.Iface { // 实现这个方法完成具体的逻辑。 public String doAction(com.winwill.thrift.Request request) throws com.winwill.thrift.RequestException, TException { System.out.println("Get request: " + request); if (StringUtils.isBlank(request.getName()) || request.getType() == null) { throw new com.winwill.thrift.RequestException(); } String result = "Hello, " + request.getName(); if (request.getType() == com.winwill.thrift.RequestType.SAY_HELLO) { result += ", Welcome!"; } else { result += ", Now is " + new Date().toLocaleString(); } return result; }}
2.启动服务
package com.winwill.thrift; import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TCompactProtocol;import org.apache.thrift.protocol.TJSONProtocol;import org.apache.thrift.protocol.TProtocolFactory;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TSimpleServer;import org.apache.thrift.transport.TFastFramedTransport;import org.apache.thrift.transport.TFramedTransport;import org.apache.thrift.transport.TServerSocket;import org.apache.thrift.transport.TTransportFactory;import org.slf4j.*;import java.net.ServerSocket; public class HelloWordServer { public static void main(String[] args) throws Exception { int port = 7912; String transport_type = "buffered"; String protocol_type = "binary"; String server_type = "thread-pool"; String domain_socket = "";// ServerSocket socket = new ServerSocket(7912); // Protocol factory TProtocolFactory tProtocolFactory = null; if (protocol_type.equals("json")) { tProtocolFactory = new TJSONProtocol.Factory(); } else if (protocol_type.equals("compact")) { tProtocolFactory = new TCompactProtocol.Factory(); } else { tProtocolFactory = new TBinaryProtocol.Factory(); } TTransportFactory tTransportFactory = null; if (transport_type.equals("framed")) { tTransportFactory = new TFramedTransport.Factory(); } else if (transport_type.equals("fastframed")) { tTransportFactory = new TFastFramedTransport.Factory(); } else { // .equals("buffered") => default value tTransportFactory = new TTransportFactory(); } TServerSocket serverTransport = new TServerSocket(new TServerSocket.ServerSocketTransportArgs().port(port));; com.winwill.thrift.HelloWordService.Processor processor = new com.winwill.thrift.HelloWordService.Processor(new HelloWordServiceImpl()); TServer.Args tServerArgs = new TServer.Args(serverTransport); tServerArgs.processor(processor); tServerArgs.protocolFactory(tProtocolFactory); tServerArgs.transportFactory(tTransportFactory); TServer server = new TSimpleServer(tServerArgs); System.out.println("Running server..."); server.serve(); }}
3.客户端请求
package com.winwill.thrift; import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TProtocol;import org.apache.thrift.transport.TSocket;import org.apache.thrift.transport.TTransport; public class HelloWordClient { public static void main(String[] args) throws Exception { TTransport transport = new TSocket("【此处使用服务器ip地址】", 7912); TProtocol protocol = new TBinaryProtocol(transport); // 创建client com.winwill.thrift.HelloWordService.Client client = new com.winwill.thrift.HelloWordService.Client(protocol); transport.open(); // 建立连接 // 第一种请求类型 com.winwill.thrift.Request request = new com.winwill.thrift.Request() .setType(com.winwill.thrift.RequestType.SAY_HELLO).setName("winwill2012").setAge(24); System.out.println(client.doAction(request)); // 第二种请求类型 request.setType(com.winwill.thrift.RequestType.QUERY_TIME).setName("winwill2012"); System.out.println(client.doAction(request)); transport.close(); // 请求结束,断开连接 }}
在一台服务器上测试启动服务
输出Running server...
在另一台机器启动客户端
输出
Hello, winwill2012, Welcome!
Hello, winwill2012, Now is 2016-10-13 17:06:47
此时服务器端输出
Get request: Request(type:SAY_HELLO, name:winwill2012, age:24)
Get request: Request(type:QUERY_TIME, name:winwill2012, age:24)
说明已经成功连接啦
本文所用到的工具和工程已经打包上传到云盘,欢迎大家下载
链接:http://pan.baidu.com/s/1jHQXSma 密码:65uh
转载于:https://blog.51cto.com/gomic/1861584
Thrift在windows7下的安装与实践相关推荐
- Windows7下硬盘安装RHEL 6.1
Windows7下硬盘安装RHEL 6.1 1. 首先,大概需要一个10g的fat32格式分区的盘,笔者这里为e盘分了10g. 2. 下载rhel-server-6.1-i38 ...
- Windows7下无法安装Oracle11.1.0问题
问题现象 Windows7下安装Oracle11G(11.1.0.6.0),提示如下信息 正在检查操作系统要求... 要求的结果:5.0,5.1,5.2,6.0之一 实际结果:6.1 检查完成.此次检 ...
- VMware16在虚拟机Windows7下无法安装VMware tools问题vmware16(转自GakingChen)
1.基础信息 VMware Workstation 版本:16.1.1 build-17801498 虚拟机操作系统:Windows 7 2.问题1:无法安装VMware Tools 问题描述: ...
- u盘如何linux双系统,怎么用U盘在Windows7下再安装ubuntu形成双系统?
步骤: 1.进入到live界面后,显示以下界面,如图 2.点击桌面上InstallUbuntu11.04进入安装界面,选择安装语言,这里的语言仅仅指安装过程中的语言(支持中文安装),如图 3.点击Fo ...
- windows7下硬盘安装32位ubuntu12.04LTS
1. vmlinuz这个文件,在64位iso里叫vmlinuz.efi,使用时把后缀去掉. 2. 用EasyBCD配置引导文件时,注意64位Win 7采用的是GPT分区表,所以,C盘不是(hd0,0) ...
- windows7下emacs安装
操作系统:windows7 64bits emacs版本:emacs-24.5 下载Emacs:下载链接:http://ftp.gnu.org/pub/gnu/emacs/windows/,文件名:e ...
- 【原】DjianGo Windows7下的安装
安装DjianGo前必须安装Python环境,由于已经装过,这里不再贴出安装Python的步骤,我的Python版本是3.2. 1.下载django https://github.com/django ...
- Oracle9在Windows7下的安装
下载并解压oracle 9i,文件如下: 2.安装数据库 打开"92010NT_Disk1",双击运行"setup.exe",运行界面如下: 点击"下 ...
- Telnet在Windows7下的安装
Telnet是系统管理员常用的远程登录和管理工具,在Windows 2000/XP/2003/Vista系统中它作为标准的系统组件集成到系统中供用户使用,不过在Win7中,该功能默认是不安装的.要使用 ...
- Thrift在Windows及Linux平台下的安装和使用示例
thrift介绍 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的RPC(远程服务调用)框架. 本文主要目的是分别介绍在Windows及Linux平台下的Thri ...
最新文章
- js同时打开两个连接
- 深海中的STL—nth_element
- RecyclerView 可以与CollapsingToolbarLayout一起使用
- python下载word文件-python-docx操作word文件(*.docx)
- idea快速从dao层跳转到mapper.xml文件的插件
- c++11中静态断言static_assert
- Java 8实现BASE64编解码
- 吐血干货,直播首屏耗时400ms以下的优化实践
- Flask框架的学习与实战:实战小项目
- 时间片轮转调度算法详解
- Python UI自动化 编程(一) UIAutomation
- 数学模型之最小二乘法
- Shopnc之nginx配置
- python描述对象静态特性的数据为_外国法律中,对婚生子女的否认请求均规定有时效限制,日本法律规定的时效期限是 ( )_学小易找答案...
- 【opencv机器学习】基于SVM和神经网络的车牌识别
- 中国汽车用品行业需求态势及销售前景规模调研报告2021-2027年
- Linux编写内核模块,实现在/proc目录下添加文件
- 如何通过短视频打造自己的IP形象
- 解决win7 Windows USB无法驱动/驱动错误/该设备无法启动。(代码10)
- Microsoft Office Outlook——商业人士眼中的完美客户端