在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
thrift 从 0.9.1版本开始,可以完美支持 go 语言,可以完美的实现跨语言的 rpc 调用了。下面以 go 和 java 语言相互调用为例。
/** example.thrift */ namespace go example service transdata { bool sendMsg(1: string msgJson), }
下载地址 http://www.apache.org/dyn/closer.cgi?path=/thrift/0.11.0/thrift-0.11.0.exe 生成库文件 thrift.0.11.0.exe -r --gen go example.thrift
/** thrift_example.go */
/** thrift_example_test.go */
namespace go service.thrift service Transdata { bool sendMsg(1: string msgJson), }
生成库文件 thrift.0.11.0.exe -r --gen java example.thrift
package service.thrift; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TBinaryProtocol.Factory; import org.apache.thrift.server.TNonblockingServer; import org.apache.thrift.server.TServer; import org.apache.thrift.transport.TNonblockingServerSocket; import org.apache.thrift.transport.TNonblockingServerTransport; import org.apache.thrift.transport.TTransportException; /** * @author zhengqian */ public class Server { public static void StartSimpleServer(Transdata.Processor<ExampleHandler> processor) { TNonblockingServerTransport serverTransport = null; try { serverTransport = new TNonblockingServerSocket(19090); } catch (TTransportException e) { e.printStackTrace(); } Factory protFactory = new TBinaryProtocol.Factory(true, true); //TCompactProtocol.Factory protFactory = new TCompactProtocol.Factory(); TNonblockingServer.Args args = new TNonblockingServer.Args( serverTransport); args.processor(processor); args.protocolFactory(protFactory); TServer server = new TNonblockingServer(args); System.out.println("Start server on port 19090 ..."); server.serve(); } public static void main(String[] args) { StartSimpleServer(new Transdata.Processor<ExampleHandler>(new ExampleHandler())); } }
package service.thrift; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; /** * @author zhengqian */ public class Client { public static void main(String[] args) { try { TTransport transport = new TFramedTransport(new TSocket("localhost", 19090)); TProtocol protocol = new TBinaryProtocol(transport); Transdata.Client client = new Transdata.Client(protocol); transport.open(); System.out.println(client.sendMsg("test string")); transport.close(); } catch (TTransportException e) { e.printStackTrace(); } catch (TException x) { x.printStackTrace(); } } }
|
请发表评论