Thrift是一个RPC框架
1. 用IDL定义好实体和服务框架,如实体字段名,类型等。服务名,服务参数,返回值等
2. 通过编译器或者说代码生成器生成RPC框架代码
IDL语法,代码生成器的安装使用可以在官网查
这里参考了一个网上的Demo,忘了出处了
Test.thrift
namespace java com.gxf.thrift
enum RequestType {
SAY_HELLO, //问好
QUERY_TIME, //询问时间
}
struct Request {
1: required RequestType type; // 请求的类型,必选
2: required string name; // 发起请求的人的名字,必选
3: optional i32 age; // 发起请求的人的年龄,可选
}
exception RequestException {
1: required i32 code;
2: optional string reason;
}
// 服务名
service HelloWordService {
string doAction(1: Request request) throws (1:RequestException qe); // 可能抛出异常。
}
使用代码生成器,生成框架代码:thrift --gen java Test.thrift
Server端逻辑实现,需要实现XXXService.Iface接口
HelloServiceImpl.java
public class HelloWordServiceImpl implements HelloWordService.Iface { // 实现这个方法完成具体的逻辑。 public String doAction(Request request) throws RequestException, org.apache.thrift.TException { System.out.println("Get request: " + request); if (StringUtils.isBlank(request.getName()) || request.getType() == null) { throw new com.gxf.thrift.RequestException(); } String result = "Hello, " + request.getName(); if (request.getType() == com.gxf.thrift.RequestType.SAY_HELLO) { result += ", Welcome!"; } else { result += ", Now is " + new Date().toLocaleString(); } return result; } }
Server端启动代码HelloServer.java
import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TThreadPoolServer; import org.apache.thrift.transport.TServerSocket; import java.net.ServerSocket; public class HelloWordServer { public static void main(String[] args) throws Exception { ServerSocket socket = new ServerSocket(7912); TServerSocket serverTransport = new TServerSocket(socket); TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory(); /** * 关联处理器与GreetingService服务实现 */ TProcessor processor = new HelloWordService.Processor(new HelloWordServiceImpl()); TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport); serverArgs.processor(processor); serverArgs.protocolFactory(proFactory); TServer server = new TThreadPoolServer(serverArgs); System.out.println("Start server on port 7912..."); server.serve(); } }
客户端代码
HelloWorldClient.java
import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; public class HelloWordClient { public static void main(String[] args) throws Exception { TTransport transport = new TSocket("127.0.0.1", 7912); TProtocol protocol = new TBinaryProtocol(transport); // 创建client com.gxf.thrift.HelloWordService.Client client = new com.gxf.thrift.HelloWordService.Client(protocol); transport.open(); // 建立连接 // 第一种请求类型 com.gxf.thrift.Request request = new com.gxf.thrift.Request() .setType(com.gxf.thrift.RequestType.SAY_HELLO).setName("guanxiangfei").setAge(24); System.out.println(client.doAction(request)); // 第二种请求类型 request.setType(com.gxf.thrift.RequestType.QUERY_TIME).setName("guanxiangfei"); System.out.println(client.doAction(request)); transport.close(); // 请求结束,断开连接 } }
客户端定义好请求实体,通过RPC请求服务
总结:
1. Thrift不仅仅提供了RPC,也提供了序列化反序列化框架
2. 序列化框架有多种序列化方式可以选择,二进制,json等
3. RPC传输方式有Socket, SockectChannel, File等