首先下载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. C读txt到二维数组

    #include<stdio.h> #include<stdlib.h> #define maxn 200 void main() { FILE *fp; int s[maxn ...

  2. subllime text 创建可复用的代码片段

    对于前端工程师来讲,写一个html页面的基本结构是体力活,每次去拷贝一个也麻烦,sublime text 2 提供了一个很好的复用代码片段.下面介绍一下创建一个html5的代码片段的过程. 在菜单上点 ...

  3. hdu 1010 Tempter of the Bone(dfs暴力)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  4. 搭建PhoneGap for Android开发环境

    一.确保Android开发环境正常. 二.下载PhoneGap.可到http://www.phonegapcn.com/去下载,速度快些. 三.新建一个PhoneGap项目 1.在eclipse中新建 ...

  5. Android UI目录

    Android UI目录 序:最近一直想进阶android应用开发,虽然对一些相关的android知识都大体熟悉,但是自己的android知识体系,经不起推敲.经不起高手的垂问.经过几个月的努力学习, ...

  6. WAV文件格式分析

    一. RIFF概念 在Windows环境下,大部分的多媒体文件都依循着一种结构来存放信息,这样的结构称为"资源互换文件格式"(Resources lnterchange File ...

  7. Codeforces #245(div2)

    A:A. Points and Segments (easy) 题目看了n久,開始认为尼玛这是div2的题目么,题目还标明了easy.. 意思是给你一n个点,m个区间,在n个点上放蓝球或者红球,然后让 ...

  8. 【Trie】【HDU1247】【Hat’s Wordsfd2】

    题目大意: hat's word 的定义是字典中 恰好由另外两个单词连接起来的单词 给你一本字典,问有多少个hat's word,(字典按字典序给出) 单词数50000.. 初步思路: 单词分为前缀单 ...

  9. 关于ASP.NET中的负载均衡

    ASP.NET站点中做负载均衡: 基于HTTP协议我们可能发现我们要解决两点问题: 第一做到负载均衡,我们需要一个负载均衡器. 可以通过DNS轮询来做,在DNS服务器上配置为每次对我们做负载均衡的同一 ...

  10. Unity之GUI控件

    在这里就贴一个连接吧:GUI