一。thrift简介
thrift通过一个中间语言IDL(接口定义语言)来定义RPC的数据类型和接口,这些内容写在以.thrift结尾的文件中,然后通过特殊的编译器来生成不同语言的代码,以满足不同需要的开发者,比如java开发者,就可以生成java代码,python开发者可以生成python代码,生成的代码中不但包含目标语言的接口定义,方法,数据类型,还包含有RPC协议层和传输层的实现代码.
thrift是一种c/s的架构体系.在最上层是用户自行实现的业务逻辑代码.第二层是由thrift编译器自动生成的代码,主要用于结构化数据的解析,发送和接收。TServer主要任务是高效的接受客户端请求,并将请求转发给Processor处理。Processor负责对客户端的请求做出响应,包括RPC请求转发,调用参数解析和用户逻辑调用,返回值写回等处理。从TProtocol以下部分是thirft的传输协议和底层I/O通信。TProtocol是用于数据类型解析的,将结构化数据转化为字节流给TTransport进行传输。TTransport是与底层数据传输密切相关的传输层,负责以字节流方式接收和发送消息体,不关注是什么数据类型。底层IO负责实际的数据传输,包括socket、文件和压缩数据流等
thrift官网:http://thrift.apache.org/ thrift idl转换不同接口代码命令:http://archive.apache.org/dist/thrift/0.12.0/ idl语法规范:http://thrift.apache.org/docs/idl idel语法参考(github):https://github.com/apache/thrift/blob/master/tutorial/tutorial.thrift 不同语言服务器客户端通讯例子:http://thrift.apache.org/tutorial/ java服务器客户端通讯例子:http://thrift.apache.org/tutorial/java
二。thrift javarpc入门案例
1.定义idl文件
idl是thrift结尾的接口定义文件,拥有自己的语法,参考github上https://github.com/apache/thrift/blob/master/tutorial/tutorial.thrift 例子来编写
接口用途:
- 服务器需要实现这个接口,编写实现代码,供其他端调用。
- 客户端需要调用这个接口的远程实现。
单独创建一个maven项目新建一个目录thrift,目录下新建message.thrift,内容:
namespace java io.github.jiaozi789.service
struct Message{
1: string revciveEmail,
2: string content,
}
service MessageService{
void sendMessage(1: Message message),
i32 add(1: i32 i,2: i32 j),
}
代码解释:
- namesapce 语言 生成的java接口的报名
- struts Message 表示定义一个结构,生成java的类 1:表示第一个参数,string表示字符串(其他数据类型参考官方例子)
- service 表示定义接口 ,接口中定义方法
创建一个当前maven项目的子项目thrift_app,用来存放生成的java接口
下载转换程序 地址http://archive.apache.org/dist/thrift/0.12.0/ 将exe文件下载后,放入thrift目录
新建一个run.cmd文件添加生成接口的命令
thrift -gen java -out ../thrift_app/src/main/java ./message.thrift
在idea中选中run.cmd邮件open in terminate 执行 run即可刷新thrift_app/src/main/java目录生成了两个文件
以下服务器端和客户端代码 参考 http://thrift.apache.org/tutorial/java
服务器端:
新建服务器的maven模块项目thrift_server
添加MessageService.Iface接口的实现类
package io.github.jiaozi789.service.impl;
import io.github.jiaozi789.service.Message;
import io.github.jiaozi789.service.MessageService;
import org.apache.thrift.TException;
public class MessageServiceImpl implements MessageService.Iface
{
public void sendMessage(Message message) throws TException {
System.out.println(message.getContent());
}
@Override
public int add(int i, int j) throws TException {
return i+j;
}
}
添加thrift和api模块项目的maven依赖
<dependencies>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.11.0</version>
</dependency>
<dependency>
<artifactId>thrift_app</artifactId>
<groupId>io.github.jiaozi789</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
添加运行main类启动端口绑定该实现类
package io.github.jiaozi789;
import io.github.jiaozi789.service.MessageService;
import io.github.jiaozi789.service.impl.MessageServiceImpl;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;
public class Server {
public static void main(String[] args) throws TTransportException {
MessageServiceImpl msi=new MessageServiceImpl();
MessageService.Processor processor=new MessageService.Processor(msi);
TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(new TServer.Args(serverTransport).processor(processor));
server.serve();
}
}
启动查看9090端口已启动
C:\Users\jiaozi>netstat -aon | find "9090"
TCP 0.0.0.0:9090 0.0.0.0:0 LISTENING 98364
TCP [::]:9090 [::]:0 LISTENING 98364
客户端:
新建thrift-client模块项目
添加thrift和api模块项目的maven依赖
<dependencies>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.11.0</version>
</dependency>
<dependency>
<artifactId>thrift_app</artifactId>
<groupId>io.github.jiaozi789</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
添加一个main方法调用服务端,调用sendMessage方法
package io.github.jiaozi789.client;
import io.github.jiaozi789.service.Message;
import io.github.jiaozi789.service.MessageService;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
public class Client {
public static void main(String[] args) throws Exception {
TTransport transport=new TSocket("localhost", 9090);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
MessageService.Client client=new MessageService.Client(protocol);
Message message = new Message();
message.setContent("hello server");
client.sendMessage(message);
}
}
运行服务器端输出 hello server
也可以使用命令生成python的接口端,使用python客户端调用