使用thrift进行跨语言调用(php c# java)
1:前言
實(shí)際上本文說(shuō)的是跨進(jìn)程的異構(gòu)語(yǔ)言調(diào)用,舉個(gè)簡(jiǎn)單的例子就是利用PHP寫的代碼去調(diào)C#或是java寫的服務(wù)端。其實(shí)除了本文提供的辦法還有其他辦法,例如http+xml(json)等等都能做到。
本文的意義是介紹thrift,記錄我在調(diào)試thrift時(shí)遇到的問題和相應(yīng)的解決方案,避免大家走彎路。
2:大概的流程
thrift是通過(guò)socket+序列化協(xié)議來(lái)完成跨語(yǔ)言調(diào)用的。類似的方案有protocolbuffer(http://code.google.com/p/protobuf/)這個(gè)性能出眾,thrift性能我回頭再做測(cè)試。
使用的流程是
A:定義自己的通信接口,接口可以使用的數(shù)據(jù)類型有string,int32等,當(dāng)然你也可以自己定義枚舉和結(jié)構(gòu)體
B:使用thrift.exe生成相應(yīng)的代碼
C:調(diào)用
3:c#,PHP,Java在調(diào)試thrift時(shí)遇到的問題
首先我們?nèi)ハ螺dthrift,地址是http://thrift.apache.org/
解壓縮后會(huì)看到
lib下就是執(zhí)行的各種語(yǔ)言的代碼。
A:調(diào)試PHP注意事項(xiàng)
一定要注冊(cè)各個(gè)php代碼也就是這句。
require_once __DIR__.'/lib/Thrift/ClassLoader/ThriftClassLoader.php';$loader = new ThriftClassLoader();$loader->registerNamespace('Thrift', __DIR__ . '/lib');$loader->register();B:調(diào)試java注意事項(xiàng)
下載沒有提供的包
地址是:
http://commons.apache.org/proper/commons-lang/download_lang.cgi
http://hc.apache.org/downloads.cgi
http://www.slf4j.org/
3:demo
定義接口(方法名不能一樣)
View Code enum ParameterValueType {AnsiString = 1,Byte,Boolean,Currency = 4,Date,DateTime,Decimal,Double,Guid,Int16,Int32,Int64,String,Time,Xml }enum ParameterValueDirection {Input,Output,InputOutput,ReturnValue }enum AdoCommandType {Text,StoredProcedure }struct DBParameter {1: string DbParameterName,2: ParameterValueType DbType,3: string DbParameterValue,4: ParameterValueDirection DbDirection,5: i32 Size }struct ResultMessage {1: i32 IsSuccess,2: string ErrorMessage,3: string ReturnValue,4: list<DBParameter> ReturnParameter,5: list<string> DataSetColumnName,6: list<list<string>> DataSetRowValue }service DataAccessComponent {ResultMessage ExecuteDataset(1:string ConnectionConfigKey,2:AdoCommandType commandType,3:string commandText,4:list<DBParameter> DBParameter), ResultMessage ExecuteNonQuery(1:string ConnectionConfigKey,2:AdoCommandType commandType,3:string commandText,4:list<DBParameter> DBParameter),ResultMessage ExecuteScalar(1:string ConnectionConfigKey,2:AdoCommandType commandType,3:string commandText,4:list<DBParameter> DBParameter), }生成代碼
View Code thrift -gen java ado.thrift thrift -gen php ado.thrift thrift -gen csharp ado.thrift根據(jù)語(yǔ)言把生成的語(yǔ)言放到相應(yīng)的文件夾下。
下面簡(jiǎn)單列一下調(diào)用代碼
1:PHP//設(shè)置IP,端口建立連接$socket = new TSocket('localhost', '9090');$transport = new TBufferedTransport($socket);//選定通信協(xié)議$protocol = new TBinaryProtocol($transport);$client = new DataAccessComponentClient($protocol); //生成的類2:c# serverint port = 9090;DataAccess da = new DataAccess();// ProcessorDataAccessComponent.Processor pc = new DataAccessComponent.Processor(da);// TransportTServerSocket tServerSocket =new TServerSocket(port);// Protocol factoryTProtocolFactory tProtocolFactory =new TBinaryProtocol.Factory();TServer serverEngine;// Simple ServerserverEngine = new TSimpleServer(pc, tServerSocket);// ThreadPool Server//serverEngine = new TThreadPoolServer(pc,tServerSocket);// Run itserverEngine.Serve();clientTTransport transport = new TSocket("localhost", 9090);TProtocol protocol = new TBinaryProtocol(transport);ThriftTest.Client client = new ThriftTest.Client(protocol);transport.Open();int val = client.test("1213");client.work();transport.Close();Console.WriteLine(val);Console.ReadLine(); 3:java servertry {//private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName());//System.setProperty("log4j.configuration", "log4j.properties");int port = 9090;// ProcessorThriftServer testHandler =new ThriftServer();ThriftTest.Processor testProcessor =new ThriftTest.Processor(testHandler);// TransportTServerSocket tServerSocket =new TServerSocket(port);// Protocol factoryTProtocolFactory tProtocolFactory =new TBinaryProtocol.Factory();TServer serverEngine;// Simple Server//serverEngine = new TSimpleServer(new Args(tServerSocket).processor(testProcessor));// ThreadPool ServerserverEngine = new TThreadPoolServer(new TThreadPoolServer.Args(tServerSocket).processor(testProcessor).protocolFactory(tProtocolFactory));//Set server event handlerserverEngine.setServerEventHandler(new TestServerEventHandler());// Run itSystem.out.println("Starting the server on port " + port + "...");serverEngine.serve();} catch (Exception x) {x.printStackTrace();}System.out.println("done.");總結(jié)
以上是生活随笔為你收集整理的使用thrift进行跨语言调用(php c# java)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 05-VTK在图像处理中的应用(2)
- 下一篇: ASP.NET 学习笔记_13 view