zoukankan      html  css  js  c++  java
  • Thrift初试

    Restful 基于 Http 进行通讯。

    • 开放、标准、简单、兼容性升级容易;
    • 性能略低。在 QPS 高或者对响应时间要求苛刻的服务上,可以用 RPC,RPC采用二进制传输、TCP 通讯,所以通常性能更好。

    RPC通讯

    • 效率相对较高
    • 耦合度强,如果兼容性处理不好的话,一旦服务器端接口升级,客户端就要更新,即使是增加一个参数,而 rest 则比较灵活。

    最佳实践:对内一些性能要求高的场合用 RPC,对内其他场合以及对外用 Rest。比如 web 服务器和视频转码服务器之间通讯可以用 restful 就够了,转账接口用 RPC 性能会更高 一些。

    以上参考:老杨的课件。

     

    Thrift是一个跨语言通信的服务框架,不同语言开发的程序可以通过Thrift来进行通信。

    使用

    下载:http://www.apache.org/dyn/closer.cgi?path=/thrift/0.11.0/thrift-0.11.0.exe

    把 thrift-***.exe 解压到磁盘,改名为 thrift.exe(用起来方便一些)

    编写 **.thrift 文件(IDL)

    根据IDL,自动生成对应的语言代码:

    thrift -gen csharp f: etcoreThriftsschool.thrift

     

    一个简单实例

    https://github.com/tianbogit/ThriftDemo

    一个宿主寄宿多个服务:

    //关联处理器与服务的实现
    TProcessor helloProcessor = new HelloService.Processor(new MyHelloService());
    TProcessor schoolProcessor = new SchoolService.Processor(new MySchoolService());
    
    //创建服务端对象 
    var processorMulti = new TMultiplexedProcessor();
    processorMulti.RegisterProcessor("helloService", helloProcessor);
    processorMulti.RegisterProcessor("schoolService", schoolProcessor);
    TServer server = new TThreadPoolServer(processorMulti, serverTransport, new TTransportFactory(), factory);
    

      

    参考

    [1]“Thirft框架介绍”

    [2]“Thrift使用指南”

    [3]“使用Thrift RPC编写程序”

    [4]“让Thrift支持双向通信”

    [5]“浅谈Thrift内部实现原理“

    http://dongxicheng.org/recommend/

  • 相关阅读:
    Intern Day5
    PTA1007
    Intern Day5
    Intern Day2
    Intern Day5
    Intern Day2
    Intern Day2
    Intern Day2
    Intern Day1
    柯南剧场版17绝海的侦探
  • 原文地址:https://www.cnblogs.com/tianboblog/p/10064696.html
Copyright © 2011-2022 走看看