1. Preface
In fact, this article is about cross-process heterogeneous language calls. A simple example is to use PHP code to call the C # or java-written server. In fact, there are other methods besides the methods provided in this article, such as http + xml (json.
The significance of this article is to introduce thrift and record the problems I encountered during thrift debugging and corresponding solutions to avoid detours.
2: Rough Process
Thrift uses the socket + serialization protocol to complete cross-language calls. Similar Solutions include:ProtocolBuffer (Http://code.google.com/p/protobuf/)This performance is outstanding. I will test thrift performance later.
The process used is
A: define your own communication interface. The data types that can be used by the interface include string and int32. Of course, you can also define the enumeration and struct.
B: Use thrift.exe to generate the corresponding code.
C: Call
3: problems encountered during thrift debugging in c #, PHP, and Java
First we go to download thrift, the address is http://thrift.apache.org/
After decompression, you will see
Lib is the code of various languages executed.
A: PHP debugging considerations
You must register various php codes.
require_once __DIR__.'/lib/Thrift/ClassLoader/ThriftClassLoader.php';$loader = new ThriftClassLoader();$loader->registerNamespace('Thrift', __DIR__ . '/lib');$loader->register();
B: java debugging considerations
Download unprovided packages
The address is:
Http://commons.apache.org/proper/commons-lang/download_lang.cgi
Http://hc.apache.org/downloads.cgi
Http://www.slf4j.org/
3: demo
Define an interface (the method name cannot be the same)
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),}
Generate code
View Code
thrift -gen java ado.thriftthrift -gen php ado.thriftthrift -gen csharp ado.thrift
Place the generated language in the corresponding folder according to the language.
Below is a simple list of Call Code
1: PHP // set the IP address and establish a connection on the port $ socket = new TSocket ('localhost', '123'); $ transport = new TBufferedTransport ($ socket ); // select the communication protocol $ protocol = new TBinaryProtocol ($ transport); $ client = new DataAccessComponentClient ($ protocol); // generated Class 2: c # server int port = 9090; dataAccess da = new DataAccess (); // Processor DataAccessComponent. processor pc = new DataAccessComponent. processor (da); // Transport TServerSocket tServerSocket = new TServerSocket (port); // Protocol factory TProtocolFactory tProtocolFactory = new TBinaryProtocol. factory (); TServer serverEngine; // Simple Server serverEngine = new TSimpleServer (pc, tServerSocket); // ThreadPool Server // serverEngine = new TThreadPoolServer (pc, tServerSocket ); // Run it serverEngine. serve (); client TTransport transport = new TSocket ("localhost", 9090); TProtocol = 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: assumervertry {// 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); // Transport TServerSocket tServerSocket = new TServerSocket (port); // Protocol factory TProtocolFactory tProtocolFactory = new TBinaryProtocol. factory (); TServer serverEngine; // Simple Server // serverEngine = new TSimpleServer (new Args (tServerSocket ). processor (testProcessor); // ThreadPool Server serverEngine = new TThreadPoolServer (new TThreadPoolServer. args (tServerSocket ). processor (testProcessor ). protocolFactory (tProtocolFactory); // Set server event handler serverEngine. setServerEventHandler (new TestServerEventHandler (); // Run it System. out. println ("Starting the server on port" + port + "... "); serverEngine. serve ();} catch (Exception x) {x. printStackTrace ();} System. out. println ("done. ");