Thrift初探:簡單實現C#通訊服務程式,收藏
Thrift初探:簡單實現C#通訊服務程式
好久沒有寫文章了,由於換工作了,所以一直沒有時間來寫博。今天抽個空練練手下~最近接觸了下Thrift,網上也有很多文章對於Thrift做了說明:
Thrift是一種可伸縮的跨語言服務框架,它結合了功能強大的軟體堆疊的程式碼生成引擎,以建設服務,工作效率和無縫地與 C++,C#,Java,Python和PHP和Ruby結合。thrift允許你定義一個簡單的定義檔案中的資料型別和服務介面。以作為輸入檔案,編譯 器生成程式碼用來方便地生成RPC客戶端和伺服器通訊的無縫跨程式語言。
它的好處是什麼?當然是它支援大多數時下流行的語言。通過Thrift命令自動生成相應的語言指令碼。而進行一些效能對比中,它的好處顯而易見。
以上是傳輸相同的內容時內容大小的對比。
以上是執行開銷比較結果。
TCompactProtocol和TBinaryProtocol是Thrift支援的兩種協議,其中TCompactProtocol使用Variable-Length Quantity (VLQ) 編碼對資料進行壓縮。
詳細可以檢視: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原始碼地址: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 javacom.javabloger.gen.code#註釋1struct Blog{#註釋2
1 : string topic
2 :binarycontent
3 :i64createdTime
4 : string id
5 : string ipAddress
6 :map < string , string > props
}
serviceThriftCase{#註釋3
i32testCase1( 1 :i32num1, 2 :i32num2, 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的原始碼,具體裡面都生成什麼程式碼,後面會做出介紹。
5. 然後,我現在開啟SVN原始碼中的 trunk\lib\csharp\ 路徑,我用專案開啟
編譯後,得到Thrift.dll檔案,為了後面使用Thrift做準備。
6.新建工程,新增Server以及Client專案,把剛才生成的程式碼檔案放入Common專案中。讓Client和Server專案引用Thrift.dll類庫。
7. 編寫服務端程式:
public class Server{
public void Start()
{
TServerSocketserverTransport = new TServerSocket( 7911 , 0 , false );
ThriftCase.Processorprocessor = new ThriftCase.Processor( new BusinessImpl());
TServerserver = new TSimpleServer(processor,serverTransport);
Console.WriteLine( " Startingserveronport7911... " );
server.Serve();
}
}
其中BusinessImpl具體提供業務邏輯的實現:
public class BusinessImpl:ThriftCase.Iface{
public int testCase1( int num1, int num2,Stringnum3)
{
int i = num1 + num2;
Console.Write( " testCase1num1+num2is: " + i);
Console.WriteLine( " num3is: " + num3);
return i;
}
public List < String > testCase2(Dictionary < String,String > num1)
{
Console.WriteLine( " testCase2num1: " + 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 ++ )
{
Blogblog = blogs[i];
Console.Write( " id: " + blog.Id);
Console.Write( " ,IpAddress: " + blog.IpAddress);
// Console.Write(",Content:"+newString(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)
{
TTransporttransport = new TSocket( " localhost " , 7911 );
TProtocolprotocol = new TBinaryProtocol(transport);
ThriftCase.Clientclient = new ThriftCase.Client(protocol);
transport.Open();
Console.WriteLine( " Clientcalls..... " );
map.Add( " blog " , " http://www.javabloger.com%22);/
client.testCase1( 10 , 21 , " 3 " );
client.testCase2(map);
client.testCase3();
Blogblog = new Blog();
// blog.setContent("thisisblogcontent".getBytes());
blog.CreatedTime = DateTime.Now.Ticks;
blog.Id = " 123456 " ;
blog.IpAddress = " 127.0.0.1 " ;
blog.Topic = " thisisblogtopic " ;
blogs.Add(blog);
client.testCase4(blogs);
transport.Close();
Console.ReadKey();
}
}
9. 執行Server以及Client:
從客戶端呼叫的方法,服務端已經接收到了資料。
原始碼下載:ThriftCSharp.rar
後面的文章,我會具體介紹下Thrift的工作機制和原理。
分類: C#.net
http://www.cnblogs.com/hanmos/archive/2011/09/15/2177891.html
轉載於:https://my.oschina.net/cclove/blog/66438