标签:
下载地址:http://thrift.apache.org/download
thrift-0.9.3.exe 用于编译Thrift中间文件生成对应语言代码的工具
thrift-0.9.3.tar.gz 包含Thrift各个语言的源码库,以及一些测试程序代码等
解压thrift-0.9.3.tar.gz文件。
(1) 生成.NET库
打开工程:E:\Thrift\thrift-0.9.3\lib\csharp\src\Thrift.sln 编译,即可生成Thrift.dll
我的环境是VS2010以及.NET 4.0
(2) 生成Java库
Java库是通过Ant构建的,需要安装Ant,安装步骤这里就不赘述了。
打开命令行CD到java库代码所在的路径:E:\Thrift\thrift-0.9.3\lib\java(包含build.xml)
然后直接执行ant命令即可发现build目录下生成对应的jar文件。
namespace java test namespace csharp test service Hello { string helloString(1:string word) }
Thrift-0.9.3.exe –gen java test.thrift
Thrift-0.9.3.exe –gen csharp test.thrift
可以看到在当前目录下会出现生成的对应代码。
新建普通Java项目,将之前ant编译生成的相关的jar文件(libthrift-0.9.3.jar以及E:\Thrift\thrift-0.9.3\lib\java\build\lib目录下所有jar文件)add tobuild path;然后还要将生成的Hello.java也加入到工程中,注意包名。
(1)首先编写接口实现类:
package test; import org.apache.thrift.TException; import test.Hello.Iface; public classHelloImpl implementsIface{ privatestaticintcount= 0; @Override publicString helloString(String word)throwsTException { count += 1; System.out.println("get " + word + " " +count); return "hello " + word + " " + count; } }
(2)编写寄宿代码,启动并监听在指定端口:
package test; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TBinaryProtocol.Factory; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TThreadPoolServer; import org.apache.thrift.server.TThreadPoolServer.Args; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TTransportException; import test.Hello.Processor; public classServer { @SuppressWarnings({"rawtypes", "unchecked" }) public void startServer() { try { System.out.println("thrift server host on port 8899"); TServerSocket serverTransport = new TServerSocket(8899); Hello.Processorprocess = newProcessor(newHelloImpl()); Factory portFactory = newTBinaryProtocol.Factory(true, true); Args args = newArgs(serverTransport); args.processor(process); args.protocolFactory(portFactory); TServer server = newTThreadPoolServer(args); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } } publicstaticvoidmain(String[] args) { System.out.println("thrift server init"); Server server = new Server(); System.out.println("thrift server start"); server.startServer(); System.out.println("thrift server end"); } }
新建普通控制台项目,引入Thrift.dll;然后还要将生成的Hello.cs也加入到工程中。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Thrift.Transport; using Thrift.Protocol; namespace ThriftTest { class ClientTest { static void Main(string[]args) { TTransporttransport = new TSocket("localhost", 8899); TProtocolprotocol = new TBinaryProtocol(transport); test.Hello.Client client = newtest.Hello.Client(protocol); transport.Open(); Console.WriteLine("Client calls client.helloString()....."); Console.WriteLine(client.helloString("jiyiqin")); client.Dispose(); } } }
运行java服务端Server.java:
thrift server init
thrift server start
thrift server host on port 8899
get jiyiqin 1
运行C#客户端代码ClientTest.cs
Client calls client.helloString().....
Hello jiyiqin 1
请按任意键继续…
可以看到Thrift和ICE等跨语言RPC框架开发步骤非常相似,几乎相同,生成的文件也都差不多,但是和基于Servlet的Hessian这种跨语言RPC框架差别较大。
标签:
原文地址:http://blog.csdn.net/jiyiqinlovexx/article/details/50478588