1、Hello.thrift文件内容
namespace java com.seasy.thrift struct Message { 1: i32 type; 2: binary data; } struct Response { 1: i32 code; 2: string message; } service Hello{ string helloString(1:string param) i32 helloInt(1:i32 param) bool helloBoolean(1:bool param) void helloVoid() Response sendMessage(1:Message message) }
2、堵塞式线程池服务模型
server端:
TServerSocket serverTransport = new TServerSocket(Configuration.SERVER_PORT, Configuration.TIMEOUT); TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl()); TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport); tArgs.requestTimeoutUnit(TimeUnit.MILLISECONDS); tArgs.requestTimeout(5000); tArgs.protocolFactory(new TBinaryProtocol.Factory()); tArgs.processor(processor); TServer server = new TThreadPoolServer(tArgs); server.serve();
client端:
TTransport transport = new TSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); Hello.Client client = new Hello.Client(protocol); ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8")); client.sendMessage(new Message(1, data));
3、非堵塞式多线程服务模型
server端:
TNonblockingServerSocket serverSocket = new TNonblockingServerSocket(8080); TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl()); TNonblockingServer.Args tArgs = new TNonblockingServer.Args(serverSocket); tArgs.processor(processor); tArgs.transportFactory(new TFramedTransport.Factory()); tArgs.protocolFactory(new TCompactProtocol.Factory()); TServer tserver = new TNonblockingServer(tArgs); tserver.serve();
client端:
TTransport transport = new TFramedTransport(new TSocket(host, port)); transport.open(); TProtocol protocol = new TCompactProtocol(transport); Hello.Client client = new Hello.Client(protocol); ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8")); Response response = client.sendMessage(new Message(1, data)); System.out.println(response.getCode() + ", " + response.getMessage());
异步client端:
TAsyncClientManager asyncClientManager = new TAsyncClientManager(); final TNonblockingTransport nonblockingTransport = new TNonblockingSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT); TProtocolFactory protocolFactory = new TCompactProtocol.Factory(); final Hello.AsyncClient client = new Hello.AsyncClient(protocolFactory, asyncClientManager, nonblockingTransport); client.helloBoolean(true, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() { @Override public void onComplete(helloBoolean_call response) { try { System.out.println(response.getResult()); } catch (TException e) { e.printStackTrace(); } } @Override public void onError(Exception ex) { ex.printStackTrace(); } });
4、通讯层采用SSL安全认证
server端:
TSSLTransportParameters params = new TSSLTransportParameters(); params.setKeyStore("server.jks", "123456", "SunX509", "JKS"); TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(Configuration.SERVER_PORT, Configuration.TIMEOUT, null, params); TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl()); TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport); tArgs.processor(processor); tArgs.protocolFactory(new TBinaryProtocol.Factory()); server = new TThreadPoolServer(tArgs); server.serve();
client端:
TSSLTransportParameters params = new TSSLTransportParameters(); params.setTrustStore("CA.jks", "123456", "SunX509", "JKS"); transport = TSSLTransportFactory .getClientSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT, params); TProtocol protocol = new TBinaryProtocol(transport); Hello.Client client = new Hello.Client(protocol); ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8")); client.sendMessage(new Message(1, data));
5、多处理器服务模型
server端:
TMultiplexedProcessor processor = new TMultiplexedProcessor(); processor.registerProcessor("helloService", new Hello.Processor<Hello.Iface>(new HelloServiceImpl())); TServerSocket serverTransport = new TServerSocket(Configuration.SERVER_PORT, Configuration.TIMEOUT); TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport); tArgs.requestTimeoutUnit(TimeUnit.MILLISECONDS); tArgs.requestTimeout(5000); tArgs.protocolFactory(new TBinaryProtocol.Factory()); tArgs.processor(processor); TServer server = new TThreadPoolServer(tArgs); server.serve();
client端:
TTransport transport = new TSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "helloService"); Hello.Client client = new Hello.Client(multiplexedProtocol); ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8")); client.sendMessage(new Message(1, data));
6、半同步半异步服务模型
server端:
TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl()); TNonblockingServerSocket socketTransport = new TNonblockingServerSocket(Configuration.SERVER_PORT); THsHaServer.Args thhsArgs = new THsHaServer.Args(socketTransport); thhsArgs.processor(processor); thhsArgs.transportFactory(new TFramedTransport.Factory()); thhsArgs.protocolFactory(new TCompactProtocol.Factory()); TServer server = new THsHaServer(thhsArgs); server.serve();
client端:
TTransport transport = new TFramedTransport(new TSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT)); transport.open(); TProtocol protocol = new TCompactProtocol(transport); Hello.Client client = new Hello.Client(protocol); ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8")); client.sendMessage(new Message(1, data));
7、TThreadedSelectorServer使用范例
服务端:
TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor(); multiplexedProcessor.registerProcessor("Hello", new Hello.Processor<Hello.Iface>(new HelloServiceImpl())); TNonblockingServerTransport transport = new TNonblockingServerSocket(Configuration.SERVER_PORT, Configuration.TIMEOUT); TThreadedSelectorServer.Args tArgs = new TThreadedSelectorServer.Args(transport); tArgs.processor(multiplexedProcessor); tArgs.transportFactory(new TFramedTransport.Factory()); tArgs.protocolFactory(new TCompactProtocol.Factory()); tArgs.selectorThreads(5); tArgs.workerThreads(50); TServer server = new TThreadedSelectorServer(tArgs); server.serve();
客户端:
TTransport transport = new TFramedTransport(new TSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT)); transport.open(); TProtocol protocol = new TCompactProtocol(transport); TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "Hello"); client = new Hello.Client(multiplexedProtocol); System.out.println(client.helloString("hello string")); ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8")); Response response = client.sendMessage(new Message(1, data)); System.out.println(response.getCode() + ", " + response.getMessage());
相关推荐
thrift-0.9.1桌面生成代码工具, 免安装版本,可直接生成thrift代码的工具(0.9.1版本)
采用java操作thrift代码示例
Thrift使用示例代码,主要配合博客《由浅入深了解thrift》写的测试代码
Scrooge 是一个 Thrift 代码解析/生成器,能够生成 Scala 和 Java 代码。这就意味着,它能够取代 Apache Thrift 代码生成器,并能在 libthrift 上生成符合标准的可兼容的二进制编解码。 建议使用Scala语法生成...
利用thrift代码生成工具生成rpc远程调用需要的代码,并包含生成代码所需要的jar
Thrift0.9.1 示例代码 Java版本 Thrift0.9.1 示例代码 Java版本
java代码使用thrift2操作hbase示例,thrift2连接hbase添加数据,单条查找,删除数据,根据扫描器查找,修改数据等测试实例
thrift官方代码+与dubbo集成支持原生thrift协议
本例改编自Apache Thrift教程: http://mikecvet.wordpress.com/2010/05/13/apache-thrift-tutorial-the-sequel/ http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/ 原教程使用的是c++ server和...
thrift中间件小例子
thrift web实例代码。使用thrift js时,PRotocol需要使用TJSONPRotocol/TBinaryProtocol协议,Thrift.Protocol对应的是TJSONPRotocol。Transport需要使用TXHRTransport/TWebSocketTransport通道,Thrift.Transport...
thrift入门教程+代码
Thrift的简单使用demo
Thrift双向通讯java代码
thrift在windows下生成C#代码
Thrift RPC客户端的服务化框架代码,
Java中使用Thrift实现RPC示例代码.rar
RemoteExporter这块其实主要就是使用了它getServiceInterface和getProxyForService两个方法,这个和thrift服务端代码结合暴露内部的服务。客户端主要使用spring的MethodInterceptor和UrlBasedRemoteAccessor以及代理...
Thrift中实现Java与Python的RPC互相调用示例代码;Thrift中实现Java与Python的RPC互相调用示例代码;Thrift中实现Java与Python的RPC互相调用示例代码
Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, ...