.Net RPC框架Thrift的用法

  关于Thrift

下面是来自百度百科关于Thrift的介绍:

thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和引擎,以构建在 C++, Java, Go,Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。

Apache开源地址:http://thrift.apache.org/

Thrift也是.Net平台上一款不错的RPC框架,更何况可以实现与众多编程语言之间的远程调用。下面具体介绍下Thrift在.Net上的用法。

首先,下载Thrift工具,这里选择windows平台的,下载地址:http://www.apache.org/dyn/closer.cgi?path=/thrift/0.11.0/thrift-0.11.0.exe

其次,下载具体的Thrift Sdk包,http://www.apache.org/dyn/closer.cgi?path=/thrift/0.11.0/thrift-0.11.0.tar.gz

内部包含了所支持的语言源码,这里,我们选择csharp目录里的内容,用VS打开,编译生成DLL文件,可供后续使用。

其次,编写Thrift文件,内容如下,这里定义了一个User类,包含一个Int32类型的ID和一个string类型的Name属性,同时,在定义了一个服务,UserService,其中包含GetUserByID和GetAllUser两个方法。  、

struct User {
    1: i32 ID
    2: string Name
  }

service UserService {
    User GetUserByID(1:i32 userID)
    list<User> GetAllUser()
}      

  然后,通过上面下载的Thrif.exe命令行工具,进行代码的生成,windows平台,cmd进入thrift.exe所在目录,执行如下命令:

thrift --gen csharp user.thrift

 命令格式如:

thrift --gen <language> <Thrift filename>

也可以把thrift.exe放到指定目录或C盘位置,配置环境变量Path,然后可以直接执行以上命令。

执行完以上命令后,会在当前目录下,生成一个名为gen-csharp的文件夹,里面包含了2个类(User.cs和UserService.cs)。

User.cs代码如下:

