Apache Thrift 配置与简单实例——C# C/S 通信
2016-04-14 14:56
681 查看
Thrift 下载
去官网 https://thrift.apache.org/download 下载两个文件:thrift-0.9.3.exe
thrift-0.9.3.tar.gz
前者用于将 .thrift 文件编译成其他语言文件(如 .cs, .cpp 等),后者是源代码,用于生成不同语言的 thrift 库文件。
下载完成后:
将 thrift-0.9.3.exe 文件重命名为 thrift.exe 并存储在 C 盘新建的文件夹Thrift 下,将 C:/Thrift 加入环境变量,重启电脑使配置生效
将 thrift-0.9.3.tar.gz 解压,进入 lib/csharp/src 文件夹,使用 Visual Studio 打开 Thrift.sln 文件,编译。完成后可在 bin/Debug 中找到 Thrift.dll 文件,就是我们每次使用 Thrift 必须加载的库文件。(提示:编译时可能会弹框提示需要 .net framework 3.5 的库,按照提示链接去官网 https://www.microsoft.com/en-us/download/details.aspx?id=22 下载安装即可)
编写 Thrift 脚本
假设服务端(Server)为客户端(Client)提供简单的数学运算功能。编写 demo.thrift 脚本如下:namespace csharp MathClient namespace csharp MathServer service MathService{ i32 add (1:i32 a, 2:i32 b), i32 sub (1:i32 a, 2:i32 b), i32 mul (1:i32 a, 2:i32 b), i32 div (1:i32 a, 2:i32 b), i32 mod (1:i32 a, 2:i32 b) }
从控制台进入 demo.thrift 的目录后,编译该文件为 C# 代码:
thrift --gen csharp demo.thrift
随后会生成一个 gen-csharp 的文件夹,其中包括 MathService.cs 文件,该文件需要同时导入到 Server 和 Client 的程序中,下面将详细说明。
如果是两种语言,如 Java 和 C# 通信的话,需要 –gen csharp 和 –gen java 分别编译一次,将得到的 .cs 和 .class 文件分别导入到两个项目中。
Server 程序
在 Visual Studio 中新建 C# Console Application 程序:ThriftServer。将 Thrift.dll. 导入到 Reference 中,同时将之前生成的 MathService.cs 导入项目中,并在 Program.cs 中包含:using Thrift.Server; using Thrift.Transport;
新建一个类 MathServer.cs (注意不是 MathService.cs),实现 MathService.Iface 接口:
public class MathServer : MathService.Iface { public MathServer() { } public int add(int a, int b) { Console.WriteLine("Called add({0},{1})={2}", a, b, a + b); return a + b; } public int sub(int a, int b) { Console.WriteLine("Called sub({0},{1})={2}", a, b, a - b); return a + b; } public int mul(int a, int b) { Console.WriteLine("Called mul({0},{1})={2}", a, b, a * b); return a + b; } public int div(int a, int b) { Console.WriteLine("Called div({0},{1})={2}", a, b, a / b); return a / b; } public int mod(int a, int b) { Console.WriteLine("Called mod({0},{1})={2}", a, b, a % b); return a % b; } }
然后修改 Program.cs 中的 Main 函数为:
static void Main(string[] args) { try { MathServer handler = new MathServer(); MathService.Processor processor = new MathService.Processor(handler); TServerTransport serverTransport = new TServerSocket(9095); TServer server = new TSimpleServer(processor, serverTransport); Console.WriteLine("Starting the server..."); server.Serve(); } catch (Exception x) { Console.WriteLine(x.StackTrace); } Console.WriteLine("done."); }
这样 ThriftServer 项目就可以运行了。它开启 9095 端口(可以自设),然后等待 Client 与其通信。
Client 端
同样新建一个C#控制台项目:ThriftClient,并将 Thrift.dll 和 MathService.cs 导入到项目中,在Program.cs 中包含:using Thrift.Transport; using Thrift.Protocol;
修改 Program.cs 中的 Main 函数如下:
static void Main(string[] args) { TTransport transport = new TSocket("localhost", 9095); transport.Open(); TProtocol protocal = new TBinaryProtocol(transport); MathService.Client client = new MathService.Client(protocal); Console.WriteLine(client.add(10, 20)); Console.WriteLine(client.sub(10, 20)); Console.WriteLine(client.mul(10, 20)); Console.WriteLine(client.div(10, 20)); Console.WriteLine(client.mod(10, 20)); }
运行
首先运行 MathServer,可以看到:然后运行 MathClient,可以看到:
同时 MathServer 的控制台显示:
完!
参考
详细介绍 Apache Thrift - 可伸缩的跨语言服务开发框架(Java):http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/相关文章推荐
- org.apache.commons.lang.StringUtils的jar包是什么
- xmapp 的apache mpm
- apache-jmeter学习文档
- commons-codec使用介绍
- Exception: org.apache.ibatis.builder.xml.IncompleteStatementException: Could not find result map....
- Mac下的Apache(二)问题收集
- Apache2.4.16+SVN1.7.9+svnmanager1.10 升级 (三)
- org.apache.commons.collections.CollectionUtils
- org.apache.xmlbeans.XmlOptions.setLoadEntityBytesL
- apache lucene solr 官网历史版本下载地址
- 关于wampserver 安装时候vcruntime140.dll 缺失
- 搭建storm集群(apache-storm-0.9.5.tar.gz) .
- DedeAMPZ 是快速配置php+mysql环境的一个整合套件,包含php5.2、Apache2.2、MySql5,下载地址:
- Ubuntu14.04下安装与配置Apache服务器
- org.apache.commons.dbutils.DbUtils
- Apache Lucene(全文检索引擎)—创建索引
- phpstudy APACHE支持.htaccess以及 No input file specified解决方案
- commons-cli使用介绍
- Tomacat7启动报错-org.apache.catalina.deploy.WebXml addFilter
- linux 下 apache启动、停止、重启命令