转载至 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. c++无关类型指针的强制转换原理的分析和尝试

    因最近看到大量的c类型指针强制转换,联系到c++的reinterpret_cast强制转换符,故总结一下. 先上图 由图中可以看出,先声明了一个结构体t与一个含有三个元素的数组num,接着声明一个指向 ...

  2. linux系统状态脚本

    #!/bin/bashprintf "%10s\n" "##主机名##"printf "%-10s\n" "$(hostname) ...

  3. 深度学习-LSTM与GRU

    http://www.sohu.com/a/259957763_610300此篇文章绕开了数学公式,对LSTM与GRU采用图文并茂的方式进行说明,尤其是里面的动图,让人一目了然.https://zyb ...

  4. 【LEETCODE】58、数组分类,适中级别,题目:238、78、287

    package y2019.Algorithm.array.medium; import java.util.Arrays; /** * @ProjectName: cutter-point * @P ...

  5. [洛谷P2056][ZJOI2007]捉迷藏(2019-7-20考试)

    题目大意:有一棵$n(n\leqslant10^6)$个点的树,上面所有点是黑点,有$m$次操作: $C\;u$:把点$u$颜色翻转 $G$:问树上最远的两个黑点的距离,若没有黑点输出$0$ 题解:有 ...

  6. centos 7 安装nginx并启动(笔记)

    参考 https://www.cnblogs.com/liujuncm5/p/6713784.html Nginx 是 C语言 开发 一. gcc 安装安装 nginx 需要先将官网下载的源码进行编译 ...

  7. This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567.

    记事本打开csproj文件 搜索nuget 删除Target节点 类似如下: <Target Name="EnsureBclBuildImported" BeforeTarg ...

  8. 14.1 Scroll说明和注意事项

    使用scroll滚动搜索: 比如全文搜索10万条数据,不能一次全搜出来返回,太耗时了.通常是一批一批的获取结果,滚动搜索     1. 第一次搜索时,会生成这批数据的快照,下次再搜的时候,基于此快照进 ...

  9. 使用jQuery开发accordion手风琴插件

    一.插件效果 手风琴插件常用的功能均已实现,包括:手风琴菜单项的折叠展开效果.选中指定菜单项.判断菜单项是否选中等. 效果如下: 二.插件内部HTML元素结构 <!-- accordioon组件 ...

  10. 使用springboot实现一个简单的restful crud——03、前端页面、管理员登陆(注销)功能

    前言 这一篇我们就先引入前端页面和相关的静态资源,再做一下管理员的登陆和注销的功能,为后续在页面上操作数据做一个基础. 前端页面 前端的页面是我从网上找的一个基于Bootstrap 的dashboar ...