转载至 https://headsigned.com/posts/csharp-apache-thrift-demo/

This demo application shows how to implement a simple Apache Thrift client/server in C#.

Thrift is a software framework that enables creation of services that can be served and consumed by many different languages. This article is not a tutorial for Thrift, nor for the Thrift interface definition language - these you can find on the official tutorial pages.

You can download the source code or check it out at GitHub. Visual C# 2010 Express was used to create the solution.

How the demo works

There are 3 projects in the solution - Common, Server, and Client. Project ‘Common’ contains code that is shared by both client and server. This includes the code generated by the thrift compiler from the demo-interface.thrift file:

// demo-interface.thrift

namespace * Common.ThriftInterface

struct BookInfo
{
1: i32 Id,
2: string Author,
3: string Title
} service LibraryService
{
list<BookInfo> GetAllBooks();
BookInfo GetBook(1: i32 bookId);
}

The Thrift compiler will compile this code into two classes: BookInfo and LibraryService. This is done as a part of the Common project pre-build step by invoking the Vendor\Thrift.0.9.1.3\tools\thrift-0.9.1.exe.

Server

In order to expose the LibraryService, LibraryServiceHandler implements the generated LibraryService.Iface:

// LibraryServiceHandler.cs

internal class LibraryServiceHandler : LibraryService.Iface
{
// <snip> public List<BookInfo> GetAllBooks() { /* <snip> */ }
public BookInfo GetBook(int bookId) { /* <snip> */ }
}

An instance of this handler is passed to the LibraryService.Processor, and is served by starting a Thrift server:

// Server

var handler = new LibraryServiceHandler();
var processor = new LibraryService.Processor(handler); TServerTransport transport = new TServerSocket();
TServer server = new TThreadPoolServer(processor, transport); server.Serve();

Client

Connecting a client and consuming the service is also quite simple. You only need to know the address, port, and protocol of the server. In the demo application, out TThreadPoolServer uses the default protocol, which is binary:

// Client

var transport = new TSocket("localhost", );
var protocol = new TBinaryProtocol(transport);
var client = new LibraryService.Client(protocol); transport.Open(); var allBooks = client.GetAllBooks(); // Actual Thrift call
var firstBook = client.GetBook(allBooks.First().Id); // Actual Thrift call

This simple client/server demo shows how easy it is to get started with Thrift and C#. To learn more, check out the missing guide.

C# Apache Thrift Demo的更多相关文章

  1. Apache thrift RPC 双向通信

    在上一篇介绍Apache thrift 安装和使用,写了一个简单的demo,讲解thrift服务的发布和客户端调用,但只是单向的客户端发送消息,服务端接收消息.而客户端却得不到服务器的响应. 在不涉及 ...

  2. Apache Thrift 服务开发框架学习记录

    Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架. 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Servic ...

  3. Apache Thrift - 可伸缩的跨语言服务开发框架

    To put it simply, Apache Thrift is a binary communication protocol 原文地址:http://www.ibm.com/developer ...

  4. Apache Thrift学习之二(基础及原理)

    Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Java 开发人员角度详细介绍 Apache Thrift 的架构.开发和部署,并且 ...

  5. Apache Thrift学习之一(入门及Java实例演示)

    目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...

  6. Apache Thrift入门(安装、测试与java程序编写)

    安装Apache Thrift ubuntu linux运行: #!/bin/bash #下载 wget http://mirrors.cnnic.cn/apache/thrift/0.9.1/thr ...

  7. jdbc链接hive报错:java.lang.ClassNotFoundException: org.apache.thrift.transport.TTransport

    写了个jdbc连接hive2的demo,结果报错:java.lang.ClassNotFoundException: org.apache.thrift.transport.TTransport,实际 ...

  8. 【Java】分布式RPC通信框架Apache Thrift 使用总结

    简介 Apache Thrift是Facebook开源的跨语言的RPC通信框架,目前已经捐献给Apache基金会管理,由于其跨语言特性和出色的性能,在很多互联网公司得到应用,有能力的公司甚至会基于th ...

  9. Apache Thrift使用简介

    Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.和其它RPC框架相比,它主要具有如下连个特点: 高性能. 它采用的是二进制序列化,并且用的是长 ...

随机推荐

  1. Word 中实现公式居中编号右对齐 -- 含视频教程(9)

    1. 两种方法 不管你用「Word 自带公式」还是「Mathtype」,一般来说,Word 中实现公式居中编号右对齐的方法有两种.(1):表格法:(2):制表位. 2. 方法1:表格法 >> ...

  2. Eclipse Block Selection(块选择)快捷键 Alt + Shift + A

    说实话,我暂时还没用过这个快捷键.但是不代表以后我也不会用它. Eclipse 有个地方可以专门查看这些小技巧. Help → Tip of the Day 进入下面这个窗口: 将 Unread on ...

  3. Linux 总结篇

    1. sudo -i update upgrade install 包名 (openjdk-8-jdk) autoremove 自动删除不需要的包(remove卸载) sudo apt-get 2. ...

  4. 10分钟用scratch写一个大鱼吃小鱼的小游戏

    第一次给张江小朋友教Scratch课程之前,还在担心一些概念能不能向小朋友解释清楚,可上完课发现,我严重低估了小朋友的聪明程度,发现现在的孩子相比较自己8.9岁的时候,简直聪明太多倍了. 所以总结了半 ...

  5. kafka和zookeeper安装部署(版本弄不好就是坑)

    yum install -y unzip zip 配置host vi /etc/host172.19.68.10 zk1 1. zookeeper zookeeper下载地址 http://mirro ...

  6. 消息认证码 - MAC (Message Authentication Code)

    消息认证包括两个目标 1消息完整性认证: 确保张三发给我的消息是完整的,在传输过程中没有被第三方篡改 2消息的来源认证: 确保这个数据是张三发给我的,而不是李四发给我的 第一个目标通常使用散列函数来达 ...

  7. java 单链表反转

    最近与人瞎聊,聊到各大厂的面试题,其中有一个就是用java实现单链表反转.闲来无事,决定就这个问题进行一番尝试. 1.准备链表 准备一个由DataNode组成的单向链表,DataNode如下: pub ...

  8. 【转载】C#使用Math.Sqrt方法进行开平方操作

    在C#的数学数值运算中,有时候需要进行对数值进行开平方操作,C#的数值计算类Math类中内置了开平方操作的方法Sqrt,直接调用此方法可计算出相应的平方值,Math.Sqrt方法签名为:double ...

  9. MySQL数据库使用时注意事项

    MySQL数据库使用时注意事项 建表的角度上 1.合理安排表关系 2.尽量把固定长度的字段放在前面 3.尽量使用char 代替varchar 4.分表:水平分和垂直分 在使用sql语句的时候 1.尽量 ...

  10. 几个不错的echarts +百度地图 案例

    https://echarts.baidu.com/examples/editor.html?c=map-polygon https://echarts.baidu.com/examples/edit ...