Thrift is a scalable cross-language service framework that combines a powerful software stack code generation engine to build services, work efficiently and seamlessly with C++,c#,java,python and PHP and Ruby. Thrift allows you to define a simple definition of the data type in the file and the service interface. As an input file, the compiler generates code to easily build a seamless cross-programming language for RPC client and server communication. What are the benefits of it? Of course it supports most popular languages nowadays. The corresponding language script is automatically generated by the thrift command. For some performance comparisons, the benefits are obvious.
The above is the comparison of the content size when transmitting the same content.
The above is the running cost comparison result.
Tcompactprotocol and Tbinaryprotocol are two protocols supported by thrift, where Tcompactprotocol uses Variable-length Quantity (VLQ) encoding to compress data.
Details can be viewed: http://www.javabloger.com/article/apache-thrift-architecture.html
Next, I want to tell you how to use thrift to build a C # version of the client and server-side communication programs.
1. Download the Thrift installation package and check out the SVN source code from the official website first:
Official website: http://thrift.apache.org/download/
Here i download a thrift compiler for Windows version of EXE file (thrift-0.7.0.exe)
Check out SVN source address: Http://svn.apache.org/repos/asf/thrift/trunk
2. Here I use the article (http://www.javabloger.com/article/thrift-java-code-example.html) example, which generates Java source code, to complete a C # version of the example.
3. First create the script, named Textcsharp.thrift, the script content is as follows:
namespace Java com.javabloger.gen.code # Comment 1struct Blog { # NOTE 2 1:string topic 2:binary Content 3:i64 createdtime 4:string ID 5:string ipAddress 6:map<string,string> Props }service thriftcase { # Note 3 i32 testCase1 (1:i32 num1, 2:i32 num2, 3:string num3) # Note 4 list<string> testCase2 (1:map<string,string> num1) void TestCase3 () void TestCase4 (1:list<blog> Blog) }
4. Execute thrift command: Thrift-gen CSharp Testcsharp.thrift, here is a description: the parameter "CSharp" means that the C # code will be generated automatically here, if you write Java,python and so on, you can use "Java" or " Py "instead.
So get Gen-csharp directory, this directory contains Support thrift blog and Thriftcase source code, the concrete inside all generated what code, later will make introduction.
5. Then I open the trunk\lib\csharp\ path in the SVN source and I open it with the project
After compiling, get Thrift.dll file, in order to use thrift to prepare later.
6. Create a new project, add the server and the client project, and put the code file you just generated into the common project. have client and server projects reference the Thrift.dll class library.
7. Write the service-side program:
public class Server {public void Start () { Tserversocket servertransport = new Tserversocket (7911, 0, false) ; Thriftcase.processor Processor = new Thriftcase.processor (new Businessimpl ()); Tserver Server = new Tsimpleserver (processor, servertransport); Console.WriteLine ("Starting server on port 7911 ..."); Server. Serve ();
Where Businessimpl specifically provides the implementation of business logic:
public class BusinessImpl:ThriftCase.Iface {public int testCase1 (int num1, int num2, String num3) { int i = NUM1 + num2; Console.Write ("TestCase1 num1+num2 is:" + i); Console.WriteLine ("Num3 is:" + num3); return i; } public list<string> TestCase2 (dictionary<string, string> num1) {Console.writelin E ("TestCase2 num1:" + num1); list<string> list = new list<string> (); List. ADD ("Num1"); return list; } public void TestCase3 () {Console.WriteLine ("TestCase3 ..." + datetime.now); } public void TestCase4 (list<blog> blogs) {Console.WriteLine ("TestCase4 ..."); for (int i = 0; i < blogs. Count; i++) {Blog blog = blogs[i]; Console.Write ("ID:" + blog.) ID); Console.Write (", IpAddress:" + blog.) IpAddress); Console.Write (", Content:" + new String (blog). Content)); Console.Write (", topic:" + blog.) TOPIC); Console.Write (", Time:" + blog.) Createdtime); } Console.WriteLine ("\ n"); } }
Let it inherit the Thriftcase.iface interface.
8. Write the client program:
Class Client {static dictionary<string, string> map = new dictionary<string, string> (); Static List<blog> blogs = new list<blog> (); static void Main (string[] args) {Ttransport transport = new Tsocket ("localhost", 7911); Tprotocol protocol = new Tbinaryprotocol (transport); Thriftcase.client Client = new thriftcase.client (protocol); Transport. Open (); Console.WriteLine ("Client calls ..."); Map. ADD ("blog", "Http://www.javabloger.com%22");/Client.testcase1 (10, 21, "3"); CLIENT.TESTCASE2 (map); CLIENT.TESTCASE3 (); Blog blog = new Blog (); Blog.setcontent ("This is blog content". GetBytes ()); Blog. Createdtime = DateTime.Now.Ticks; Blog. Id = "123456"; Blog. IpAddress = "127.0.0.1"; Blog. Topic = "This is blog Topic"; Blogs. ADD (blog); CLIENT.TESTCASE4 (blogs); Transport. Close (); Console.readkey (); } }
9. Run Server and client:
The method that is called from the client, the server has received the data.
Source code Download: Thriftcsharp.rar
Thrift Simple implementation of C # Communication Service Program (cross-language MicroServices)