首先下载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. 函数模板的载体-HPP

    在C++中,我们通常将声明放在.h头文件中,将具体的实现代码放在.cpp文件中.但是函数模板通常不这么做,函数模板是将其声明和实现都放在.hpp文件中.hpp是Header Plus Plus的缩写, ...

  2. DayOfWeek中英文星期转换

    DateTime.Now.DayOfWeek;   //英文星期几 var dayOfWeek = new List<string>() { "星期日", " ...

  3. 手机端页面自适应:rem布局

    rem布局非常简单,首页你只需在页面引入这段原生js代码就可以了 (function (doc, win) { var docEl = doc.documentElement, resizeEvt = ...

  4. UVA 1605 Building for UN

    题意: 有n个国家,要求你设计一栋楼并为这n个国家划分房间,要求国家的房间必须连通,且每两个国家之间必须有一间房间是相邻的 分析: 其实非常简单,完全被样例误导了.只需要设计两层就可以了,每个国家占第 ...

  5. 【笔记】让DIV水平垂直居中的两种方法

    今天写的了百度前端学院春季班的任务:定位和居中问题  由于距离上次学习CSS有点久远了,加上以前木有记笔记的习惯,方法忘得只剩下一种,今天通过网上查阅资料总结了以下两种简单的方法让DIV水平垂直居中. ...

  6. There is no satiety in study

    好不容易考上了硕士.这个时候,才终于明白什么叫做学无止境.用了1周linux,发现需要学习的东西太多了.life is too short to learn c plus plus 果然如此.不过我们 ...

  7. contact表单错误解决记录

    在上篇表单验证中,过程中可谓坎坷,记录一下错误问题及解决方案. 我们用到的模板contact_form.html如下,其他urls.py自行去修改. <html> <head> ...

  8. View的事件分发机制

    一.点击事件的传递规则 传递事件的主要方法: public boolean dispatchTouchEvent(MotionEvent ev) 如果事件能够传递到当前View,则该方法一定会被调用. ...

  9. Oracle EBS-SQL (BOM-10):检查有BOM无计划员的数据.sql

    select DISTINCT     msi.segment1 编码    ,msi.description 描述    ,msi.item_type 物料类型    ,msi.inventory_ ...

  10. pragma comment的使用

    该宏放置一个注释到对象文件或者可执行文件. #pragma comment( comment-type [,"commentstring"] ) comment-type是一个预定 ...