Thrift对多接口服务的支持

Thrift在0.9.1版本之前,一直只提交了对单一接口服务的支持,即一个RPC服务器(对应一个端口)支持一个服务接口的实现。

但是很多时候,我们的服务不能实现在一个接口里,一是接口里的方法越来越多,不好管理和使用;二是根据职责的单一要求,不能类型的方法,不能放在同一接口里。

在 Thrift-0.9.1之前,我们要解决上面的问题,一是通过多个RPC服务器来实现,这个方法必然导致了我们RPC服务器越来越多,管理上麻烦;二是通过其他的一些途径,如使用netty作为RPC服务器等,这个方法实现上相对麻烦一些,需要去了解netty的知识。

这些方法在这里就不再详述了。

从Thrift-0.9.1开始,thrift开始提供对多接口服务的支持,使得我们开发多接口RPC服务相对简单多了。

这里还是给出例子来说明。

首先,我们做了两个接口定义文件来定义两个接口:

namespace javacom.eli.test.service

struct Topic

{

1: i32 uid,

2: string name,

3: string content

}

service TopicService

{

void store(1: Topic topic),

Topic retrieve(1: i32 uid)

}

namespace javacom.eli.test.service

struct User

{

1: i32 uid,

2: string name,

3: string blurb

}

service UserService

{

void store1(1: User user),

User retrieve1(1: i32 uid)

}

然后使用Thrift-0.9.1.exe生成相应的JavaBean及接口:

然后,我们写两个接口的实现类:

import org.apache.thrift.TException;

import com.eli.test.service.Topic;

import com.eli.test.service.TopicService;

public class TopicImpl implements TopicService.Iface{

public void store(Topic topic)throws TException

{

System.out.println("theinput topic: ");

System.out.println("id:"+topic.getUid());

System.out.println("name:"+topic.getName());

System.out.println("content:"+topic.getContent());

}

public Topic retrieve(int uid)throws TException

{

System.out.println("theinput uid: "+uid);

return newTopic(uid,"test","test");

}

}

import org.apache.thrift.TException;

import com.eli.test.service.User;

import com.eli.test.service.UserService;

public class UserImpl implements UserService.Iface{

public void store1(User user)throws TException

{

System.out.println("theinput user: ");

System.out.println("uid:"+user.getUid());

System.out.println("name:"+user.getName());

System.out.println("blur:"+user.getBlurb());

}

public User retrieve1(int uid)throws TException

{

System.out.println("theinput uid: "+uid);

return newUser(uid,"tom","123");

}

}

上述工作完成以后,就可以写服务器代码了。

服务器代码与单一接口的服务器代码最大的不同是使用了“TMultiplexedProcessor”类,通过该类,可以注册多个接口的服务实现类:

TMultiplexedProcessor processor = new TMultiplexedProcessor();

processor.registerProcessor("TopicService", newTopicService.Processor<TopicService.Iface>(new TopicImpl()));

processor.registerProcessor("UserService", new UserService.Processor<UserService.Iface>(new UserImpl()));

其他代码就跟以前的服务器代码一样了。

完整的服务器代码如下:

TMultiplexedProcessor processor = newTMultiplexedProcessor();

TServerTransport t = new TServerSocket(9090);

TServer server = new TThreadPoolServer(newTThreadPoolServer.Args(t).processor(processor));

processor.registerProcessor("TopicService", newTopicService.Processor<TopicService.Iface>(new TopicImpl()));

processor.registerProcessor("UserService", newUserService.Processor<UserService.Iface>(new UserImpl()));

//         TSimpleServer server = new TSimpleServer(new Args(t).processor(processor));

System.out.println("the serveris started and is listening at 9090...");

server.serve();

最后是客户端代码了,客户端代码的不同之处是引入了“TMultiplexedProtocol”类,它来帮助客户端区别调用哪个接口:

TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol,"TopicService");

TopicService.Clientservice1 = new TopicService.Client(mp1);

完整的客户端测试代码如下:

TSocket transport = new TSocket("localhost",9090);

TBinaryProtocol protocol = new TBinaryProtocol(transport);

TMultiplexedProtocolmp1 = new TMultiplexedProtocol(protocol,"TopicService");

TopicService.Client service1 = newTopicService.Client(mp1);

TMultiplexedProtocolmp2 = new TMultiplexedProtocol(protocol,"UserService");

UserService.Client service2 = newUserService.Client(mp2);

transport.open();

service1.store(new Topic(668,"test topic","just a test!"));

service2.store1(new User(888,"tom","haha"));

System.out.println(service1.retrieve(168));

System.out.println(service2.retrieve1(999));

transport.close();

运行服务器代码和客户端代码,则客户端会打印如下结果:

Topic(uid:168, name:test, content:test)

User(uid:999, name:tom, blurb:123)

服务器端会打印如下的信息:

the server is started and is listening at 9090...

the input topic:

id: 668

name: test topic

content: just a test!

the input user:

uid: 888

name: tom

blur: haha

the input uid: 168

the input uid: 999

通过上面的例子,可以看到,Thrift-0.9.1版本很简单的解决了多接口服务的问题,随便说一下,该版本并没有解决所有语言的多接口实现问题,只是解决了Java和其他几个语言。请大家在使用的时候,查清楚您使用的语言是否解决了这个问题。

