首先下载thrift.exe,和对应lib包。注意版本一定要一致。

否则编译会不识别出现错误。

可能会出现org.slf4j这个错误,那么你要把slf4j-api.jar下载下来引入到你的project中

namespace java com.nerd.thrift.service
/**
*
*/
service sayThriftService{
void say();
}

通过在命令行中转到 thrift-1.8.0.exe -gen java  sayThriftService

在磁盘目录中(com.nerd.thrift.service)可发现这个脚本对应的java代码

例如以下:

public class sayThriftService {

  /**
*
*/
public interface Iface { public void say() throws org.apache.thrift.TException; } public interface AsyncIface { public void say(org.apache.thrift.async.AsyncMethodCallback<AsyncClient.say_call> resultHandler) throws org.apache.thrift.TException; } public static class Client extends org.apache.thrift.TServiceClient implements Iface {
public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {
public Factory() {}
public Client getClient(org.apache.thrift.protocol.TProtocol prot) {
return new Client(prot);
}
public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
return new Client(iprot, oprot);
}
}
...................省略(详细看自己的生成代码)

先写一个Server类:

package com.nerd.clq;

import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TServerSocket; import com.nerd.clq.thrift.sayThriftService;
import com.nerd.clq.thrift.sayThriftService.Iface; public class Server implements sayThriftService.Iface{
private static TServer server;
@Override
public void say() throws TException {
System.out.println(System.currentTimeMillis());
} public static void main(String[] args) throws TException {
Server server1 = new Server();
TServerSocket serverTransport = new TServerSocket(8080);
TProtocolFactory proFactory = new TBinaryProtocol.Factory();
sayThriftService.Processor<Iface> processor = new sayThriftService.Processor<Iface>(server1);
Args arg = new Args(serverTransport) {
}.protocolFactory(proFactory).processor(processor);
server = new TThreadPoolServer(arg);
//启动服务(先启动这个类,然后启动client类)
server.serve();
}
}

client客户端类

package com.nerd.clq;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport; import com.nerd.clq.thrift.sayThriftService; public class Client {
public static void main(String[] args) throws TException {
TTransport transport = new TSocket("localhost", 8080);
TProtocol protocol = new TBinaryProtocol(transport);
sayThriftService.Client client = new sayThriftService.Client(protocol);
transport.open();
client.say();
transport.close();
} }

server编写的一般步骤:

1. 创建Handler

2. 基于Handler创建Processor

3. 创建Transport

4. 创建Protocol方式

5. 基于Processor, Transport和Protocol创建Server

6. 执行Server
client编写的一般步骤:

1. 创建Transport

2. 创建Protocol方式

3. 基于Transport和Protocol创建Client

4. 执行Client的方法
创建Transport的时候。一般都须要创建对应的Socket。

Thrift使用实例的更多相关文章

  1. python thrift使用实例

    前言 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Python开发人员角度简单介绍 Apache Thrift 的架构.开发和使 ...

  2. Apache Thrift 服务开发框架学习记录

    Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架. 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Servic ...

  3. Apache Thrift - 可伸缩的跨语言服务开发框架

    To put it simply, Apache Thrift is a binary communication protocol 原文地址:http://www.ibm.com/developer ...

  4. Apache Thrift学习之二(基础及原理)

    Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Java 开发人员角度详细介绍 Apache Thrift 的架构.开发和部署,并且 ...

  5. thrift概述

    Apache Thrift 是FaceBook实现的一种跨平台的远程服务调用(RPC)的框架.它采用接口描述语言(IDL)定义并创建服务,传输数据采用二进制格式,相对于XML和Json等常用数据传输方 ...

  6. Thrift 入门教程

    1. 概述 thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Go,Python, PHP, Ruby, Erl ...

  7. JaveWeb 公司项目(3)----- 通过Thrift端口获取数据库数据

    前面两篇博客的内容主要是界面搭建的过程,随着界面搭建工作的完成,网页端需要加入数据,原先的B/S架构中C#通过Thrift接口获取数据,所以在网页端也沿用这个设计 首先,新建一个Maven下的Web项 ...

  8. thrift框架总结,可伸缩的跨语言服务开发框架

    thrift框架总结,可伸缩的跨语言服务开发框架 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Service,基于 JSON 消息格式的 RESTful 服务等.其 ...

  9. Apache thrift - 使用,内部实现及构建一个可扩展的RPC框架

    本文首先介绍了什么是Apache Thrift,接着介绍了Thrift的安装部署及如何利用Thrift来实现一个简单的RPC应用,并简单的探究了一下Thrift的内部实现原理,最后给出一个基于Thri ...

随机推荐

  1. Python网络资源 + Python Manual

    如何学习Python + 如何有效利用Python有关的网络资源 + 如何利用Python自带手册(Python Manual) 都差点忘了说了,在看下面所有的内容之前,对于python版本不了解的, ...

  2. linux之SQL语句简明教程---INSERT INTO

    到目前为止,我们学到了将如何把资料由表格中取出.但是这些资料是如果进入这些表格的呢? 这就是这一页 (INSERT INTO) 和下一页 (UPDATE) 要讨论的. 基本上,我们有两种作法可以将资料 ...

  3. poj2301

    Beat the Spread! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17794   Accepted: 8484 ...

  4. C++关联容器综合应用:TextQuery小程序

    本文介绍C++关联容器综合应用:TextQuery小程序(源自C++ Primer). 关于关联容器的概念及介绍,请参考园子里这篇博文:http://www.cnblogs.com/cy568sear ...

  5. Kinect for windows 破解 一,简单的体感超级玛丽

    背景知识 1.  游戏模拟器:现在有很多模拟器,让我们可以在PC上玩红白机,PS上的游戏.本破解用的FC 红白机模拟器.网上有很多地方可以下载.注意语言要和你的操作系统一致. 2.  按键模拟器:本破 ...

  6. VMware Workstation虚拟机使用ISO映像文件

    VMware Workstation虚拟机使用ISO映像文件 VMware Workstation虚拟机使用ISO映像文件

  7. iOS 原生地图 开发

    iOS开发有时候用到地图,不少人第一想到的是用第三方.当然有时候为了和安卓同步,可能会一起使用某一第三方.但有时候,我们是可以用原生地图开发的.上面两个示意图是原生地图的自定义开发.运行demo,将展 ...

  8. 绿色mysql启动脚本

    启动脚本如下:./mysqld_safe --defaults-file=/export/servers/mysql-5.5.38/my.cnf --ledir=/export/servers/mys ...

  9. 《think in python》学习-8

    字符串 字符串是一个序列,可以用方括号操作符来访问字符串中的单独字符 fruit = 'banana' letter = fruit[1] 方括号中的表达式称为下标 下标从0 开始 任何表达式,包括变 ...

  10. dom处理配置文件_待完善

    import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java ...