Communication Framework Analysis -- Google protobuf vs Facebook thirft

Source: Internet
Author: User
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

  1. Namespace CPP Zam. Thrift. Test

  2. Namespace py thrifttest

  3. Namespace Java com. Zam. Thrift. Test

  4. Namespace PHP thrifttest

  5. Service Hello {

  6. String hellostring (1: String word)

  7. }

4) Compile the interface implementation code

  1. Package com. Zam. server;

  2. Import org. Apache. Thrift. texception;

  3. Import com. Zam. Thrift. Test. Hello. iface;

  4. Public class helloimpl implements iface {

  5. Private Static int COUNT = 0;

  6. @ Override

  7. Public String hellostring (string word) throws texception {

  8. // Todo auto-generated method stub

  9. Count + = 1;

  10. System. Out. println ("get" + word + "" + count); Return "hello" + word + "" + count;

  11. }

  12. }

5) Write server code

  1. Package com. Zam. server;

  2. Import org. Apache. Thrift. Protocol. tbinaryprotocol;

  3. Import org. Apache. Thrift. Protocol. tbinaryprotocol. factory;

  4. Import org. Apache. Thrift. server. tserver;

  5. Import org. Apache. Thrift. server. tthreadpoolserver;

  6. Import org. Apache. Thrift. server. tthreadpoolserver. ARGs;

  7. Import org. Apache. Thrift. Transport. tserversocket;

  8. Import org. Apache. Thrift. Transport. ttransportexception;

  9. Import com. Zam. Thrift. Test. Hello;

  10. Import com. Zam. Thrift. Test. Hello. processor;

  11. Public class server {

  12. Public void startserver (){

  13. Try {

  14. System. Out. println ("Thrift server opened port 1234 ");

  15. Tserversocket servertransport = new tserversocket (1234 );

  16. Hello. processor process = new processor (New helloimpl ());

  17. Factory portfactory = new tbinaryprotocol. Factory (True, true );

  18. ARGs = new ARGs (servertransport );

  19. Args. processor (process );

  20. Args. protocolfactory (portfactory );

  21. Tserver Server = new tthreadpoolserver (ARGs );

  22. Server. Serve ();

  23. }

  24. Catch (ttransportexception e ){

  25. E. printstacktrace ();

  26. }

  27. }

  28. Public static void main (string [] ARGs ){

  29. System. Out. println ("Thrift server init ");

  30. Server = new server ();

  31. System. Out. println ("Thrift Server start ");

  32. Server. startserver ();

  33. System. Out. println ("Thrift server end ");

  34. }

  35. }

6) write client code

  1. Package com. Zam. server;

  2. Import org. Apache. Thrift. texception;

  3. Import org. Apache. Thrift. Protocol. tbinaryprotocol;

  4. Import org. Apache. Thrift. Protocol. tprotocol;

  5. Import org. Apache. Thrift. Transport. tsocket;

  6. Import org. Apache. Thrift. Transport. tTransport;

  7. Import org. Apache. Thrift. Transport. ttransportexception;

  8. Import com. Zam. Thrift. Test. Hello;

  9. Public class client {

  10. Public void startclient (){

  11. TTransport transport;

  12. Try {

  13. System. Out. println ("Thrift client connext server At 1234 port ");

  14. Transport = new tsocket ("localhost", 1234 );

  15. Tprotocol protocol = new tbinaryprotocol (transport );

  16. Hello. Client client = new HELLO. Client (Protocol );

  17. Transport. open ();

  18. System. Out. println (client. hellostring ("panguso "));

  19. Transport. Close ();

  20. System. Out. println ("Thrift client close connextion ");

  21. } Catch (ttransportexception e ){

  22. E. printstacktrace ();

  23. } Catch (texception e ){

  24. E. printstacktrace ();

  25. }

  26. }

  27. Public static void main (string [] ARGs ){

  28. System. Out. println ("Thrift client init ");

  29. Client client = new client ();

  30. System. Out. println ("Thrift client start ");

  31. Client. startclient ();

  32. System. Out. println ("Thrift client end ");

  33. }

  34. }

8) run the server and client code

8.1) Start the server

  1. Thrift server init

  2. Thrift Server start

  3. Thrift server open port 1234

8.2) Start the client

  1. Thrift client init

  2. Thrift client start

  3. Thrift client connext server At 1234 Port

  4. Hello panguso 1

  5. Thrift client close connextion

  6. Thrift client end


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.