Thrift: simple implementation of C # communication service programs

Source: Internet
Author: User

I haven't written any articles for a long time. Because I have changed my job, I have never had time to write blogs. Today, I have an empty trainer ~ Thrift has recently been introduced, and many articles on the Internet have explained Thrift:
Thrift is a scalable cross-language service framework that combines powerful software stack code generation engines to build services, work efficiency, and seamlessly with C ++, C #, Java, Python, PHP, and Ruby. Thrift allows you to define data types and service interfaces in a simple definition file. As an input file, the compiler generates code to easily generate a seamless cross-programming language for RPC client and server communication.
What are its advantages? Of course, it supports most popular languages. Use the Thrift command to automatically generate the corresponding language script. In some performance comparisons, its benefits are obvious.

The above is a comparison of the content size when the same content is transmitted.

The above is the result of running overhead comparison.

TCompactProtocol and TBinaryProtocol are two protocols supported by Thrift, where TCompactProtocol uses Variable-Length Quantity (VLQ) encoding to compress data.

See: http://www.javabloger.com/article/apache-thrift-architecture.html for details

 

Next, I would like to explain how to use Thrift to build the C # client and the server communication program.

1. Download the Thrift installation package from the official website and check out the SVN source code:

Official Website: http://thrift.apache.org/download/

Here I downloaded an exefile (thrift-0.7.0.exe) for Thrift compiler for windows.pdf)

Checkout 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 the Java Source Code) to complete a C # version example.

 

3. Create a script named textCsharp. thrift. The script content is as follows:

Namespace java com. javabloger. gen. code # Note 1

Struct Blog {# comment 2
1: string topic
2: binary content
3: i64 createdTime
4: string id
5: string ipAddress
6: map <string, string> props
}

Service thritcase {# comment 3
I32 testCase1 (1: i32 num1, 2: i32 num2, 3: string num3) # comment 4

List <string> testCase2 (1: map <string, string> num1)

Void testCase3 ()

Void testCase4 (1: list <Blog> blog)
}

 

4. run the thrift command: thrift-gen csharp testCsharp. thrift: the "csharp" parameter indicates that C # code is automatically generated here. If java and python are written here, they can be replaced by "java" or "py.

The gen-csharp directory contains the blogs that support Thrift and the source code of thritcase. What code is generated in this directory will be introduced later.

 

5. Now I open the trunk \ lib \ csharp \ path in SVN source code.

After compilation, obtain the Thrift. dll file to prepare for using Thrift later.

 

6. Create a project, add the Server and Client projects, and put the generated code file into the Common project. Let the Client and Server project reference the Thrift. dll class library.

 

7. Write the server program:

Public class Server
{
Public void Start ()
{
TServerSocket serverTransport = new TServerSocket (7911, 0, false );
Thritcase. Processor processor = new ThriftCase. Processor (new BusinessImpl ());
TServer server = new TSimpleServer (processor, serverTransport );
Console. WriteLine ("Starting server on port 7911 ...");
Server. Serve ();
}
}

BusinessImpl provides specific implementation of business logic:

Public class BusinessImpl: thrifle tcase. 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. WriteLine ("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 ");
}
}

Inherit the thritcase. Iface interface.

 

8. Compile 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 );
Thritcase. Client client = new thritcase. Client (protocol );
Transport. Open ();
Console. WriteLine ("Client CILS .....");
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;
Blogging. 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 called by the client has received data.

 

Source code download: thrifle tcsharp.rar

In the following article, I will introduce the working mechanism and principle of Thrift.

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.