首先下载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. linux分区,文件系统,目录结构概述

    1.Linux中如何表示硬盘,分区 Linux内核读取光驱,硬盘等资源时均通过“设备文件”的形式进行,因此在linux系统中,将硬 盘和分区表示为不同的文件.具体表述形式如下: 硬盘:对于IDE接口的 ...

  2. windows2003 64位注册码 序列号 激活码

    Windows 2003 R2 64bit Enterprise VOL Edition 企业版 MR78C-GF2CY-KC864-DTG74-VMT73 VPT7T-77D38-KWVW2-2G3 ...

  3. 开源 iOS 项目分类索引大全

    GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Category类型的库,附带很多自定义 ...

  4. C#获取URL参数值(NameValueCollection)

    在写程序的时候,我们经常需要对页面进行传参数,比如page?id=1234,那么在page这个页面中就直接可以使用string id = Request.QueryString["id&qu ...

  5. .net通用权限框架B/S(一)

    一直做软件实施,用过一些二次开发平台,最近看了一些大神写的框架,于是参考写了一个B/S通用权限框架,项目使用MVC4+EF5+EASYUI(.net framework4),开发环境vs2010+sq ...

  6. hdu2243考研路茫茫——单词情结

    Problem Description 背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如&q ...

  7. 自写Date工具类

    以前写项目的时候总是在使用到了时间的转换的时候才在工具类中添加一个方法,这样很容易导致代码冗余以及转换的方法注释不清晰导致每次使用都要重新看一遍工具类.因此整理出经常使用的一些转换,用作记录,以便以后 ...

  8. 【noip2012提高组】国王游戏

    恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 成一排,国王站在队伍的最前面. ...

  9. 解决Button在IE6、7下的自适应宽度问题

    很早就遇到过这么个小问题,但由于其并未影响到实际作用和美观就没有正面解决它,现在,我们来试着解决它. 写一个Button,有两种方式:其一,直接button标签:其二,input type=”butt ...

  10. Android 混淆文件project.properties和proguard-project.txt

    参考文档:http://blog.csdn.net/xueyepiaoling/article/details/8202359 http://glblong.blog.51cto.com/305861 ...