✨
RPC服务框架
  • RPC服务框架
  • Dubbo
    • Dubbo简介
    • Dubbo技术原理
    • Dubbo配置使用
    • Dubbo控制台
    • 测验
  • gRPC
    • gRPC简介
    • gRPC配置及使用
    • grpc架构及原理
    • 测验
  • Protocol Buffers
    • Protocol Buffers简介
  • Thrift
    • Thrift简介
    • Thrift环境搭建
    • Thrift项目实现
    • Thrift架构及原理
    • Thrift类型系统
    • 待整理
    • 测验
  • spring cloud
    • Spring Cloud简介
Powered by GitBook
On this page
  • Thrift协议栈
  • Transport层
  • Protocol层
  • Processor层
  • Server实现

Was this helpful?

  1. Thrift

Thrift架构及原理

参考文章 https://my.oschina.net/wangmengjun/blog/917456 https://diwakergupta.github.io/thrift-missing-guide/thrift.pdf

PreviousThrift项目实现NextThrift类型系统

Last updated 4 years ago

Was this helpful?

Thrift协议栈

Transport层

其中Transport层提供了一个简单的网络读写抽象层。这使得thrift底层的transport从系统其它部分(如:序列化/反序列化)解耦。以下是一些Transport接口提供的方法:

open
close
read
write
flush

Protocol层

Protocol抽象层定义了一种将内存中数据结构映射成可传输格式的机制。换句话说,Protocol定义了datatype怎样使用底层的Transport对自己进行编解码。因此,Protocol的实现要给出编码机制并负责对数据进行序列化。

Thrift支持以下几种Protocol

  • TBinaryProtocol : 二进制格式.

  • TCompactProtocol : 压缩格式

  • TJSONProtocol : JSON格式

  • TSimpleJSONProtocol : 提供JSON只写协议, 生成的文件很容易通过脚本语言解析

  • 等等

Processor层

Processor封装了从输入数据流中读数据和向数据数据流中写数据的操作。读写数据流用Protocol对象表示。

与服务相关的processor实现由编译器产生。 Processor主要工作流程如下:

从连接中读取数据(使用输入protocol),将处理授权给handler(由用户实现),最后将结果写到连接上(使用输出protocol)。

Server实现

Server实现的几个步骤如下~

(1) 创建一个transport对象

(2) 为transport对象创建输入输出protocol

(3) 基于输入输出protocol创建processor

(4) 等待连接请求并将之交给processor处理

package top.lilixin.thrift.server;

import java.util.logging.Logger;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;

import top.lilixin.thrift.service.UserService;
import top.lilixin.thrift.service.impl.UserServiceImpl;

/**
 * @author lilixin
 *
 */
public class TSimpleServerExample {

	private static final Logger logger = Logger.getLogger(TSimpleServerExample.class.getName());

	private static final int SERVER_PORT = 9999;

	public static void main(String[] args) {

		try {
			
			/**
			 * 1. 创建Transport
			 */
			TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
			TServer.Args tArgs = new TServer.Args(serverTransport);
			
			/**
			 * 2. 为Transport创建Protocol
			 */
			tArgs.protocolFactory(new TBinaryProtocol.Factory());
			// tArgs.protocolFactory(new TCompactProtocol.Factory());
			// tArgs.protocolFactory(new TJSONProtocol.Factory());
			
			/**
			 * 3. 为Protocol创建Processor
			 */
			TProcessor tprocessor = new UserService.Processor<UserService.Iface>(new UserServiceImpl());
			tArgs.processor(tprocessor);


			/**
			 * 4. 创建Server并启动
			 * 
			 * org.apache.thrift.server.TSimpleServer - 简单的单线程服务模型,一般用于测试
			 */
			TServer server = new TSimpleServer(tArgs);
			logger.info("UserService TSimpleServer start ....");
			server.serve();
			

		} catch (Exception e) {
			logger.severe("Server start error!!!" + e.getLocalizedMessage());
			e.printStackTrace();
		}
	}
}