thrift学习笔记
Thrift学习笔记
一:thrift介绍

二 thrift数据传输协议
三 thrift传输层
四 thrift服务器端
五 thrift客户端
六 thrift开发步骤
1服务器端
2客户端
七 thrift数据类型
1基本类型
bool:布尔值,true 或 false
byte:8 位有符号整数
i16:16 位有符号整数
i32:32 位有符号整数
i64:64 位有符号整数
double:64 位浮点数
string:utf-8编码的字符串
2结构体类型
struct:定义公共的对象
enum: 枚举类型
3容器类型
list:对应 Java 的 ArrayList
set:对应 Java 的 HashSet
map:对应 Java 的 HashMap
4异常类型
exception:对应 Java 的 Exception
5服务类型
service:对应服务的类 提供接口
八 thrift例子
enum 类型
struct Student{
1: required i32 id
2: required string username
3: required string password
4: requried string number
5: optional double age
}
struct 类型
struct School{
1: required i32 id
2: required string name
3: required set<Student> students
4: required list<Student> rank
5: required map<string, string> number_name
}
service 类型
service ThriftMysqlService{
void addUser(1:Student user)
list<Student> queryAllUser()
Student queryOneUser(1:i32 id)
map<string, string> queryOneArticle(1:i32 id)
}
具体代码
thrift.thrift 定义数据类型和接口的文件
namespace java org.seava.thrift_example.thrift
struct User{
1: required i32 userId
2: required string username
3: required string password
}
service ThriftService{
void addUser(1:User user)
User queryUser(1:i32 id)
list<User> queryUserList()
map<string, string> queryUserNamePass()
map<i32, User> queryUserMap()
}
到apache的官网下载thrift.exe程序, 下载地址 http://thrift.apache.org/ ,下下来通过cmd命令窗口去运行如下命令
thrift -gen java xxx.thrift
接口实现 ThriftServiceImpl.java
package org.seava.thrift_example.thrift; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class ThriftServiceImpl implements ThriftService.Iface { public void addUser(User user) throws org.apache.thrift.TException{
System.out.println(user.userId + " " + user.username + " " + user.password);
} public User queryUser(int id) throws org.apache.thrift.TException{
System.out.println(id);
User user = new User();
user.userId = 100;
user.username = "FFF";
user.password = "NNN";
return user;
} public List<User> queryUserList() throws org.apache.thrift.TException{
User user = new User();
user.userId = 100;
user.username = "FFF";
user.password = "NNN";
User user2 = new User();
user2.userId = 102;
user2.username = "FFF2";
user2.password = "NNN2";
List<User> list = new ArrayList<User>();
list.add(user2);
list.add(user);
return list;
} public Map<String,String> queryUserNamePass() throws org.apache.thrift.TException{
User user = new User();
user.userId = 100;
user.username = "FFF";
user.password = "NNN";
Map<String, String> map = new HashMap<String, String>();
map.put("password", user.password);
map.put("useranme", user.username);
return map;
} public Map<Integer,User> queryUserMap() throws org.apache.thrift.TException{
User user = new User();
user.userId = 100;
user.username = "FFF";
user.password = "NNN";
User user2 = new User();
user2.userId = 102;
user2.username = "FFF2";
user2.password = "NNN2";
Map<Integer, User> map = new HashMap<Integer, User>();
map.put(user.userId, user);
map.put(user2.userId, user2);
return map;
} }
服务器 Server.java
package org.seava.thrift_example.thrift; import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException; public class Server { public static int port = 8090; /**
* 简单服务器类型 阻塞单线程
* 步骤
* 创建TProcessor
* 创建TServerTransport
* 创建TProtocol
* 创建TServer
* 启动Server
*/
public static void startSimpleServer(){
//创建processor
TProcessor tprocessor = new ThriftService.Processor<ThriftService.Iface>(new ThriftServiceImpl());
try {
//创建transport 阻塞通信
TServerSocket serverTransport = new TServerSocket(port);
//创建protocol
TBinaryProtocol.Factory protocol = new TBinaryProtocol.Factory();
//将processor transport protocol设入到服务器server中
TServer.Args args = new TServer.Args(serverTransport);
args.processor(tprocessor);
args.protocolFactory(protocol);
//定义服务器类型 设定参数
TServer server = new TSimpleServer(args);
//开启服务
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
} /**
* 多线程服务器 阻塞多线程
*/
public static void startThreadPoolServer(){
//创建processor
TProcessor tprocessor = new ThriftService.Processor<ThriftService.Iface>(new ThriftServiceImpl());
try{
//创建transport 阻塞通信
TServerSocket serverTransport = new TServerSocket(port);
//创建protocol 数据传输协议
TBinaryProtocol.Factory protocol = new TBinaryProtocol.Factory();
TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport);
args.processor(tprocessor);
args.protocolFactory(protocol);
//创建服务器类型 多线程
TServer server = new TThreadPoolServer(args);
//开启服务
server.serve();
}catch(Exception e){
e.printStackTrace();
}
} /**
* 非阻塞I/O
*/
public static void startTNonblockingServer(){
//创建processor
TProcessor tprocessor = new ThriftService.Processor<ThriftService.Iface>(new ThriftServiceImpl());
try{
//创建transport 非阻塞 nonblocking
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(port);
//创建protocol 数据传输协议
TCompactProtocol.Factory protocol = new TCompactProtocol.Factory();
//创建transport 数据传输方式 非阻塞需要用这种方式传输
TFramedTransport.Factory transport = new TFramedTransport.Factory();
TNonblockingServer.Args args = new TNonblockingServer.Args(serverTransport);
args.processor(tprocessor);
args.transportFactory(transport);
args.protocolFactory(protocol);
//创建服务器 类型是非阻塞
TServer server = new TNonblockingServer(args);
//开启服务
server.serve();
}catch(Exception e){
e.printStackTrace();
}
} /**
* 半同步半异步的非阻塞I/O
*/
public static void startTHsHaServer(){
//创建processor
TProcessor tprocessor = new ThriftService.Processor<ThriftService.Iface>(new ThriftServiceImpl());
try{
//创建transport 非阻塞
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(port);
//非阻塞需要的传输方式
TFramedTransport.Factory transport = new TFramedTransport.Factory();
//数据传输协议
TCompactProtocol.Factory protocol = new TCompactProtocol.Factory();
//创建半同步半异步服务
THsHaServer.Args args = new THsHaServer.Args(serverTransport);
args.processor(tprocessor);
args.transportFactory(transport);
args.protocolFactory(protocol);
//创建 服务类型
TServer server = new THsHaServer(args);
//开启服务
server.serve();
}catch(Exception e){
e.printStackTrace();
}
} public static void main(String args[]){
//开启简单服务器
// Server.startSimpleServer();
//开启多线程服务器
// Server.startThreadPoolServer();
// Server.startTNonblockingServer();
// Server.startTHsHaServer();
Server.startTNonblockingServer();
}
}
Server.java实现了简单服务器(阻塞单线程) 阻塞多线程 非阻塞 半同步半异步非阻塞
注意: 非阻塞时传输层需要选择TFramedTransport
客户端 Client.java
package org.seava.thrift_example.thrift; import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import org.apache.thrift.async.TAsyncClientManager;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TNonblockingTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport; public class Client implements Runnable { public static String ip = "localhost";
public static int port = 8090;
public static int time_out = 30000; /**
* 客户端设置
* 创建Transport
* 创建TProtocol
* 基于TTransport和TProtocol创建Client
* 调用Client的相应方法
*/
public static void startSimpleClient(){
TTransport transport = null;
try{
//创建Transport
transport = new TSocket(ip, port, time_out);
//创建TProtocol
TProtocol protocol = new TBinaryProtocol(transport);
//基于TTransport和TProtocol创建Client
ThriftService.Client client = new ThriftService.Client(protocol);
transport.open();
//调用client方法
List<User> list = client.queryUserList();
for(User user : list){
System.out.println(user.userId + " " + user.username + " " + user.password);
}
Map<String, String> map = client.queryUserNamePass();
System.out.println(map);
User user = client.queryUser(10);
System.out.println(user.userId + " " + user.username + " " + user.password);
Map<Integer, User> map_u = client.queryUserMap();
System.out.println(map_u);
User uu = new User();
uu.userId = 1111;
uu.username = "mmbbmmbb";
uu.password = "ppbbppbb";
client.addUser(uu);
}catch(Exception e){
e.printStackTrace();
}
} /**
* 调用阻塞服务器的客户端
*/
public static void startNonblockingClient(){
TTransport transport = null;
try{
transport = new TFramedTransport(new TSocket(ip, port));
TCompactProtocol protocol = new TCompactProtocol(transport);
ThriftService.Client client = new ThriftService.Client(protocol);
transport.open();
//调用client方法
List<User> list = client.queryUserList();
for(User user : list){
System.out.println(user.userId + " " + user.username + " " + user.password);
}
Map<String, String> map = client.queryUserNamePass();
System.out.println(map);
User user = client.queryUser(10);
System.out.println(user.userId + " " + user.username + " " + user.password);
Map<Integer, User> map_u = client.queryUserMap();
System.out.println(map_u);
User uu = new User();
uu.userId = 1111;
uu.username = "mmbbmmbb";
uu.password = "ppbbppbb";
client.addUser(uu);
}catch(Exception e){
e.printStackTrace();
}
} public static void startAsynClient(){
try{
TAsyncClientManager clientManager = new TAsyncClientManager();
TNonblockingTransport transport = new TNonblockingSocket(ip, port, time_out);
TProtocolFactory tprotocol = new TCompactProtocol.Factory();
ThriftService.AsyncClient asyncClient = new ThriftService.AsyncClient(tprotocol, clientManager, transport);
System.out.println("Client start ...");
CountDownLatch latch = new CountDownLatch(1);
AsynCallback callBack = new AsynCallback(latch);
System.out.println("call method queryUser start ...");
asyncClient.queryUser(100, callBack);
System.out.println("call method queryUser end");
boolean wait = latch.await(30, TimeUnit.SECONDS);
System.out.println("latch.await =:" + wait);
}catch(Exception e){
e.printStackTrace();
}
} public void run(){
Client.startSimpleClient();
} public static void main(String args[]){
//调用简单服务器
// Client.startSimpleClient();
/*Client c1 = new Client();
Client c2 = new Client(); new Thread(c1).start();
new Thread(c2).start();*/ // Client.startNonblockingClient();
// Client.startNonblockingClient();
Client.startAsynClient();
}
}
客户端实现了 阻塞单线程 和 异步客户端
具体代码在github上:https://github.com/WaterHsu/thrift-example.git
thrift学习笔记的更多相关文章
- Thrift学习笔记—IDL基本类型
thrift 采用IDL(Interface Definition Language)来定义通用的服务接口,并通过生成不同的语言代理实现来达到跨语言.平台的功能.在thrift的IDL中可以定义以下一 ...
- 学习笔记:The Log(我所读过的最好的一篇分布式技术文章)
前言 这是一篇学习笔记. 学习的材料来自Jay Kreps的一篇讲Log的博文. 原文很长,但是我坚持看完了,收获颇多,也深深为Jay哥的技术能力.架构能力和对于分布式系统的理解之深刻所折服.同时也因 ...
- springmvc学习笔记--Interceptor机制和实践
前言: Spring的AOP理念, 以及j2ee中责任链(过滤器链)的设计模式, 确实深入人心, 处处可以看到它的身影. 这次借项目空闲, 来总结一下SpringMVC的Interceptor机制, ...
- 学习笔记:The Log(我所读过的最好的一篇分布式技术文章)
前言 这是一篇学习笔记. 学习的材料来自Jay Kreps的一篇讲Log的博文. 原文非常长.可是我坚持看完了,收获颇多,也深深为Jay哥的技术能力.架构能力和对于分布式系统的理解之深刻所折服.同一时 ...
- Dubbo -- 系统学习 笔记 -- 依赖
Dubbo -- 系统学习 笔记 -- 目录 依赖 必需依赖 缺省依赖 可选依赖 依赖 必需依赖 JDK1.5+ 理论上Dubbo可以只依赖JDK,不依赖于任何三方库运行,只需配置使用JDK相关实现策 ...
- spring cloud 学习(二)关于 Eureka 的学习笔记
关于 Eureka 的学习笔记 个人博客地址 : https://zggdczfr.cn/ ,欢迎光临~ 前言 Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务.Sprin ...
- SparkSQL学习笔记
概述 冠状病毒来临,宅在家中给国家做贡献之际,写一篇随笔记录SparkSQL的学习笔记,目的有二,一是记录整理之前的知识作为备忘录,二是分享技术,大家共同进步,有问题也希望大家不吝赐教.总体而言,大数 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
随机推荐
- Int.Parse()、Convert.toInt32()和(int)区别
通过网上的查询从而了解了Int.Parse().Convert.toInt32()和(int)区别. 一.定义上的差别 int类型表示一种整型,.NET Framework 类型为 System.In ...
- 关于“心脏出血”漏洞(heartbleed)的理解
前阵子“心脏出血”刚发生的时候读了下源代码,给出了自己觉得比较清楚的理解. -------------------------穿越时空的分割线--------------------------- ...
- Backbone.js developer 武汉 年薪8w-10w
1. 精通Backbone.js 2. 熟练Ajax.NoSQL.RESTful APIs 3. 了解Pusher.com和 Parse.com 4. 具有良好的沟通能力,学习能力,敬 ...
- web前端开发分享-css,js入门篇(转)
转自:http://www.cnblogs.com/jikey/p/3600308.html 关注前端这么多年,没有大的成就,就入门期间积累了不少技巧与心得,跟大家分享一下,不一定都适合每个人,毕竟人 ...
- 线段相交 POJ 2653
// 线段相交 POJ 2653 // 思路:数据比较水,据说n^2也可以过 // 我是每次枚举线段,和最上面的线段比较 // O(n*m) // #include <bits/stdc++.h ...
- 简易版CSS3 Tab菜单 实用的Tab切换
今天我们要来分享一款非常简易而又实用的CSS3 Tab菜单,Tab菜单没有非常华丽的动画,但是代码非常简洁易懂,也可以在大部分场合使用,因此也非常实用,如果你需要加入动画效果,也可以自己方便地修改这款 ...
- 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇08:弹幕系统》本系列完结
8.弹幕系统 弹幕系统概述: 弹幕系统的设计体现了射击游戏的基本要素,玩家要在敌人放出的大量子弹(弹幕)的细小空隙间闪避,能在玩家闪躲弹幕的时候给玩家带来快感,接近满屏的子弹,增加了对玩家的视觉冲击力 ...
- ZOJ3772 - Calculate the Function(线段树+矩阵)
题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...
- Microsoft TFS 如何显示在Windows 的上下文菜单中
How to showing in Windows Explorer context for TFS I am not sure if this would help or you are willi ...
- UVALive 7327 Digit Division (模拟)
Digit Division 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/D Description We are given ...