Apache Thrift是一个facebook简历的RPC框架,现在是一个Apache的顶级项目。Thrift允许通过一个跨语言的定义文件的方式定义数据类型和服务接口,这个文件作为RPC客户端和服务器通信的标准,你也可以去看看Thrift的白皮书了解更多信息。
根据Apache Thrift的官方站点的描述,Thrift是一个:
software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.
安装Thrift比较的烦,但是在Windows下官方编译了一个thrift.exe,下载安装就行了。
写 Thrift定义文件(.thrift file)
如果你之前有接触过这个东西的话,写定义文件非常的简单。但这里可以参考官方的教程快速开始。
示例定义文件(add.thrift)
|
1
|
namespace java com.eviac.blog.samples.thrift.server // defines the namespace
|
|
3
|
typedef i32 int //typedefs to get convenient names for your types
|
|
5
|
service AdditionService { // defines the service to add two numbers
|
|
6
|
int add(1:int n1, 2:int n2), //defines a method
|
编译Thrift定义文件
下面的命令编译.thrift文件
对于我的例子来讲,命令是:
|
1
|
thrift --gen java add.thrift
|
在执行完代码后,在gen-java目录下你会发现构建RPC服务器和客户端有用的源代码。在我的例子中我将创建一个叫做AddtionService.java的java文件。
写一个 service handler
Service handler 类必须实现 AdditionService.Iface接口。
示例Service handler(AdditionServiceHandler.java)
|
01
|
package com.eviac.blog.samples.thrift.server;
|
|
03
|
import org.apache.thrift.TException;
|
|
05
|
public class AdditionServiceHandler implements AdditionService.Iface {
|
|
08
|
public int add(int n1, int n2) throws TException {
|
写一个简单的服务器
下面的示例代码是一个简单的Thrift服务器。可以看到下面的代码中有一段是注释了的,可以去掉注释来启用多线程服务器。
示例服务器(MyServer.java)
|
01
|
package com.eviac.blog.samples.thrift.server;
|
|
03
|
import org.apache.thrift.transport.TServerSocket;
|
|
04
|
import org.apache.thrift.transport.TServerTransport;
|
|
05
|
import org.apache.thrift.server.TServer;
|
|
06
|
import org.apache.thrift.server.TServer.Args;
|
|
07
|
import org.apache.thrift.server.TSimpleServer;
|
|
09
|
public class MyServer {
|
|
11
|
public static void StartsimpleServer(AdditionService.Processor processor) {
|
|
13
|
TServerTransport serverTransport = new TServerSocket(9090);
|
|
14
|
TServer server = new TSimpleServer(
|
|
15
|
new Args(serverTransport).processor(processor));
|
|
17
|
// Use this for a multithreaded server
|
|
18
|
// TServer server = new TThreadPoolServer(new
|
|
19
|
// TThreadPoolServer.Args(serverTransport).processor(processor));
|
|
21
|
System.out.println("Starting the simple server...");
|
|
23
|
} catch (Exception e) {
|
|
28
|
public static void main(String[] args) {
|
|
29
|
StartsimpleServer(new AdditionService.Processor(new AdditionServiceHandler()));
|
写一个客户端
下面的例子是一个使用Java写的客户端短使用AdditionService的服务。
|
01
|
package com.eviac.blog.samples.thrift.client;
|
|
03
|
import org.apache.thrift.TException;
|
|
04
|
import org.apache.thrift.protocol.TBinaryProtocol;
|
|
05
|
import org.apache.thrift.protocol.TProtocol;
|
|
06
|
import org.apache.thrift.transport.TSocket;
|
|
07
|
import org.apache.thrift.transport.TTransport;
|
|
08
|
import org.apache.thrift.transport.TTransportException;
|
|
10
|
public class AdditionClient {
|
|
12
|
public static void main(String[] args) {
|
|
17
|
transport = new TSocket("localhost", 9090);
|
|
20
|
TProtocol protocol = new TBinaryProtocol(transport);
|
|
21
|
AdditionService.Client client = new AdditionService.Client(protocol);
|
|
23
|
System.out.println(client.add(100, 200));
|
|
26
|
} catch (TTransportException e) {
|
|
28
|
} catch (TException x) {
|
运行服务端代码(MyServer.java)将会看到下面的输出。
|
1
|
Starting the simple server...
|
然后运行客户端代码(AdditionClient.java),将会看到如下输出。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29754888/viewspace-1220319/
,如需转载,请注明出处,否则将追究法律责任。