当前位置:网站首页>Thrift入门学习
Thrift入门学习
2022-06-25 22:11:00 【键盘歌唱家】
一、简介
Thrift最初由FaceBook研发,主要用于各个服务之间的RPC通信,支持跨语言,常用的语言比如C++,Java,Python,PHP,Ruby,Erlang,Perl,Haskell,C#等等。
Thrift是一个典型的CS(客户端/服务端)结构,客户端和服务端可以使用不同的语言开发,那么一定就要有一种中间语言来关联客户端和服务端的语言,这种语言就是IDL(Interface Description Language)
二、前置的环境配置
本人目前使用的是mac系统,需要电脑上安装homebrew,在用homebrew 来安装git和thrift。git的环境是安装thrift的基础!
1.homebrew的安装:
xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
2.git的安装
brew install git
3.thrift的安装:
brew install thrift
4.安装成功的标识如下:
至于windows的安装,详情请看官网
三、Thrift类型
1.数据类型
Thrift不支持无符号类型
byte:有符号字节
i16:16位有符号整数
i32:32位有符号整数
i64:64位有符号整数
double:64位浮点数
string:字符串
2.容器类型
集合中元素可以是除了service之外的任何类型,包括exception
list:一系列由T类型的数据组成的有序列表,元素可以重复
set:一系列由T类型的数据组成的无序列表,元素不可重复
map:一个字典结构,key为K类型,value为V类型,相当于Java中的HashMap
四、Thrift工作原理
1、如何实现多语言之间的通信?
数据传输使用socket(多种语言均支持),数据再以特定的格式(String等)发送,接受方语言进行解析
2、定义thrift的文件,由thrift文件(IDL)生成双方语言的接口、model,在生成的model以及接口中会有解码编码的代码。
五、Demo小案例
1.引入maven依赖
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.14.2</version>
</dependency>
2.编写thrift文件
namespace java thrift.generated
typedef i16 short
typedef i32 int
typedef i64 long
typedef bool boolean
typedef string String
struct Person {
1: optional String username,
2: optional int age,
3: optional boolean married
}
exception DataException {
1: optional String message,
2: optional String callStack,
3: optional String date
}
service PersonService {
Person getPersonByUsername(1: required String username) throws (1: DataException dataException),
void savePerson(1: required Person person) throws (1: DataException dataException)
}
3.通过命令生成对于的gen-java文件


如果类文件爆红!请在文件头加上package语句:
4.编写Server端代码
package com.maoyan.luzelong.thrift.test;
import com.maoyan.luzelong.thrift.generated.PersonService;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.layered.TFramedTransport;
public class ThriftServer {
public static void main(String[] args) throws Exception{
TNonblockingServerSocket socket = new TNonblockingServerSocket(8899);
THsHaServer.Args arg = new THsHaServer.Args(socket).minWorkerThreads(2).maxWorkerThreads(4);
PersonService.Processor<PersonServiceImpl> processor = new PersonService.Processor<>(new PersonServiceImpl());
arg.protocolFactory(new TCompactProtocol.Factory());
arg.transportFactory(new TFramedTransport.Factory());
arg.processorFactory(new TProcessorFactory(processor));
TServer server = new THsHaServer(arg);
System.out.println("Thrift Server start");
server.serve();
}
}
5.编写Client端
package com.maoyan.luzelong.thrift.test;
import com.maoyan.luzelong.thrift.generated.Person;
import com.maoyan.luzelong.thrift.generated.PersonService;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFramedTransport;
public class ThriftClient {
public static void main(String[] args) throws TTransportException {
TTransport transport = new TFramedTransport(new TSocket("localhost",8899),600);
TProtocol protocol = new TCompactProtocol(transport);
PersonService.Client client = new PersonService.Client(protocol);
try{
transport.open();
Person person = client.getPersonByUsername("张三");
System.out.println(person.getUsername());
System.out.println(person.getAge());
System.out.println(person.isMarried());
System.out.println("---------");
Person person2 = new Person();
person2.setUsername("李四");
person2.setAge(30);
person2.setMarried(true);
client.savePerson(person2);
}catch (Exception e){
throw new RuntimeException(e.getMessage(),e);
}finally {
transport.close();
}
}
}
6.测试(先运行Server,再运行Client)

边栏推荐
- How to generate get/set methods in idea
- 详细讲解局部变量、全局变量、静态变量三种类型
- php中使用Makefile编译protobuf协议文件
- Jenkins releases PHP project code
- Analyse des cinq causes profondes de l'échec du développement de produits
- step7和wincc联合仿真_过路老熊_新浪博客
- Final and static
- ssh的复习
- Search rotation array ii[Abstract dichotomy exercise]
- Recommended system design
猜你喜欢

iomanip头文件在实战中的作用

关于运行scrapy项目时提示 ModuleNotFoundError: No module named 'pymongo‘的解决方案

利用VBScript连接mysql数据库_过路老熊_新浪博客
![Find the minimum value of flipped array [Abstract bisection]](/img/b9/1e0c6196e6dc51ae2c48f6c5e83289.png)
Find the minimum value of flipped array [Abstract bisection]

Unsigned and signed vernacular

详细讲解局部变量、全局变量、静态变量三种类型

line-height小用

Common problems encountered when creating and publishing packages using NPM

SSL/TLS、对称加密和非对称加密和TLSv1.3

关于scrapy爬虫时,由spider文件将item传递到管道的方法注意事项
随机推荐
php中使用google protobuf协议环境配置
[转载]RSLOGIX 5000实例教程
JS中的数字数组去重
Recherche documentaire (3): examen des modèles de prévision de la consommation d'énergie des bâtiments fondés sur les données
Analyse des cinq causes profondes de l'échec du développement de produits
ssh的复习
使用npm创建并发布包时遇到的常见问题
《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
Let's talk about string today
Talk about singleton mode!
手工制作 pl-2303hx 的USB转TTL电平串口的电路_过路老熊_新浪博客
用ES5的方式实现const
The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
C ++ 引用与指针总结
树莓派开机发送热点进行远程登录
Understanding of pseudo classes
xtrabackup的备份还原
Hand made pl-2303hx USB to TTL level serial port circuit_ Old bear passing by_ Sina blog
Line height for small use
详细讲解局部变量、全局变量、静态变量三种类型