Thrift 简单实现C#通讯服务程序 (跨语言 MicroServices)
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 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的源代码,具体里面都生成什么代码,后面会做出介绍。

5. 然后,我现在打开SVN源码中的 trunk\lib\csharp\ 路径,我用项目打开

编译后,得到Thrift.dll文件,为了后面使用Thrift做准备。
6.新建工程,添加Server以及Client项目,把刚才生成的代码文件放入Common项目中。让Client和Server项目引用Thrift.dll类库。

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:

从客户端调用的方法,服务端已经接收到了数据。
源代码下载:ThriftCSharp.rar
Thrift 简单实现C#通讯服务程序 (跨语言 MicroServices)的更多相关文章
- Thrift初探:简单实现C#通讯服务程序
Thrift是一种可伸缩的跨语言服务框架,它结合了功能强大的软件堆栈的代码生成引擎,以建设服务,工作效率和无缝地与C++,C#,Java,Python和PHP和Ruby结合.thrift允许你定义一个 ...
- thrift框架总结,可伸缩的跨语言服务开发框架
thrift框架总结,可伸缩的跨语言服务开发框架 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Service,基于 JSON 消息格式的 RESTful 服务等.其 ...
- Thrift实现C#通讯服务程序
Thrift初探:简单实现C#通讯服务程序 好久没有写文章了,由于换工作了,所以一直没有时间来写博.今天抽个空练练手下~最近接触了下Thrift,网上也有很多文章对于Thrift做了说明: ...
- Golang、Php、Python、Java基于Thrift0.9.1实现跨语言调用
目录: 一.什么是Thrift? 1) Thrift内部框架一瞥 2) 支持的数据传输格式.数据传输方式和服务模型 3) Thrift IDL 二.Thrift的官方网站在哪里? 三.在哪里下载?需要 ...
- Golang通过Thrift框架完美实现跨语言调用
每种语言都有自己最擅长的领域,Golang 最适合的领域就是服务器端程序. 做为服务器端程序,需要考虑性能同时也要考虑与各种语言之间方便的通讯.采用http协议简单,但性能不高.采用TCP通讯,则需要 ...
- Apache Thrift 跨语言服务开发框架
Apache Thrift 是一种支持多种编程语言的远程服务调用框架,由 Facebook 于 2007 年开发,并于 2008 年进入 Apache 开源项目管理.Apache Thrift 通过 ...
- Apache Thrift - 可伸缩的跨语言服务开发框架
To put it simply, Apache Thrift is a binary communication protocol 原文地址:http://www.ibm.com/developer ...
- 基于Thrift的跨语言、高可用、高性能、轻量级的RPC框架
功能介绍 跨语言通信 方便的使Java.Python.C++三种程序可以相互通信 负载均衡和容灾处理 方便的实现任务的分布式处理 支持服务的水平扩展,自动发现新的服务节点 能够兼容各种异常情况,如节点 ...
- 使用thrift进行跨语言调用(php c# java)
使用thrift进行跨语言调用(php c# java) 1:前言 实际上本文说的是跨进程的异构语言调用,举个简单的例子就是利用PHP写的代码去调C#或是java写的服务端.其实除了本文提供的办法 ...
随机推荐
- poj 1664 放苹果(递推)
题目链接:http://poj.org/problem? id=1664 放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- 再次编译 arm toolchains
为什么说再呢,因为已经好多次了.每次失败,都再从失败的地方开始.今天这篇呢,主要是记录今天的进展. 1. 编译要分三步走 之前学习的时候就有印象,要三步走.但是因为没有实践过,所以,忘差不多了.所谓三 ...
- J2EE开发之三种项目架构
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6285069.html 在我们开发项目时,一般都要先划分好哪些是与用户交互的,哪些用来处理请求/数据等等,这些过 ...
- Atitit.Guibutton与面板---项目规模的评估----文件数统计,结构,代码行数,每类型文件行数.
Atitit.Guibutton与面板---项目规模的评估----文件数统计,结构,代码行数,每类型文件行数. 1. Kpi::: 代码行数(凝视行数,空白的行数), 方法数,class数 1 2. ...
- 如何导出标准模板库(STL)类的实例化和包含STL类对象数据成员的类
本文翻译自 https://support.microsoft.com/zh-cn/help/168958/how-to-export-an-instantiation-of-a-standard-t ...
- aapt 命令可应用于查看apk包名、主activity、版本等很多信息
aapt即Android Asset Packaging Tool,在SDK的build-tools目录下,本文小结了一下该工具的用法. 配置环境变量后可直接在cmd使用该命令 http://blog ...
- 删数问题(NOI94)
删数问题(NOI94) 输入一个高精度的正整数N,去掉其中任意S个数字后剩下的数字按原左右次序组成一个新的正整数.编程对给定的N和S,寻找一种方案使得剩下的数字组成的新数最小.输出新的正整数.(N不超 ...
- Swift 导航栏设置图片点击事件,图片蓝色的解决方案
如果导航栏想做一个点击事件,正好是一个图片 我们可以直接这样: self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIIm ...
- HDUOJ-------单词数
单词数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- java WSDL接口webService实现方式
一.使用JDK生成WSDL的对象类 1.cmd进入JDK的bin文件中 执行命令 wsimport -keep -p com.demo.client http://localhost:8080/Dem ...