Thrift对多接口服务的支持的更多相关文章

  1. Thrift搭建分布式微服务1

    Thrift搭建分布式微服务 一.Thrift是什么? 关于Thrift的基本介绍,参看张善友的文章Thrift简介. 二.为什么使用微服务? 在公司的高速发展过程中,随着业务的增长,子系统越来越多. ...

  2. Thrift搭建分布式微服务(四)

      第一篇 <连接配置> 第二篇 <连接池> 第三篇 <标准通信> 第四篇 快速暴露接口 之前的文章,我们介绍了如何使用连接池管理Thrift节点,以及使用Thri ...

  3. Thrift搭建分布式微服务(二)

    第二篇 连接池  连接池配置,请前往Thrift搭建分布式微服务(一)  下面要介绍的其实不是单一的连接池,应该说是连接池集合.因为它要管理多个Tcp Socket连接节点,每个服务节点都有设置了自己 ...

  4. SpringBoot接口服务处理Whitelabel Error Page(转)

    To switch it off you can set server.error.whitelabel.enabled=false http://stackoverflow.com/question ...

  5. dotnet core 开发无缝兼容Http和Websocket协议的接口服务

    在应用接口开发中往往要针对不同协义开发相应的代理服务,但对于Websocket和http这两种协议来说就有些不同,从实现上来看Websocket可以说是Http的升级子协议, 两者在协议处理上基本一致 ...

  6. ASP.NET WebAPI构建API接口服务实战演练

    一.课程介绍 一.王小二和他领导的第一次故事 有一天王小二和往常一下去上早班,刚吃完早餐刚一打开电脑没一会儿.王小二的领导宋大宝走到他的面前,我们现在的系统需要提供服务给其他内部业务系统,我看你平时喜 ...

  7. SpringBoot接口服务处理Whitelabel Error Page

    转载请注明来源:http://blog.csdn.net/loongshawn/article/details/50915979 <SpringBoot接口服务处理Whitelabel Erro ...

  8. Thrift写RPC接口

    Thrift总结(二)创建RPC服务 前面介绍了thrift 基础的东西,怎么写thrift 语法规范编写脚本,如何生成相关的语言的接口.不清楚的可以看这个<Thrift总结(一)介绍>. ...

  9. PHP laravel+thrift+swoole打造微服务框架

    Laravel作为最受欢迎的php web框架一直广受广大互联网公司的喜爱. 笔者也参与过一些由laravel开发的项目.虽然laravel的性能广受诟病但是业界也有一些比较好的解决方案,比如堆机器, ...

随机推荐

  1. STL模板_智能指针概念

    一.智能指针1.类类型对象,在其内部封装了一个普通指针.当智能指针对象因离开作用域而被析构时,其析构函数被执行,通过其内部封装的普通指针,销毁该指针的目标对象,避免内存泄露.2.为了表现出和普通指针一 ...

  2. Qt(QML)本地化

    Internationalization and Localization with Qt Quick 程序国际化 1) Use qsTr() for all  Literial UI strings ...

  3. fatal error LNK1123: failure during conversion to COFF: file invalid or corr

    新装VS2010出现标题的错误,使用了下面的方法,不行 这个是由于日志文件引起的,可以将 项目\属性\配置属性\清单工具\输入和输出\嵌入清单:原来是"是",改成"否&q ...

  4. C/C++常用编辑器

    VIM ,www.vim.org/ Emacs, www.gnu.org/software/emacs/ notepad++,www.notepad-plus-plus.org/ Textmate,h ...

  5. unity3d 2d游戏制作的模式

      经过了4个月不懈的努力,我和图灵教育合作的这本3D游戏开发书预计下个月就要出版了.这里MOMO先打一下广告,图灵的出版社编辑成员都非常给力,尤其是编辑小花为这本书付出了很大的努力,还有杨海玲老师, ...

  6. MySQL学习系列一---命令行连接mysql和执行sql文件

    1.命令行连接mysql #mysql -h(主机) -u(用户名) -p (数据库名) mysql -hlocalhost -uroot -p testdb Enter password: **** ...

  7. MySQL的InnoDB和MyISAM比较

    InnoDB 1)虽然不支持用户创建聚族索引,但InnoDB会对主键建立聚簇索引.如果你不指定主键,InnoDB会用一个具有唯一且非空值的索引来代替.如果不存在这样的索引,InnoDB会定义一个隐藏的 ...

  8. 如何使用Excel和Word编辑和打印条形码

    本文介绍如何使用Microsoft Office Excel 2007和Microsoft Office Word 2007进行条形码的编辑后,通过普通的办公打印机将条形码打印出来. 对于少量,简单的 ...

  9. Window下 Qt 编译MySQL驱动(居然用到了动态库格式转换工具)

    一步步在Window下开发Qt 今天开始安装MySQL,看了些关于MySQL安装的博文,方法大致相同,但是遇到的细节问题各有不同,或者没有讲全面,下面来说说个人的安装过程及遇到的问题. 1.首先下载, ...

  10. PE头的应用---插入代码到EXE或DLL文件中

    三.代码实现(DELPHI版本),采用第三种方式实现代码插入. 1. 定义两个类,一个用来实现在内存中建立输入表:一个用来实现对PE头的代码插入. DelphiCode: const MAX_SECT ...