当前位置:网站首页>Thrift getting started
Thrift getting started
2022-06-26 00:07:00 【Keyboard singer】
One 、 brief introduction
Thrift By the first FaceBook Research and development , It is mainly used for communication between various services RPC signal communication , Support for cross language , Common languages such as C++,Java,Python,PHP,Ruby,Erlang,Perl,Haskell,C# wait .
Thrift It's a typical CS( client / Server side ) structure , Client and server can be developed in different languages , Then there must be an intermediate language to associate the languages of the client and the server , This language is IDL(Interface Description Language)
Two 、 Front end environment configuration
I am using mac System , Need to be installed on your computer homebrew, In use homebrew To install git and thrift.git Your environment is installation thrift The basis of !
1.homebrew Installation :
xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
2.git Installation
brew install git
3.thrift Installation :
brew install thrift
4. The identification of successful installation is as follows :
as for windows Installation , Details please see Official website
3、 ... and 、Thrift type
1. data type
Thrift Unsigned types are not supported
byte: Signed byte
i16:16 Bit signed integer
i32:32 Bit signed integer
i64:64 Bit signed integer
double:64 Bit floating point
string: character string
2. Container type
Elements in a collection can be anything other than service Any type other than , Include exception
list: A series of T An ordered list of types of data , Elements can be repeated
set: A series of T An unordered list of types of data , Element is not repeatable
map: A dictionary structure ,key by K type ,value by V type , amount to Java Medium HashMap
Four 、Thrift working principle
1、 How to realize the communication between multiple languages ?
Data transmission uses socket( Many languages support ), Data in a specific format (String etc. ) send out , The language of the receiver shall be parsed
2、 Definition thrift The file of , from thrift file (IDL) Generate interfaces for both languages 、model, In the generated model And there will be decoding code in the interface .
5、 ... and 、Demo Small cases
1. introduce maven rely on
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.14.2</version>
</dependency>
2. To write thrift file
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. Generate for by command gen-java file


If the class file is red ! Please add package sentence :
4. To write Server End code
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. To write Client End
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(" Zhang San ");
System.out.println(person.getUsername());
System.out.println(person.getAge());
System.out.println(person.isMarried());
System.out.println("---------");
Person person2 = new Person();
person2.setUsername(" Li Si ");
person2.setAge(30);
person2.setMarried(true);
client.savePerson(person2);
}catch (Exception e){
throw new RuntimeException(e.getMessage(),e);
}finally {
transport.close();
}
}
}
6. test ( First run Server, Run again Client)

边栏推荐
- Efficacy of kiwi fruit enzyme_ Old bear passing by_ Sina blog
- 网络协议之:redis protocol详解
- redis之集群
- Common methods of object class
- 如何绕过SSL验证
- Idea common shortcut keys
- Hand made pl-2303hx USB to TTL level serial port circuit_ Old bear passing by_ Sina blog
- Sword finger offer 48 Longest substring without duplicate characters
- 搜索旋转数组II[抽象二分练习]
- 用ES5的方式实现const
猜你喜欢

Studio5k V28 installation and cracking_ Old bear passing by_ Sina blog

Common problems encountered when creating and publishing packages using NPM

About the solution to prompt modulenotfounderror: no module named'pymongo 'when running the scratch project
![寻找翻转数组的最小值[抽象二分]](/img/b9/1e0c6196e6dc51ae2c48f6c5e83289.png)
寻找翻转数组的最小值[抽象二分]

Establishment of multiple background blocks in botu software_ Old bear passing by_ Sina blog
![Bit Compressor [蓝桥杯题目训练]](/img/d5/231d20bf4104cc2619b2a4f19b605c.png)
Bit Compressor [蓝桥杯题目训练]

6. common instructions (upper) v-cloak, v-once, v-pre

ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided

文獻調研(三):數據驅動的建築能耗預測模型綜述

10.2.3、Kylin_kylin的使用,维度必选
随机推荐
DNS复习
Several common rich text editors
PCB生产为什么要做拼板和板边
常用的几款富文本编辑器
DHCP review
记录一些cf的题
SMT葡萄球现象解决办法
文獻調研(三):數據驅動的建築能耗預測模型綜述
10.4.1、數據中臺
Given the parameter n, there will be n integers 1, 2, 3,... From 1 to n, n. These n arrays have n! An arrangement that lists all columns in ascending order of size and marks them one by one. Given n a
smt贴片加工行业常见术语及知识汇总
Linking MySQL database with visual studio2015 under win10
Simulation connection between WinCC and STEP7_ Old bear passing by_ Sina blog
给定参数n,从1到n会有n个整数1,2,3,...,n,这n个数组共有n!种排列,按照大小顺序升序排列出所有列的情况,并一一标记,给定n和k,返回第k个值
SPI锡膏检查机的作用及原理
Sword finger offer 48 Longest substring without duplicate characters
Implement const in Es5
SSH review
Search rotation array ii[Abstract dichotomy exercise]
SMT贴片加工PCBA板清洗注意事项