/**
 * Autogenerated by Thrift Compiler (0.11.0)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Thrift;
using Thrift.Collections;
using System.Runtime.Serialization;
using Thrift.Protocol;
using Thrift.Transport;

#if !SILVERLIGHT
[Serializable]
#endif
public partial class User : TBase
{
  private int _ID;
  private string _Name;

  public int ID
  {
    get
    {
      return _ID;
    }
    set
    {
      __isset.ID = true;
      this._ID = value;
    }
  }

  public string Name
  {
    get
    {
      return _Name;
    }
    set
    {
      __isset.Name = true;
      this._Name = value;
    }
  }

  public Isset __isset;
  #if !SILVERLIGHT
  [Serializable]
  #endif
  public struct Isset {
    public bool ID;
    public bool Name;
  }

  public User() {
  }

  public void Read (TProtocol iprot)
  {
    iprot.IncrementRecursionDepth();
    try
    {
      TField field;
      iprot.ReadStructBegin();
      while (true)
      {
        field = iprot.ReadFieldBegin();
        if (field.Type == TType.Stop) {
          break;
        }
        switch (field.ID)
        {
          case 1:
            if (field.Type == TType.I32) {
              ID = iprot.ReadI32();
            } else {
              TProtocolUtil.Skip(iprot, field.Type);
            }
            break;
          case 2:
            if (field.Type == TType.String) {
              Name = iprot.ReadString();
            } else {
              TProtocolUtil.Skip(iprot, field.Type);
            }
            break;
          default:
            TProtocolUtil.Skip(iprot, field.Type);
            break;
        }
        iprot.ReadFieldEnd();
      }
      iprot.ReadStructEnd();
    }
    finally
    {
      iprot.DecrementRecursionDepth();
    }
  }

  public void Write(TProtocol oprot) {
    oprot.IncrementRecursionDepth();
    try
    {
      TStruct struc = new TStruct("User");
      oprot.WriteStructBegin(struc);
      TField field = new TField();
      if (__isset.ID) {
        field.Name = "ID";
        field.Type = TType.I32;
        field.ID = 1;
        oprot.WriteFieldBegin(field);
        oprot.WriteI32(ID);
        oprot.WriteFieldEnd();
      }
      if (Name != null && __isset.Name) {
        field.Name = "Name";
        field.Type = TType.String;
        field.ID = 2;
        oprot.WriteFieldBegin(field);
        oprot.WriteString(Name);
        oprot.WriteFieldEnd();
      }
      oprot.WriteFieldStop();
      oprot.WriteStructEnd();
    }
    finally
    {
      oprot.DecrementRecursionDepth();
    }
  }

  public override string ToString() {
    StringBuilder __sb = new StringBuilder("User(");
    bool __first = true;
    if (__isset.ID) {
      if(!__first) { __sb.Append(", "); }
      __first = false;
      __sb.Append("ID: ");
      __sb.Append(ID);
    }
    if (Name != null && __isset.Name) {
      if(!__first) { __sb.Append(", "); }
      __first = false;
      __sb.Append("Name: ");
      __sb.Append(Name);
    }
    __sb.Append(")");
    return __sb.ToString();
  }

}

  UserService.cs代码如下:

/**
 * Autogenerated by Thrift Compiler (0.11.0)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Thrift;
using Thrift.Collections;
using System.Runtime.Serialization;
using Thrift.Protocol;
using Thrift.Transport;

public partial class UserService {
  public interface ISync {
    User GetUserByID(int userID);
    List<User> GetAllUser();
  }

  public interface Iface : ISync {
    #if SILVERLIGHT
    IAsyncResult Begin_GetUserByID(AsyncCallback callback, object state, int userID);
    User End_GetUserByID(IAsyncResult asyncResult);
    #endif
    #if SILVERLIGHT
    IAsyncResult Begin_GetAllUser(AsyncCallback callback, object state);
    List<User> End_GetAllUser(IAsyncResult asyncResult);
    #endif
  }

  public class Client : IDisposable, Iface {
    public Client(TProtocol prot) : this(prot, prot)
    {
    }

    public Client(TProtocol iprot, TProtocol oprot)
    {
      iprot_ = iprot;
      oprot_ = oprot;
    }

    protected TProtocol iprot_;
    protected TProtocol oprot_;
    protected int seqid_;

    public TProtocol InputProtocol
    {
      get { return iprot_; }
    }
    public TProtocol OutputProtocol
    {
      get { return oprot_; }
    }

    #region " IDisposable Support "
    private bool _IsDisposed;

    // IDisposable
    public void Dispose()
    {
      Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
      if (!_IsDisposed)
      {
        if (disposing)
        {
          if (iprot_ != null)
          {
            ((IDisposable)iprot_).Dispose();
          }
          if (oprot_ != null)
          {
            ((IDisposable)oprot_).Dispose();
          }
        }
      }
      _IsDisposed = true;
    }
    #endregion

    #if SILVERLIGHT
    public IAsyncResult Begin_GetUserByID(AsyncCallback callback, object state, int userID)
    {
      return send_GetUserByID(callback, state, userID);
    }

    public User End_GetUserByID(IAsyncResult asyncResult)
    {
      oprot_.Transport.EndFlush(asyncResult);
      return recv_GetUserByID();
    }

    #endif

    public User GetUserByID(int userID)
    {
      #if !SILVERLIGHT
      send_GetUserByID(userID);
      return recv_GetUserByID();

      #else
      var asyncResult = Begin_GetUserByID(null, null, userID);
      return End_GetUserByID(asyncResult);

      #endif
    }
    #if SILVERLIGHT
    public IAsyncResult send_GetUserByID(AsyncCallback callback, object state, int userID)
    #else
    public void send_GetUserByID(int userID)
    #endif
    {
      oprot_.WriteMessageBegin(new TMessage("GetUserByID", TMessageType.Call, seqid_));
      GetUserByID_args args = new GetUserByID_args();
      args.UserID = userID;
      args.Write(oprot_);
      oprot_.WriteMessageEnd();
      #if SILVERLIGHT
      return oprot_.Transport.BeginFlush(callback, state);
      #else
      oprot_.Transport.Flush();
      #endif
    }

    public User recv_GetUserByID()
    {
      TMessage msg = iprot_.ReadMessageBegin();
      if (msg.Type == TMessageType.Exception) {
        TApplicationException x = TApplicationException.Read(iprot_);
        iprot_.ReadMessageEnd();
        throw x;
      }
      GetUserByID_result result = new GetUserByID_result();
      result.Read(iprot_);
      iprot_.ReadMessageEnd();
      if (result.__isset.success) {
        return result.Success;
      }
      throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "GetUserByID failed: unknown result");
    }

    #if SILVERLIGHT
    public IAsyncResult Begin_GetAllUser(AsyncCallback callback, object state)
    {
      return send_GetAllUser(callback, state);
    }

    public List<User> End_GetAllUser(IAsyncResult asyncResult)
    {
      oprot_.Transport.EndFlush(asyncResult);
      return recv_GetAllUser();
    }

    #endif

    public List<User> GetAllUser()
    {
      #if !SILVERLIGHT
      send_GetAllUser();
      return recv_GetAllUser();

      #else
      var asyncResult = Begin_GetAllUser(null, null);
      return End_GetAllUser(asyncResult);

      #endif
    }
    #if SILVERLIGHT
    public IAsyncResult send_GetAllUser(AsyncCallback callback, object state)
    #else
    public void send_GetAllUser()
    #endif
    {
      oprot_.WriteMessageBegin(new TMessage("GetAllUser", TMessageType.Call, seqid_));
      GetAllUser_args args = new GetAllUser_args();
      args.Write(oprot_);
      oprot_.WriteMessageEnd();
      #if SILVERLIGHT
      return oprot_.Transport.BeginFlush(callback, state);
      #else
      oprot_.Transport.Flush();
      #endif
    }

    public List<User> recv_GetAllUser()
    {
      TMessage msg = iprot_.ReadMessageBegin();
      if (msg.Type == TMessageType.Exception) {
        TApplicationException x = TApplicationException.Read(iprot_);
        iprot_.ReadMessageEnd();
        throw x;
      }
      GetAllUser_result result = new GetAllUser_result();
      result.Read(iprot_);
      iprot_.ReadMessageEnd();
      if (result.__isset.success) {
        return result.Success;
      }
      throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "GetAllUser failed: unknown result");
    }

  }
  public class Processor : TProcessor {
    public Processor(ISync iface)
    {
      iface_ = iface;
      processMap_["GetUserByID"] = GetUserByID_Process;
      processMap_["GetAllUser"] = GetAllUser_Process;
    }

    protected delegate void ProcessFunction(int seqid, TProtocol iprot, TProtocol oprot);
    private ISync iface_;
    protected Dictionary<string, ProcessFunction> processMap_ = new Dictionary<string, ProcessFunction>();

    public bool Process(TProtocol iprot, TProtocol oprot)
    {
      try
      {
        TMessage msg = iprot.ReadMessageBegin();
        ProcessFunction fn;
        processMap_.TryGetValue(msg.Name, out fn);
        if (fn == null) {
          TProtocolUtil.Skip(iprot, TType.Struct);
          iprot.ReadMessageEnd();
          TApplicationException x = new TApplicationException (TApplicationException.ExceptionType.UnknownMethod, "Invalid method name: ‘" + msg.Name + "‘");
          oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID));
          x.Write(oprot);
          oprot.WriteMessageEnd();
          oprot.Transport.Flush();
          return true;
        }
        fn(msg.SeqID, iprot, oprot);
      }
      catch (IOException)
      {
        return false;
      }
      return true;
    }

    public void GetUserByID_Process(int seqid, TProtocol iprot, TProtocol oprot)
    {
      GetUserByID_args args = new GetUserByID_args();
      args.Read(iprot);
      iprot.ReadMessageEnd();
      GetUserByID_result result = new GetUserByID_result();
      try
      {
        result.Success = iface_.GetUserByID(args.UserID);
        oprot.WriteMessageBegin(new TMessage("GetUserByID", TMessageType.Reply, seqid));
        result.Write(oprot);
      }
      catch (TTransportException)
      {
        throw;
      }
      catch (Exception ex)
      {
        Console.Error.WriteLine("Error occurred in processor:");
        Console.Error.WriteLine(ex.ToString());
        TApplicationException x = new TApplicationException      (TApplicationException.ExceptionType.InternalError," Internal error.");
        oprot.WriteMessageBegin(new TMessage("GetUserByID", TMessageType.Exception, seqid));
        x.Write(oprot);
      }
      oprot.WriteMessageEnd();
      oprot.Transport.Flush();
    }

    public void GetAllUser_Process(int seqid, TProtocol iprot, TProtocol oprot)
    {
      GetAllUser_args args = new GetAllUser_args();
      args.Read(iprot);
      iprot.ReadMessageEnd();
      GetAllUser_result result = new GetAllUser_result();
      try
      {
        result.Success = iface_.GetAllUser();
        oprot.WriteMessageBegin(new TMessage("GetAllUser", TMessageType.Reply, seqid));
        result.Write(oprot);
      }
      catch (TTransportException)
      {
        throw;
      }
      catch (Exception ex)
      {
        Console.Error.WriteLine("Error occurred in processor:");
        Console.Error.WriteLine(ex.ToString());
        TApplicationException x = new TApplicationException      (TApplicationException.ExceptionType.InternalError," Internal error.");
        oprot.WriteMessageBegin(new TMessage("GetAllUser", TMessageType.Exception, seqid));
        x.Write(oprot);
      }
      oprot.WriteMessageEnd();
      oprot.Transport.Flush();
    }

  }

  #if !SILVERLIGHT
  [Serializable]
  #endif
  public partial class GetUserByID_args : TBase
  {
    private int _userID;

    public int UserID
    {
      get
      {
        return _userID;
      }
      set
      {
        __isset.userID = true;
        this._userID = value;
      }
    }

    public Isset __isset;
    #if !SILVERLIGHT
    [Serializable]
    #endif
    public struct Isset {
      public bool userID;
    }

    public GetUserByID_args() {
    }

    public void Read (TProtocol iprot)
    {
      iprot.IncrementRecursionDepth();
      try
      {
        TField field;
        iprot.ReadStructBegin();
        while (true)
        {
          field = iprot.ReadFieldBegin();
          if (field.Type == TType.Stop) {
            break;
          }
          switch (field.ID)
          {
            case 1:
              if (field.Type == TType.I32) {
                UserID = iprot.ReadI32();
              } else {
                TProtocolUtil.Skip(iprot, field.Type);
              }
              break;
            default:
              TProtocolUtil.Skip(iprot, field.Type);
              break;
          }
          iprot.ReadFieldEnd();
        }
        iprot.ReadStructEnd();
      }
      finally
      {
        iprot.DecrementRecursionDepth();
      }
    }

    public void Write(TProtocol oprot) {
      oprot.IncrementRecursionDepth();
      try
      {
        TStruct struc = new TStruct("GetUserByID_args");
        oprot.WriteStructBegin(struc);
        TField field = new TField();
        if (__isset.userID) {
          field.Name = "userID";
          field.Type = TType.I32;
          field.ID = 1;
          oprot.WriteFieldBegin(field);
          oprot.WriteI32(UserID);
          oprot.WriteFieldEnd();
        }
        oprot.WriteFieldStop();
        oprot.WriteStructEnd();
      }
      finally
      {
        oprot.DecrementRecursionDepth();
      }
    }

    public override string ToString() {
      StringBuilder __sb = new StringBuilder("GetUserByID_args(");
      bool __first = true;
      if (__isset.userID) {
        if(!__first) { __sb.Append(", "); }
        __first = false;
        __sb.Append("UserID: ");
        __sb.Append(UserID);
      }
      __sb.Append(")");
      return __sb.ToString();
    }

  }

  #if !SILVERLIGHT
  [Serializable]
  #endif
  public partial class GetUserByID_result : TBase
  {
    private User _success;

    public User Success
    {
      get
      {
        return _success;
      }
      set
      {
        __isset.success = true;
        this._success = value;
      }
    }

    public Isset __isset;
    #if !SILVERLIGHT
    [Serializable]
    #endif
    public struct Isset {
      public bool success;
    }

    public GetUserByID_result() {
    }

    public void Read (TProtocol iprot)
    {
      iprot.IncrementRecursionDepth();
      try
      {
        TField field;
        iprot.ReadStructBegin();
        while (true)
        {
          field = iprot.ReadFieldBegin();
          if (field.Type == TType.Stop) {
            break;
          }
          switch (field.ID)
          {
            case 0:
              if (field.Type == TType.Struct) {
                Success = new User();
                Success.Read(iprot);
              } else {
                TProtocolUtil.Skip(iprot, field.Type);
              }
              break;
            default:
              TProtocolUtil.Skip(iprot, field.Type);
              break;
          }
          iprot.ReadFieldEnd();
        }
        iprot.ReadStructEnd();
      }
      finally
      {
        iprot.DecrementRecursionDepth();
      }
    }

    public void Write(TProtocol oprot) {
      oprot.IncrementRecursionDepth();
      try
      {
        TStruct struc = new TStruct("GetUserByID_result");
        oprot.WriteStructBegin(struc);
        TField field = new TField();

        if (this.__isset.success) {
          if (Success != null) {
            field.Name = "Success";
            field.Type = TType.Struct;
            field.ID = 0;
            oprot.WriteFieldBegin(field);
            Success.Write(oprot);
            oprot.WriteFieldEnd();
          }
        }
        oprot.WriteFieldStop();
        oprot.WriteStructEnd();
      }
      finally
      {
        oprot.DecrementRecursionDepth();
      }
    }

    public override string ToString() {
      StringBuilder __sb = new StringBuilder("GetUserByID_result(");
      bool __first = true;
      if (Success != null && __isset.success) {
        if(!__first) { __sb.Append(", "); }
        __first = false;
        __sb.Append("Success: ");
        __sb.Append(Success== null ? "<null>" : Success.ToString());
      }
      __sb.Append(")");
      return __sb.ToString();
    }

  }

  #if !SILVERLIGHT
  [Serializable]
  #endif
  public partial class GetAllUser_args : TBase
  {

    public GetAllUser_args() {
    }

    public void Read (TProtocol iprot)
    {
      iprot.IncrementRecursionDepth();
      try
      {
        TField field;
        iprot.ReadStructBegin();
        while (true)
        {
          field = iprot.ReadFieldBegin();
          if (field.Type == TType.Stop) {
            break;
          }
          switch (field.ID)
          {
            default:
              TProtocolUtil.Skip(iprot, field.Type);
              break;
          }
          iprot.ReadFieldEnd();
        }
        iprot.ReadStructEnd();
      }
      finally
      {
        iprot.DecrementRecursionDepth();
      }
    }

    public void Write(TProtocol oprot) {
      oprot.IncrementRecursionDepth();
      try
      {
        TStruct struc = new TStruct("GetAllUser_args");
        oprot.WriteStructBegin(struc);
        oprot.WriteFieldStop();
        oprot.WriteStructEnd();
      }
      finally
      {
        oprot.DecrementRecursionDepth();
      }
    }

    public override string ToString() {
      StringBuilder __sb = new StringBuilder("GetAllUser_args(");
      __sb.Append(")");
      return __sb.ToString();
    }

  }

  #if !SILVERLIGHT
  [Serializable]
  #endif
  public partial class GetAllUser_result : TBase
  {
    private List<User> _success;

    public List<User> Success
    {
      get
      {
        return _success;
      }
      set
      {
        __isset.success = true;
        this._success = value;
      }
    }

    public Isset __isset;
    #if !SILVERLIGHT
    [Serializable]
    #endif
    public struct Isset {
      public bool success;
    }

    public GetAllUser_result() {
    }

    public void Read (TProtocol iprot)
    {
      iprot.IncrementRecursionDepth();
      try
      {
        TField field;
        iprot.ReadStructBegin();
        while (true)
        {
          field = iprot.ReadFieldBegin();
          if (field.Type == TType.Stop) {
            break;
          }
          switch (field.ID)
          {
            case 0:
              if (field.Type == TType.List) {
                {
                  Success = new List<User>();
                  TList _list0 = iprot.ReadListBegin();
                  for( int _i1 = 0; _i1 < _list0.Count; ++_i1)
                  {
                    User _elem2;
                    _elem2 = new User();
                    _elem2.Read(iprot);
                    Success.Add(_elem2);
                  }
                  iprot.ReadListEnd();
                }
              } else {
                TProtocolUtil.Skip(iprot, field.Type);
              }
              break;
            default:
              TProtocolUtil.Skip(iprot, field.Type);
              break;
          }
          iprot.ReadFieldEnd();
        }
        iprot.ReadStructEnd();
      }
      finally
      {
        iprot.DecrementRecursionDepth();
      }
    }

    public void Write(TProtocol oprot) {
      oprot.IncrementRecursionDepth();
      try
      {
        TStruct struc = new TStruct("GetAllUser_result");
        oprot.WriteStructBegin(struc);
        TField field = new TField();

        if (this.__isset.success) {
          if (Success != null) {
            field.Name = "Success";
            field.Type = TType.List;
            field.ID = 0;
            oprot.WriteFieldBegin(field);
            {
              oprot.WriteListBegin(new TList(TType.Struct, Success.Count));
              foreach (User _iter3 in Success)
              {
                _iter3.Write(oprot);
              }
              oprot.WriteListEnd();
            }
            oprot.WriteFieldEnd();
          }
        }
        oprot.WriteFieldStop();
        oprot.WriteStructEnd();
      }
      finally
      {
        oprot.DecrementRecursionDepth();
      }
    }

    public override string ToString() {
      StringBuilder __sb = new StringBuilder("GetAllUser_result(");
      bool __first = true;
      if (Success != null && __isset.success) {
        if(!__first) { __sb.Append(", "); }
        __first = false;
        __sb.Append("Success: ");
        __sb.Append(Success);
      }
      __sb.Append(")");
      return __sb.ToString();
    }

  }

}

  最后,我们新建3个项目,一个Server,一个Client,一个Service(定义的服务),结构如下:

让我们先看看ThrifServer服务端吧。

        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {

            Console.Title = "Thrift服务端-Server";
            TServerSocket serverTransport = new TServerSocket(8080, 0, false);
            UserService.Processor processor = new UserService.Processor(new Services.TheUserService());
            TServer server = new TSimpleServer(processor, serverTransport);
            Console.WriteLine("启动服务器,监听端口8080 ...");
            server.Serve();
        }

  这里启动服务。在服务端,我们定义了一个TheUserService类,来实现Service中定义的成员。

 /// <summary>
    /// 用户服务
    /// </summary>
    public class TheUserService:Iface
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public User GetUserByID(int userID)
        {
            return new User() { ID = 1, Name = "lichaoqiang" };
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public List<User> GetAllUser()
        {
            List<User> users = new List<User>(){
                new User() { ID = 1, Name = "lichaoqiang" },
                new User() { ID = 2, Name = "yuyuangfang" }
            };
            return users;
        }
    }  

Thrift客户端:

        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.Title = "Thrift客户端-Client";
            TTransport transport = new TSocket("10.10.10.12", 8080);

            TProtocol protocol = new TJSONProtocol(transport);
            UserService.Client client = new UserService.Client(protocol);
            transport.Open();
            //var users = client.GetAllUser();

            //users.ForEach(u => Console.WriteLine(string.Format("User ID : {0}, User Name {1}", u.ID, u.Name)));
            var user = client.GetUserByID(1);
            Console.WriteLine("------------------");
            Console.WriteLine(string.Format("User ID : {0}, User Name {1}", user.ID, user.Name));
            Console.ReadLine();
        }

  完成以上步骤后,让我们启动服务端和客户端,就可以看到效果了!

原文地址:https://www.cnblogs.com/ibeisha/p/thrift.html

时间: 2024-11-10 14:14:34

.Net RPC框架Thrift的用法的相关文章

RPC框架Thrift例子-PHP调用C++后端程序

更新 2016-02-22: Response对象不用主动创建. 前言 前段时间用了一下Facebook的开源RPC框架Thrift,做PHP客户端调用C++后端程序,真心觉得Thrift不错! 本文项目地址:https://github.com/zekunyan/ThriftDemo_PHP_CPP 先看看本文的例子示意图: 流程 PHP客户端发起请求,请求参数是“Request”类型,里面有studentID参数. CPP服务端收到请求返回数据,返回类型为“Response”,里面包含了st

RPC框架 - thrift 服务端

-------服务端程序 ------ 下载    下载 thrift 源代码包    下载 thrift 的bin包 准备描述文件(使用源代码包的示例文件)    \thrift-0.10.0\tutorial\shared.thrift    \thrift-0.10.0\tutorial\tutorial.thrift 根据描述文件生成代码    thrift-0.10.0.exe -r -o lib_server --gen php:server idl-dir/tutorial.thr

RPC框架 - thrift 客户端

-------客户端程序 ------ 下载    下载 thrift 源代码包    下载 thrift 的bin包 准备描述文件(使用源代码包的示例文件)    \thrift-0.10.0\tutorial\shared.thrift    \thrift-0.10.0\tutorial\tutorial.thrift 根据描述文件生成代码    thrift-0.10.0.exe -r -o lib_client --gen php idl-dir/tutorial.thrift 从th

rpc框架之thrift教程1 - 安装 及 hello world

thrift是一个facebook开源的高效RPC框架,其主要特点是跨语言及二进制高效传输(当然,除了二进制,也支持json等常用序列化机制),官网地址:http://thrift.apache.org 跨语言通常有二种做法, 一是将其它语言转换成某种主流的通用语言,比如:delphi.net以前就是先将delphi转换成c#,然后再编译成IL,从而实现delphi在.net上的运行(好久没关注delphi了,不知道现在还是不是这种机制) 二是先定义一种规范文件(可以简单的理解为『母版』),然后

rpc框架之 thrift连接池实现

接前一篇rpc框架之HA/负载均衡构架设计 继续,写了一个简单的thrift 连接池: 先做点准备工作: package yjmyzz; public class ServerInfo { public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(i

Apache thrift - 使用,内部实现及构建一个可扩展的RPC框架

本文首先介绍了什么是Apache Thrift,接着介绍了Thrift的安装部署及如何利用Thrift来实现一个简单的RPC应用,并简单的探究了一下Thrift的内部实现原理,最后给出一个基于Thrift的可扩展的分布式RPC调用框架,在中小型项目中是一个常见的SOA实践. Thrift介绍 Apache Thrift是Facebook 开发的远程服务调用框架,它采用接口描述语言(IDL)定义并创建服务,支持可扩展的跨语言服务开发,所包含的代码生成引擎可以在多种语言中,如 C++, Java,

分布式RPC框架性能大比拼 dubbo、motan、rpcx、gRPC、thrift的性能比较

Dubbo 是阿里巴巴公司开源的一个Java高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成.不过,略有遗憾的是,据说在淘宝内部,dubbo由于跟淘宝另一个类似的框架HSF(非开源)有竞争关系,导致dubbo团队已经解散(参见http://www.oschina.net/news/55059/druid-1-0-9 中的评论),反到是当当网的扩展版本仍在持续发展,墙内开花墙外香.其它的一些知名电商如当当.京东.国美维护了自己的分支或

RPC框架实践之:Apache Thrift

一.概述 RPC(Remote Procedure Call)即 远程过程调用,说的这么抽象,其实简化理解就是一个节点如何请求另一节点所提供的服务.在文章 微服务调用链追踪中心搭建 一文中模拟出来的调用链:ServiceA ---> ServiceB ---> ServiceC 就是一个远程调用的例子,只不过这篇文章里是通过RestTemplate这种 同步调用方式,利用的是HTTP协议在应用层完成的,这种方法虽然奏效,但有时效率并不高.而RPC可以不依赖于应用层协议,可以直接基于TCP进行远

VIP_OSP--基于Thrift的RPC框架的基本原理

公司(VIP)从2015年开始在内部推动Venus框架的使用,这是一款基于Apache Thrift远程调用框架二次开发的高性能.高可扩展的.服务治理的RPC框架.服务端使用IDL进行服务的定义,客户端集成服务的SDK即可调用服务端的服务,开发简单,大部分的公共功能都在Proxy代理层工作,减轻了开发者的负担,使其只需要关注业务部分.下面是对该框架的基本原理的简单介绍.参考文献:1. Apache Thrift - 可伸缩的跨语言服务开发框架2. 公司内部的Venux文档(内网文档,无法分享)