[三]、基本概念
1.数据类型
- 基本类型:
- bool:布尔值,true 或 false,对应 Java 的 boolean
- byte:8 位有符号整数,对应 Java 的 byte
- i16:16 位有符号整数,对应 Java 的 short
- i32:32 位有符号整数,对应 Java 的 int
- i64:64 位有符号整数,对应 Java 的 long
- double:64 位浮点数,对应 Java 的 double
- string:utf-8编码的字符串,对应 Java 的 String
- 结构体类型:
- struct:定义公共的对象,类似于 C 语言中的结构体定义,在 Java 中是一个 JavaBean
- 容器类型:
- list:对应 Java 的 ArrayList
- set:对应 Java 的 HashSet
- map:对应 Java 的 HashMap
- 异常类型:
- exception:对应 Java 的 Exception
- 服务类型:
- service:对应服务的类
2.服务端编码基本步骤:
- 实现服务处理接口impl
- 创建TProcessor
- 创建TServerTransport
- 创建TProtocol
- 创建TServer
- 启动Server
package service.demo; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TJSONProtocol; import org.apache.thrift.server.TThreadedSelectorServer; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TNonblockingServerSocket; import org.apache.thrift.transport.TNonblockingServerTransport; import org.apache.thrift.transport.TTransportException; import org.apache.thrift.transport.TTransportFactory; import service.demo.thrift.Hello; public class HelloServiceServer2 { /** * 启动 Thrift 服务器 * * @param args */ public static void main(String[] args) { int port = 7912; try { TNonblockingServerTransport transport = new TNonblockingServerSocket( 7912); // 定义处理器 TProcessor processor = new Hello.Processor(new HelloServiceImpl()); // 定义传输工厂(默认使用的是:) TTransportFactory transportFactory = new TFramedTransport.Factory(); // 定义协议工厂(默认使用的是:TBinaryProtocol) TJSONProtocol.Factory protocol = new TJSONProtocol.Factory(); // 定义server参数对象 TThreadedSelectorServer.Args ttsargs = new TThreadedSelectorServer.Args( transport); // 指定该server使用 的 传输,传输协议,业务处理器 ttsargs.protocolFactory(protocol); ttsargs.transportFactory(transportFactory); ttsargs.processor(processor); TThreadedSelectorServer server = new TThreadedSelectorServer( ttsargs); // 启动server server.serve(); } catch (TTransportException e) { e.printStackTrace(); } } }
3.客户端编码基本步骤:
- 创建Transport
- 创建TProtocol
- 基于TTransport和TProtocol创建 Client
- 调用Client的相应方法
同步代码:
// 设置调用的服务地址为本地,端口为 7911
TTransport transport = new TSocket("localhost", 7911);
transport = new TFramedTransport(transport);
transport.open();
// 设置传输协议为
TProtocol protocol = new TJSONProtocol(transport);
Hello.Client client = new Hello.Client(protocol);
// 调用服务的 helloVoid 方法
client.helloVoid();
//异步代码:
TAsyncClientManager clientManager = new TAsyncClientManager();
TNonblockingSocket transport = new TNonblockingSocket("localhost",
port, 500000);
TJSONProtocol.Factory protocolFactory = new TJSONProtocol.Factory();
Hello.AsyncClient asycnClient = new Hello.AsyncClient(
protocolFactory, clientManager, transport);
MyCallback callback = new MyCallback(wait);
asycnClient.helloString("libin", callback);
synchronized (wait) {
wait.wait();
}
AsyncClient.helloString_call msg = callback.getResult();
System.out.println("================== async client msg = " + msg);
4.数据传输协议
- TBinaryProtocol : 二进制格式.
- TCompactProtocol : 压缩格式
- TJSONProtocol : JSON格式
- TSimpleJSONProtocol : 提供JSON只写协议, 生成的文件很容易通过脚本语言解析
tips:客户端和服务端的协议要一致
thrift里面常见的几种同步异步用法
thrift客户端与服务器之间同步异步的方法:
客户端-服务器 | 调用类型 | 同步服务器 | 异步服务器 | |
同步客户端 | 同步调用 | OK | OK | |
同步客户端 | 异步调用 | OK(不保证返回结果) | 待确认 | |
异步客户端 | --- | NO | OK |
默认使用的是:同步客户端,同步调用,同步服务器
同步客户端,异步调用 ,同步服务器: thrift框架保证传输到位,但应用不保证一定处理完成
需要注意的问题
1. Thrift生成的server端是thread safe的. 但是client端不是thread safe. 所以需要多个thread和server端通信,则每个thread需要initiate一个自己的client实例.
2. 如果服务器采用TNonblockingServer的话,客户端必须采用TFramedTransport。程序链接的时候需要thriftnb。
3. 默认TServerSocket和TSocket都设置了NoDelay为1,使得报文尽快发送出去,如果客户端和服务器间传输数据量较大,通过可以设置NoDelay为0来开启Nagel算法,缓存一段数据后再进行发送,减少报文数量。
TSocket默认开启了Linger,并设置linger time为0,这样close会丢弃socket发送缓冲区中的数据,并向对端发送一个RST报文,close不会被阻塞,立即返回。
TServerSocket默认关闭了Linger,close不会被阻塞,立即返回。
4. fb303作为handler的基类,里面预置了一些rpc方法,用于监控,包括系统状态,请求次数等状态信息。
3. 默认TServerSocket和TSocket都设置了NoDelay为1,使得报文尽快发送出去,如果客户端和服务器间传输数据量较大,通过可以设置NoDelay为0来开启Nagel算法,缓存一段数据后再进行发送,减少报文数量。
TSocket默认开启了Linger,并设置linger time为0,这样close会丢弃socket发送缓冲区中的数据,并向对端发送一个RST报文,close不会被阻塞,立即返回。
TServerSocket默认关闭了Linger,close不会被阻塞,立即返回。
4. fb303作为handler的基类,里面预置了一些rpc方法,用于监控,包括系统状态,请求次数等状态信息。