Protobuf
Please refer to the following articleHttp://blog.csdn.net/hguisu/article/details/20721109
Configure thrift development environment in Windows
1) install thrift: Download The exefile to thrift.exe and copy the file to the C: \ Windows Directory (or any directory). Then, you can use it in the DOS environment.
C: \ Windows> thrift-gen Java D: \ mywork \ javaproject \ thrifttest \ test. thrift: the output Java file is output to c: \ windows in the current directory by default. You can also use the-O parameter to specify the output path.
2) download the dependent package
2.1) libthrift. jar,: http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.9.0/
2.2) slf4j-api.jar
2.3) slf4j-simple.jar
3) Compile the thrift interface file
-
Namespace CPP Zam. Thrift. Test
-
Namespace py thrifttest
-
Namespace Java com. Zam. Thrift. Test
-
Namespace PHP thrifttest
-
-
Service Hello {
-
String hellostring (1: String word)
-
}
4) Compile the interface implementation code
-
Package com. Zam. server;
-
Import org. Apache. Thrift. texception;
-
Import com. Zam. Thrift. Test. Hello. iface;
-
Public class helloimpl implements iface {
-
Private Static int COUNT = 0;
-
@ Override
-
Public String hellostring (string word) throws texception {
-
// Todo auto-generated method stub
-
Count + = 1;
-
System. Out. println ("get" + word + "" + count); Return "hello" + word + "" + count;
-
}
-
}
5) Write server code
-
Package com. Zam. server;
-
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 com. Zam. Thrift. Test. Hello;
-
Import com. Zam. Thrift. Test. Hello. processor;
-
Public class server {
-
Public void startserver (){
-
Try {
-
System. Out. println ("Thrift server opened port 1234 ");
-
Tserversocket servertransport = new tserversocket (1234 );
-
Hello. processor process = new processor (New helloimpl ());
-
Factory portfactory = new tbinaryprotocol. Factory (True, true );
-
ARGs = new ARGs (servertransport );
-
Args. processor (process );
-
Args. protocolfactory (portfactory );
-
Tserver Server = new tthreadpoolserver (ARGs );
-
Server. Serve ();
-
}
-
Catch (ttransportexception e ){
-
E. printstacktrace ();
-
}
-
}
-
-
Public static void main (string [] ARGs ){
-
System. Out. println ("Thrift server init ");
-
Server = new server ();
-
System. Out. println ("Thrift Server start ");
-
Server. startserver ();
-
System. Out. println ("Thrift server end ");
-
}
-
}
6) write client code
-
Package com. Zam. server;
-
-
Import org. Apache. Thrift. texception;
-
Import org. Apache. Thrift. Protocol. tbinaryprotocol;
-
Import org. Apache. Thrift. Protocol. tprotocol;
-
Import org. Apache. Thrift. Transport. tsocket;
-
Import org. Apache. Thrift. Transport. tTransport;
-
Import org. Apache. Thrift. Transport. ttransportexception;
-
-
Import com. Zam. Thrift. Test. Hello;
-
Public class client {
-
Public void startclient (){
-
TTransport transport;
-
Try {
-
System. Out. println ("Thrift client connext server At 1234 port ");
-
Transport = new tsocket ("localhost", 1234 );
-
Tprotocol protocol = new tbinaryprotocol (transport );
-
Hello. Client client = new HELLO. Client (Protocol );
-
Transport. open ();
-
System. Out. println (client. hellostring ("panguso "));
-
Transport. Close ();
-
System. Out. println ("Thrift client close connextion ");
-
} Catch (ttransportexception e ){
-
E. printstacktrace ();
-
} Catch (texception e ){
-
E. printstacktrace ();
-
}
-
}
-
-
Public static void main (string [] ARGs ){
-
System. Out. println ("Thrift client init ");
-
Client client = new client ();
-
System. Out. println ("Thrift client start ");
-
Client. startclient ();
-
System. Out. println ("Thrift client end ");
-
}
-
}
8) run the server and client code
8.1) Start the server
-
Thrift server init
-
Thrift Server start
-
Thrift server open port 1234
8.2) Start the client
-
Thrift client init
-
Thrift client start
-
Thrift client connext server At 1234 Port
-
Hello panguso 1
-
Thrift client close connextion
-
Thrift client end