Thrift初探:簡單實現C#通信服務程序,收藏

Thrift初探:簡單實現C#通信服務程序

   很久沒有寫文章了,因爲換工做了,因此一直沒有時間來寫博。今天抽個空練練手下~最近接觸了下Thrift,網上也有不少文章對於Thrift作了說明:
      Thrift是一種可伸縮的跨語言服務框架,它結合了功能強大的軟件堆棧的代碼生成引擎,以建設服務,工做效率和無縫地與 C++,C#,Java,Python和PHP和Ruby結合。thrift容許你定義一個簡單的定義文件中的數據類型和服務接口。以做爲輸入文件,編譯 器生成代碼用來方便地生成RPC客戶端和服務器通訊的無縫跨編程語言。
      它的好處是什麼?固然是它支持大多數時下流行的語言。經過Thrift命令自動生成相應的語言腳本。而進行一些性能對比中,它的好處顯而易見。html

image

以上是傳輸相同的內容時內容大小的對比。java

image

以上是運行開銷比較結果。python

TCompactProtocol和TBinaryProtocol是Thrift支持的兩種協議,其中TCompactProtocol使用Variable-Length Quantity (VLQ) 編碼對數據進行壓縮。apache

詳細能夠查看:http://www.javabloger.com/article/apache-thrift-architecture.html編程

 

接下來,我想講述一下如何使用Thrift搭建C#版的客戶端以及服務端通信的程序。服務器

1. 先從官網下載Thrift安裝包以及簽出SVN源碼:框架

官網下載地址:http://thrift.apache.org/download/編程語言

這裏我下載了一個Thrift compiler for Windows版本的EXE文件(thrift-0.7.0.exe)svn

簽出SVN源碼地址:http://svn.apache.org/repos/asf/thrift/trunk性能

 

2. 這裏我利用文章(http://www.javabloger.com/article/thrift-java-code-example.html)的例子(該例子生成Java源碼的),完成一個C#版本的示例。

 

3. 首先建立腳本,命名爲textCsharp.thrift,腳本內容以下:

複製代碼
namespace  java com.javabloger.gen.code   #  註釋1

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


service ThriftCase {  #  註釋3 
    i32 testCase1(
1 :i32 num1,  2 :i32 num2,  3 : string   num3)  #  註釋4

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

    
void  testCase3()

   
void  testCase4( 1 :list < Blog >  blog)  
}
複製代碼

 

4. 執行thrift命令:thrift -gen csharp testCsharp.thrift,這裏說明一下:參數"csharp」意味着這裏將自動生成C#代碼,若是這裏寫java,python等等,能夠用"java"或者"py」代替。

因而獲得gen-csharp的目錄,這個目錄裏面就包含支持Thrift的Blog以及ThriftCase的源代碼,具體裏面都生成什麼代碼,後面會作出介紹。

image

 

5. 而後,我如今打開SVN源碼中的 trunk\lib\csharp\ 路徑,我用項目打開

image

編譯後,獲得Thrift.dll文件,爲了後面使用Thrift作準備。

 

6.新建工程,添加Server以及Client項目,把剛纔生成的代碼文件放入Common項目中。讓Client和Server項目引用Thrift.dll類庫。

image

 

7. 編寫服務端程序:

複製代碼
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(); 
    } 
複製代碼

其中BusinessImpl具體提供業務邏輯的實現:

複製代碼
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.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 " ); 
        } 
    }
複製代碼

讓它繼承ThriftCase.Iface接口。

 

8. 編寫客戶端程序:

複製代碼
  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. 運行Server以及Client:

image

從客戶端調用的方法,服務端已經接收到了數據。

 

源代碼下載:ThriftCSharp.rar

後面的文章,我會具體介紹下Thrift的工做機制和原理。

相關文章
相關標籤/搜索