thrift0.5入门操作
在探索未知的程序之前,我们往往会使用“Hello World”这个经典的输出作为测试,为了遵循这个惯例,作为thrift菜鸟都不算的一员,决定跑一下“Hello world”正式进入菜鸟的行列。
thrift通过一个跨语言的定义文件的方式定义数据类型和服务接口,这个文件作为RPC客户端和服务器通信的标准,所谓跨语言,就是客户端和服务器端的语言是没有限制的,可以相同也可以不同,本质上定义的服务和接口都是一样的。
下面是我用intellij跑的java的例子:
环境配置:
1. ubuntu 13
2. thrift 0.5.0
3. Intellij 13
1.在Intellij中创建maven项目,在pom.xml中添加项目需要的依赖:
<dependencies>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.5.0-cdh</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
2.在resources目录下新建helloworld.thrift文件,内容如下:
namespace java com.qiao.thrift.hello
service IHelloWorldService {
string sayHello(1:string username)
}
3.在命令端打开helloworld.thrift文件所在的目录,执行下面的命令:
thrift -r --gen java hello_world.thrift
这时目录下会产生gen-java目录,目录下就是thrift自动生成的代码。
4. 使用cp -r命令将com/tom/thrift/hello/IHelloWorldService.java目录连同文件copy到Maven模块的src/java/main下面。
5.在Maven模块中,自定义IHelloWorldService的实现类HelloWorldService,需要实现IHelloWorldService.Iface接口,不出意料,果然仅仅需要实现sayHello方法,类定义如下:
public class HelloWorldService implements IHelloWorldService.Iface {
@Override
public String sayHello(String username) throws TException {
return "Hi,"+username+",Welcome to the thrift world";
}
}
6.自定义单线程模式的ThriftServer用于响应客户端的rpc请求,注意一点,IHelloWorldService.Iface的实现类在服务器端进行了实例化:
public class HelloWorldSimpleServer {
public static final int SERVER_PORT=8090;
public void startServer(){
try{
System.out.println("Hello World TSimpleServer start...");
TProcessor tp=new IHelloWorldService.Processor(new HelloWorldService());
TServerTransport sT=new TServerSocket(SERVER_PORT);
TServer server=new TSimpleServer(tp,sT);
System.out.println("Starting the server");
server.serve();
}catch (Exception e){
System.out.println("Server start error!!");
e.printStackTrace();
}
System.out.println("done.");
}
public static void main(String[] args){
HelloWorldSimpleServer server=new HelloWorldSimpleServer();
server.startServer();
}
}
7.自定义HelloWorld的客户端,用于远程调用第6步Thrift Server提供的IHelloWorldService服务
public class HelloWorldSimpleClient {
public void startClient(String userName) {
TTransport transport = null;
try {
transport = new TSocket("localhost", 8090);
TProtocol protocol = new TBinaryProtocol(transport);
IHelloWorldService.Client client = new IHelloWorldService.Client(protocol);
transport.open();
String res = client.sayHello(userName);
System.out.println("Thrift result =:" + res);
} catch (Exception x) {
System.out.println("Start client Error!");
x.printStackTrace();
} finally {
if (transport != null) {
transport.close();
}
}
}
public static void main(String[] args){
HelloWorldSimpleClient client=new HelloWorldSimpleClient();
client.startClient("qiao");
}
}
8.启动HelloWorldSimpleServer,控制台输出HelloWorld TSimpleServer start ....,同时,HelloWorldSimpleServer作为Server一直处于运行过程中;启动HelloWorldSimpleClient,控制台输出Thrift client result =: Hi, Tom, Welcome to the Thrift world, enjoy it! 执行完后,客户端进程结束。
另外在运行过程中出现下面的警告:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
原因是org.slf4j.impl.StaticLoggerBinder 无法载入到内存,原因是没有找到合适的绑定SLF4J,需要添加所列举的包中的某一个。解决方案是手动下载一个slf4j-nop.jar,然后添加到路径中就可以了。
另外比较全的代码例子为:https://github.com/mariusae/thrift-0.5.0-finagle.git
thrift0.5入门操作的更多相关文章
- spring boot 入门操作(二)
spring boot入门操作 使用FastJson解析json数据 pom dependencies里添加fastjson依赖 <dependency> <groupId>c ...
- spring boot 入门操作(三)
spring boot入门操作 devtools热部署 pom dependencies里添加依赖 <dependency> <groupId>org.springframew ...
- Mysql的二进制安装和基础入门操作
前言:Mysql数据库,知识非常的多,要想学精学通这块知识,估计也要花费和学linux一样的精力和时间.小编也是只会些毛皮,给大家分享一下~ 一.MySQL安装 (1)安装方式: 1 .程序包yum安 ...
- java之servlet入门操作教程一续
本节主要是在java之servlet入门操作教程一 的基础上使用myeclipse实现自动部署的功能 准备: java之servlet入门操作教程一 中完成myFirstServlet项目的创建: ...
- Mysql数据库的二进制安装和基础入门操作
前言:Mysql数据库,知识非常的多,要想学精学通这块知识,估计也要花费和学linux一样的精力和时间.小编也是只会些毛皮,给大家分享一下~ 一.MySQL安装 (1)安装方式: 1 .程序包yum安 ...
- Mycat 中间件配置初探与入门操作
Mycat中间件配置初探与入门操作 By:授客 QQ:1033553122 实践环境 Mycat-server-1.5.1-RELEASE-20161130213509-win.tar.gz 下载地址 ...
- EF+LINQ事物处理 C# 使用NLog记录日志入门操作 ASP.NET MVC多语言 仿微软网站效果(转) 详解C#特性和反射(一) c# API接受图片文件以Base64格式上传图片 .NET读取json数据并绑定到对象
EF+LINQ事物处理 在使用EF的情况下,怎么进行事务的处理,来减少数据操作时的失误,比如重复插入数据等等这些问题,这都是经常会遇到的一些问题 但是如果是我有多个站点,然后存在同类型的角色去操作 ...
- 01.JDBC操作数据库-快速入门操作
/** * 简单入门操作 * 注:先将mysql-connector-java-5.1.36.jar 构建 Build Path环境当中去 * @param args * @throws Except ...
- (转)私有代码存放仓库 BitBucket介绍及入门操作
转自:http://blog.csdn.net/lhb_0531/article/details/8602139 私有代码存放仓库 BitBucket介绍及入门操作 分类: 研发管理2013-02-2 ...
随机推荐
- Day10 MVC
经典三层 表述层(表示层): 前台交互,调用后台 web 业务逻辑层: 处理业务 service 数据持久层: 与数据库之间进行交互 dao 面向对象原则 面 ...
- PhotoSwipe-一个好用的图片放大缩小插件
通过GitHub 下载PhotoSwipe https://github.com/dimsemenov/PhotoSwipe 相关的库 <link rel="stylesheet&qu ...
- Windows下docker的安装,将ASP.NET Core程序部署在Linux和Docker中
参考文章: https://www.cnblogs.com/jRoger/p/aspnet-core-deploy-to-docker.html docker for windows下载连接: htt ...
- JAVA开发微信支付-公众号支付/微信浏览器支付(JSAPI)
写这篇文章的目的有2个,一是自己的项目刚开发完微信支付功能,趁热回个炉温习一下,二也是帮助像我这样对微信支付不熟悉,反复看了多天文档还是一知半解,原理都没摸清,更不要说实现了.本以为网上的微信开发教程 ...
- ZOJ 3212 K-Nice(满足某个要求的矩阵构造)
H - K-Nice Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Sta ...
- linux学习第十九天(iscsi配置)
一.iSCSI 服务部署网络存储 服务器配置 添加硬盘,创建分区 l[root@localhost Desktop]# ls /dev/sd* (系统下查看硬盘信息) /dev/sda /dev/ ...
- 微信公众号发送消息模板(java)
这段时间接触公众号开发,写下向用户发送消息模板的接口调用 先上接口代码 public static JSONObject sendModelMessage(ServletContext context ...
- SVG 动画(animate、animateTransform、animateMotion)
原文:https://blog.csdn.net/chy555chy/article/details/53535581 参考 MDN开发文档 https://developer.mozilla.org ...
- [JLOI2013]地形生成[组合计数]
题意 \(n\) 元素各有一个高度 \(h\) 和关键数字 \(b\) .求有多少个下标序列和高度序列,满足对任意 \(i\),\(j< i\) 且 \(h_j < h_i\)的 \(j\ ...
- angularjs 常用方法
一 angular的copy和extend 1.angular.extend() angular.extend():依次将第二个参数及后续的参数的第一层属性(不管是简单的属性还是对象)拷贝,赋给第一个 ...