Thrift基础使用

来源:互联网 发布:蜂窝移动数据当期时间 编辑:程序博客网 时间:2024/06/06 03:26

1        RPC

Client端通过动态代理,根据双方约定的服务接口存根,把要调用的远程接口的名称和接口参数发送到Server端,并获得返回值。其中:

Ø        双方持有共同的接口存根(接口定义)。

Ø        Server端实现具体的接口实现类。

Ø        Client通过动态代理,封装网络通讯过程,发送调用接口名称参数到Server端。

Ø        Server将处理结果返回Client。

实例参考:http://www.cnblogs.com/codingexperience/p/5930752.html

2        主要RPC框架

参考:http://www.open-open.com/lib/view/open1426302068107.html

2.1 Thrift

官网:http://thrift.apache.org

代码生成器下载:http://apache.fayea.com/thrift/0.10.0

Thrift数据定义类型:http://thrift.apache.org/docs/types

Thrift接口定义语言(IDL):http://thrift.apache.org/docs/idl

Thrift脚本示例和说明:

https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/tutorial.thrift

https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/shared.thrift

Thrift白皮书:http://thrift.apache.org/static/files/thrift-20070401.pdf

白皮书中文翻译:http://docong.iteye.com/blog/821899

Thrift官网实例:http://thrift.apache.org/tutorial/java

2.1.1   实例

依赖包:

libthrift-0.10.0.jar(根据官方说明生成,也可网上下载网友生成好的)

log4j-1.2.15.jar

slf4j-api-1.7.5.jar

slf4j-log4j12-1.7.5.jar

2.1.1.1 thrift脚本

HelloWorldIDL.thrift

namespace java thrift.demo

service HelloWorldIDL {

  string sayHello(1:stringusername)

}

2.1.1.2 生成通用服务接口类

thrift --gen java HelloWorldIDL.thrift

生成HelloWorldIDL.java,该类请勿手工修改。

2.1.1.3 Server端编写服务实现类

package thrift.demo;import org.apache.thrift.TException;public class HelloWorldHandler implements HelloWorldIDL.Iface {@Overridepublic String sayHello(String username) throws TException {return "Hello Thrift! [" + username + "]";}}

2.1.1.4 Server端注册服务并监听请求

package thrift.demo;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 org.apache.thrift.transport.TServerTransport;import org.apache.thrift.transport.TTransportException;import thrift.demo.HelloWorldIDL.Iface;public class HelloWorldServer {public static void main(String[] args) throws TTransportException {HelloWorldIDL.Iface helloWorldHandler = new HelloWorldHandler();HelloWorldIDL.Processor<Iface> processor = new HelloWorldIDL.Processor<Iface>(helloWorldHandler);TServerTransport serverTransport = new TServerSocket(7890);TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport);tArgs.processor(processor);tArgs.protocolFactory(new TBinaryProtocol.Factory());TServer server = new TThreadPoolServer(tArgs);server.serve();}}

2.1.1.5 Client发送请求并得到响应

package thrift.demo;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 HelloWorldClient {public static void main(String[] args) {try {TTransport transport = new TSocket("127.0.0.1", 7890);transport.open();TProtocol protocol = new TBinaryProtocol(transport);HelloWorldIDL.Client client = new HelloWorldIDL.Client(protocol);String result = client.sayHello("TTHHRRIIFFTT");System.out.println(result);transport.close();} catch (Exception e) {e.printStackTrace();}}}

2.1.1.6 运行

运行HelloWorldServer,启动服务。

运行HelloWorldClient,调用远程服务接口,控制台打印如下:

Received1

HelloThrift! [TTHHRRIIFFTT]

成功调用远程服务。

2.2 gRPC、Protobuf、ZeroC ICE

3        常见问题

 

原创粉丝点击