Thrift是一款由Fackbook开发的可伸缩、跨语言的服务开发框架
这段时间,一直在整理公司的内部 rpc 服务接口,面临的一个问题就是:由于公司内部的系统由几个不同的语言编写的。C# ,java,node.js 等,如何实现这些内部系统之间的接口统一调用,确实是比较麻烦,本来考虑用webapi 但是感觉内部系统之间用webapi 效率不高。最终,我们还是考虑引入Thrift ,通过Thrift整合各个不同的RPC服务。下面就Thrift 如何使用,做个简单的介绍,本人也是初次接触。
介绍
Thrift是一款由Fackbook开发的可伸缩、跨语言的服务开发框架,该框架已经开源并且加入的Apache项目。Thrift主要功能是:通过自定义的Interface Definition Language(IDL),可以创建基于RPC的客户端和服务端的服务代码。数据和服务代码的生成是通过Thrift内置的代码生成器来实现的。Thrift 的跨语言性体现在,它可以生成C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml , Delphi等语言的代码,且它们之间可以进行透明的通信。
Thrift 使得各个不同的语言的系统之间可以进行透明高效的通信。但是,Fackbook 的一贯作风就是只管发布,不管维护。所以,Thrift 目前还存在一些为解决的问题。大家在调研的时候,应该考虑清楚。
本文结合网络上的资源对从C#开发人员的角度简单介绍Thrift的使用,并且针对不同的传输协议和服务类型给出相应的C#实例,同时简单介绍Thrift异步客户端的实现。
Thrift代码生成器windows版下载地址
http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.exe
Thrift源码下载地址
http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.tar.gz
Window 下安装配置
Thrift 不需要安装,只需下载windows版的 Thrift代码生成器即可,下载地址如上连接
注意:下载下来之后,必须把文件名字thrift-0.10.0.exe 改为 thrift.exe, 否则cmd会提示:thrift 不是内部命令的错误。
创建thrift的语法规范编写脚本文件
根据thrift的语法规范编写脚本文件Hello.thrift,代码如下:
namespace csharp HelloThrift.Interface service HelloService{ string HelloString(1:string para) i32 HelloInt(1:i32 para) bool HelloBoolean(1:bool para) void HelloVoid() string HelloNull() }
生成Csharp 版的服务定义类
然后打开cmd切换到thrift代码生成工具的存放目录,在命令行中输入如下命令:thrift -gen csharp Hello.thrift
代码生成工具会自动在当前目录下把定义好的接口脚本生成C#代码,生成后的代码目录如下
接口脚本生成C#代码
/**
* Autogenerated by Thrift Compiler (0.9.3)
*
* 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; namespace HelloThrift.Interface
{
public partial class HelloService {
public interface Iface {
string HelloString(string para);
#if SILVERLIGHT
IAsyncResult Begin_HelloString(AsyncCallback callback, object state, string para);
string End_HelloString(IAsyncResult asyncResult);
#endif
int HelloInt(int para);
#if SILVERLIGHT
IAsyncResult Begin_HelloInt(AsyncCallback callback, object state, int para);
int End_HelloInt(IAsyncResult asyncResult);
#endif
bool HelloBoolean(bool para);
#if SILVERLIGHT
IAsyncResult Begin_HelloBoolean(AsyncCallback callback, object state, bool para);
bool End_HelloBoolean(IAsyncResult asyncResult);
#endif
void HelloVoid();
#if SILVERLIGHT
IAsyncResult Begin_HelloVoid(AsyncCallback callback, object state);
void End_HelloVoid(IAsyncResult asyncResult);
#endif
string HelloNull();
#if SILVERLIGHT
IAsyncResult Begin_HelloNull(AsyncCallback callback, object state);
string End_HelloNull(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_HelloString(AsyncCallback callback, object state, string para)
{
return send_HelloString(callback, state, para);
} public string End_HelloString(IAsyncResult asyncResult)
{
oprot_.Transport.EndFlush(asyncResult);
return recv_HelloString();
} #endif public string HelloString(string para)
{
#if !SILVERLIGHT
send_HelloString(para);
return recv_HelloString(); #else
var asyncResult = Begin_HelloString(null, null, para);
return End_HelloString(asyncResult); #endif
}
#if SILVERLIGHT
public IAsyncResult send_HelloString(AsyncCallback callback, object state, string para)
#else
public void send_HelloString(string para)
#endif
{
oprot_.WriteMessageBegin(new TMessage("HelloString", TMessageType.Call, seqid_));
HelloString_args args = new HelloString_args();
args.Para = para;
args.Write(oprot_);
oprot_.WriteMessageEnd();
#if SILVERLIGHT
return oprot_.Transport.BeginFlush(callback, state);
#else
oprot_.Transport.Flush();
#endif
} public string recv_HelloString()
{
TMessage msg = iprot_.ReadMessageBegin();
if (msg.Type == TMessageType.Exception) {
TApplicationException x = TApplicationException.Read(iprot_);
iprot_.ReadMessageEnd();
throw x;
}
HelloString_result result = new HelloString_result();
result.Read(iprot_);
iprot_.ReadMessageEnd();
if (result.__isset.success) {
return result.Success;
}
throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "HelloString failed: unknown result");
} #if SILVERLIGHT
public IAsyncResult Begin_HelloInt(AsyncCallback callback, object state, int para)
{
return send_HelloInt(callback, state, para);
} public int End_HelloInt(IAsyncResult asyncResult)
{
oprot_.Transport.EndFlush(asyncResult);
return recv_HelloInt();
} #endif public int HelloInt(int para)
{
#if !SILVERLIGHT
send_HelloInt(para);
return recv_HelloInt(); #else
var asyncResult = Begin_HelloInt(null, null, para);
return End_HelloInt(asyncResult); #endif
}
#if SILVERLIGHT
public IAsyncResult send_HelloInt(AsyncCallback callback, object state, int para)
#else
public void send_HelloInt(int para)
#endif
{
oprot_.WriteMessageBegin(new TMessage("HelloInt", TMessageType.Call, seqid_));
HelloInt_args args = new HelloInt_args();
args.Para = para;
args.Write(oprot_);
oprot_.WriteMessageEnd();
#if SILVERLIGHT
return oprot_.Transport.BeginFlush(callback, state);
#else
oprot_.Transport.Flush();
#endif
} public int recv_HelloInt()
{
TMessage msg = iprot_.ReadMessageBegin();
if (msg.Type == TMessageType.Exception) {
TApplicationException x = TApplicationException.Read(iprot_);
iprot_.ReadMessageEnd();
throw x;
}
HelloInt_result result = new HelloInt_result();
result.Read(iprot_);
iprot_.ReadMessageEnd();
if (result.__isset.success) {
return result.Success;
}
throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "HelloInt failed: unknown result");
} #if SILVERLIGHT
public IAsyncResult Begin_HelloBoolean(AsyncCallback callback, object state, bool para)
{
return send_HelloBoolean(callback, state, para);
} public bool End_HelloBoolean(IAsyncResult asyncResult)
{
oprot_.Transport.EndFlush(asyncResult);
return recv_HelloBoolean();
} #endif public bool HelloBoolean(bool para)
{
#if !SILVERLIGHT
send_HelloBoolean(para);
return recv_HelloBoolean(); #else
var asyncResult = Begin_HelloBoolean(null, null, para);
return End_HelloBoolean(asyncResult); #endif
}
#if SILVERLIGHT
public IAsyncResult send_HelloBoolean(AsyncCallback callback, object state, bool para)
#else
public void send_HelloBoolean(bool para)
#endif
{
oprot_.WriteMessageBegin(new TMessage("HelloBoolean", TMessageType.Call, seqid_));
HelloBoolean_args args = new HelloBoolean_args();
args.Para = para;
args.Write(oprot_);
oprot_.WriteMessageEnd();
#if SILVERLIGHT
return oprot_.Transport.BeginFlush(callback, state);
#else
oprot_.Transport.Flush();
#endif
} public bool recv_HelloBoolean()
{
TMessage msg = iprot_.ReadMessageBegin();
if (msg.Type == TMessageType.Exception) {
TApplicationException x = TApplicationException.Read(iprot_);
iprot_.ReadMessageEnd();
throw x;
}
HelloBoolean_result result = new HelloBoolean_result();
result.Read(iprot_);
iprot_.ReadMessageEnd();
if (result.__isset.success) {
return result.Success;
}
throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "HelloBoolean failed: unknown result");
} #if SILVERLIGHT
public IAsyncResult Begin_HelloVoid(AsyncCallback callback, object state)
{
return send_HelloVoid(callback, state);
} public void End_HelloVoid(IAsyncResult asyncResult)
{
oprot_.Transport.EndFlush(asyncResult);
recv_HelloVoid();
} #endif public void HelloVoid()
{
#if !SILVERLIGHT
send_HelloVoid();
recv_HelloVoid(); #else
var asyncResult = Begin_HelloVoid(null, null);
End_HelloVoid(asyncResult); #endif
}
#if SILVERLIGHT
public IAsyncResult send_HelloVoid(AsyncCallback callback, object state)
#else
public void send_HelloVoid()
#endif
{
oprot_.WriteMessageBegin(new TMessage("HelloVoid", TMessageType.Call, seqid_));
HelloVoid_args args = new HelloVoid_args();
args.Write(oprot_);
oprot_.WriteMessageEnd();
#if SILVERLIGHT
return oprot_.Transport.BeginFlush(callback, state);
#else
oprot_.Transport.Flush();
#endif
} public void recv_HelloVoid()
{
TMessage msg = iprot_.ReadMessageBegin();
if (msg.Type == TMessageType.Exception) {
TApplicationException x = TApplicationException.Read(iprot_);
iprot_.ReadMessageEnd();
throw x;
}
HelloVoid_result result = new HelloVoid_result();
result.Read(iprot_);
iprot_.ReadMessageEnd();
return;
} #if SILVERLIGHT
public IAsyncResult Begin_HelloNull(AsyncCallback callback, object state)
{
return send_HelloNull(callback, state);
} public string End_HelloNull(IAsyncResult asyncResult)
{
oprot_.Transport.EndFlush(asyncResult);
return recv_HelloNull();
} #endif public string HelloNull()
{
#if !SILVERLIGHT
send_HelloNull();
return recv_HelloNull(); #else
var asyncResult = Begin_HelloNull(null, null);
return End_HelloNull(asyncResult); #endif
}
#if SILVERLIGHT
public IAsyncResult send_HelloNull(AsyncCallback callback, object state)
#else
public void send_HelloNull()
#endif
{
oprot_.WriteMessageBegin(new TMessage("HelloNull", TMessageType.Call, seqid_));
HelloNull_args args = new HelloNull_args();
args.Write(oprot_);
oprot_.WriteMessageEnd();
#if SILVERLIGHT
return oprot_.Transport.BeginFlush(callback, state);
#else
oprot_.Transport.Flush();
#endif
} public string recv_HelloNull()
{
TMessage msg = iprot_.ReadMessageBegin();
if (msg.Type == TMessageType.Exception) {
TApplicationException x = TApplicationException.Read(iprot_);
iprot_.ReadMessageEnd();
throw x;
}
HelloNull_result result = new HelloNull_result();
result.Read(iprot_);
iprot_.ReadMessageEnd();
if (result.__isset.success) {
return result.Success;
}
throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "HelloNull failed: unknown result");
} }
public class Processor : TProcessor {
public Processor(Iface iface)
{
iface_ = iface;
processMap_["HelloString"] = HelloString_Process;
processMap_["HelloInt"] = HelloInt_Process;
processMap_["HelloBoolean"] = HelloBoolean_Process;
processMap_["HelloVoid"] = HelloVoid_Process;
processMap_["HelloNull"] = HelloNull_Process;
} protected delegate void ProcessFunction(int seqid, TProtocol iprot, TProtocol oprot);
private Iface 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 HelloString_Process(int seqid, TProtocol iprot, TProtocol oprot)
{
HelloString_args args = new HelloString_args();
args.Read(iprot);
iprot.ReadMessageEnd();
HelloString_result result = new HelloString_result();
result.Success = iface_.HelloString(args.Para);
oprot.WriteMessageBegin(new TMessage("HelloString", TMessageType.Reply, seqid));
result.Write(oprot);
oprot.WriteMessageEnd();
oprot.Transport.Flush();
} public void HelloInt_Process(int seqid, TProtocol iprot, TProtocol oprot)
{
HelloInt_args args = new HelloInt_args();
args.Read(iprot);
iprot.ReadMessageEnd();
HelloInt_result result = new HelloInt_result();
result.Success = iface_.HelloInt(args.Para);
oprot.WriteMessageBegin(new TMessage("HelloInt", TMessageType.Reply, seqid));
result.Write(oprot);
oprot.WriteMessageEnd();
oprot.Transport.Flush();
} public void HelloBoolean_Process(int seqid, TProtocol iprot, TProtocol oprot)
{
HelloBoolean_args args = new HelloBoolean_args();
args.Read(iprot);
iprot.ReadMessageEnd();
HelloBoolean_result result = new HelloBoolean_result();
result.Success = iface_.HelloBoolean(args.Para);
oprot.WriteMessageBegin(new TMessage("HelloBoolean", TMessageType.Reply, seqid));
result.Write(oprot);
oprot.WriteMessageEnd();
oprot.Transport.Flush();
} public void HelloVoid_Process(int seqid, TProtocol iprot, TProtocol oprot)
{
HelloVoid_args args = new HelloVoid_args();
args.Read(iprot);
iprot.ReadMessageEnd();
HelloVoid_result result = new HelloVoid_result();
iface_.HelloVoid();
oprot.WriteMessageBegin(new TMessage("HelloVoid", TMessageType.Reply, seqid));
result.Write(oprot);
oprot.WriteMessageEnd();
oprot.Transport.Flush();
} public void HelloNull_Process(int seqid, TProtocol iprot, TProtocol oprot)
{
HelloNull_args args = new HelloNull_args();
args.Read(iprot);
iprot.ReadMessageEnd();
HelloNull_result result = new HelloNull_result();
result.Success = iface_.HelloNull();
oprot.WriteMessageBegin(new TMessage("HelloNull", TMessageType.Reply, seqid));
result.Write(oprot);
oprot.WriteMessageEnd();
oprot.Transport.Flush();
} } #if !SILVERLIGHT
[Serializable]
#endif
public partial class HelloString_args : TBase
{
private string _para; public string Para
{
get
{
return _para;
}
set
{
__isset.para = true;
this._para = value;
}
} public Isset __isset;
#if !SILVERLIGHT
[Serializable]
#endif
public struct Isset {
public bool para;
} public HelloString_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.String) {
Para = 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("HelloString_args");
oprot.WriteStructBegin(struc);
TField field = new TField();
if (Para != null && __isset.para) {
field.Name = "para";
field.Type = TType.String;
field.ID = 1;
oprot.WriteFieldBegin(field);
oprot.WriteString(Para);
oprot.WriteFieldEnd();
}
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
finally
{
oprot.DecrementRecursionDepth();
}
} public override string ToString() {
StringBuilder __sb = new StringBuilder("HelloString_args(");
bool __first = true;
if (Para != null && __isset.para) {
if(!__first) { __sb.Append(", "); }
__first = false;
__sb.Append("Para: ");
__sb.Append(Para);
}
__sb.Append(")");
return __sb.ToString();
} } #if !SILVERLIGHT
[Serializable]
#endif
public partial class HelloString_result : TBase
{
private string _success; public string 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 HelloString_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.String) {
Success = 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("HelloString_result");
oprot.WriteStructBegin(struc);
TField field = new TField(); if (this.__isset.success) {
if (Success != null) {
field.Name = "Success";
field.Type = TType.String;
field.ID = 0;
oprot.WriteFieldBegin(field);
oprot.WriteString(Success);
oprot.WriteFieldEnd();
}
}
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
finally
{
oprot.DecrementRecursionDepth();
}
} public override string ToString() {
StringBuilder __sb = new StringBuilder("HelloString_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();
} } #if !SILVERLIGHT
[Serializable]
#endif
public partial class HelloInt_args : TBase
{
private int _para; public int Para
{
get
{
return _para;
}
set
{
__isset.para = true;
this._para = value;
}
} public Isset __isset;
#if !SILVERLIGHT
[Serializable]
#endif
public struct Isset {
public bool para;
} public HelloInt_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) {
Para = 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("HelloInt_args");
oprot.WriteStructBegin(struc);
TField field = new TField();
if (__isset.para) {
field.Name = "para";
field.Type = TType.I32;
field.ID = 1;
oprot.WriteFieldBegin(field);
oprot.WriteI32(Para);
oprot.WriteFieldEnd();
}
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
finally
{
oprot.DecrementRecursionDepth();
}
} public override string ToString() {
StringBuilder __sb = new StringBuilder("HelloInt_args(");
bool __first = true;
if (__isset.para) {
if(!__first) { __sb.Append(", "); }
__first = false;
__sb.Append("Para: ");
__sb.Append(Para);
}
__sb.Append(")");
return __sb.ToString();
} } #if !SILVERLIGHT
[Serializable]
#endif
public partial class HelloInt_result : TBase
{
private int _success; public int 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 HelloInt_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.I32) {
Success = 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("HelloInt_result");
oprot.WriteStructBegin(struc);
TField field = new TField(); if (this.__isset.success) {
field.Name = "Success";
field.Type = TType.I32;
field.ID = 0;
oprot.WriteFieldBegin(field);
oprot.WriteI32(Success);
oprot.WriteFieldEnd();
}
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
finally
{
oprot.DecrementRecursionDepth();
}
} public override string ToString() {
StringBuilder __sb = new StringBuilder("HelloInt_result(");
bool __first = true;
if (__isset.success) {
if(!__first) { __sb.Append(", "); }
__first = false;
__sb.Append("Success: ");
__sb.Append(Success);
}
__sb.Append(")");
return __sb.ToString();
} } #if !SILVERLIGHT
[Serializable]
#endif
public partial class HelloBoolean_args : TBase
{
private bool _para; public bool Para
{
get
{
return _para;
}
set
{
__isset.para = true;
this._para = value;
}
} public Isset __isset;
#if !SILVERLIGHT
[Serializable]
#endif
public struct Isset {
public bool para;
} public HelloBoolean_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.Bool) {
Para = iprot.ReadBool();
} 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("HelloBoolean_args");
oprot.WriteStructBegin(struc);
TField field = new TField();
if (__isset.para) {
field.Name = "para";
field.Type = TType.Bool;
field.ID = 1;
oprot.WriteFieldBegin(field);
oprot.WriteBool(Para);
oprot.WriteFieldEnd();
}
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
finally
{
oprot.DecrementRecursionDepth();
}
} public override string ToString() {
StringBuilder __sb = new StringBuilder("HelloBoolean_args(");
bool __first = true;
if (__isset.para) {
if(!__first) { __sb.Append(", "); }
__first = false;
__sb.Append("Para: ");
__sb.Append(Para);
}
__sb.Append(")");
return __sb.ToString();
} } #if !SILVERLIGHT
[Serializable]
#endif
public partial class HelloBoolean_result : TBase
{
private bool _success; public bool 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 HelloBoolean_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.Bool) {
Success = iprot.ReadBool();
} 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("HelloBoolean_result");
oprot.WriteStructBegin(struc);
TField field = new TField(); if (this.__isset.success) {
field.Name = "Success";
field.Type = TType.Bool;
field.ID = 0;
oprot.WriteFieldBegin(field);
oprot.WriteBool(Success);
oprot.WriteFieldEnd();
}
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
finally
{
oprot.DecrementRecursionDepth();
}
} public override string ToString() {
StringBuilder __sb = new StringBuilder("HelloBoolean_result(");
bool __first = true;
if (__isset.success) {
if(!__first) { __sb.Append(", "); }
__first = false;
__sb.Append("Success: ");
__sb.Append(Success);
}
__sb.Append(")");
return __sb.ToString();
} } #if !SILVERLIGHT
[Serializable]
#endif
public partial class HelloVoid_args : TBase
{ public HelloVoid_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("HelloVoid_args");
oprot.WriteStructBegin(struc);
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
finally
{
oprot.DecrementRecursionDepth();
}
} public override string ToString() {
StringBuilder __sb = new StringBuilder("HelloVoid_args(");
__sb.Append(")");
return __sb.ToString();
} } #if !SILVERLIGHT
[Serializable]
#endif
public partial class HelloVoid_result : TBase
{ public HelloVoid_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)
{
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("HelloVoid_result");
oprot.WriteStructBegin(struc); oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
finally
{
oprot.DecrementRecursionDepth();
}
} public override string ToString() {
StringBuilder __sb = new StringBuilder("HelloVoid_result(");
__sb.Append(")");
return __sb.ToString();
} } #if !SILVERLIGHT
[Serializable]
#endif
public partial class HelloNull_args : TBase
{ public HelloNull_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("HelloNull_args");
oprot.WriteStructBegin(struc);
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
finally
{
oprot.DecrementRecursionDepth();
}
} public override string ToString() {
StringBuilder __sb = new StringBuilder("HelloNull_args(");
__sb.Append(")");
return __sb.ToString();
} } #if !SILVERLIGHT
[Serializable]
#endif
public partial class HelloNull_result : TBase
{
private string _success; public string 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 HelloNull_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.String) {
Success = 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("HelloNull_result");
oprot.WriteStructBegin(struc);
TField field = new TField(); if (this.__isset.success) {
if (Success != null) {
field.Name = "Success";
field.Type = TType.String;
field.ID = 0;
oprot.WriteFieldBegin(field);
oprot.WriteString(Success);
oprot.WriteFieldEnd();
}
}
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
finally
{
oprot.DecrementRecursionDepth();
}
} public override string ToString() {
StringBuilder __sb = new StringBuilder("HelloNull_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();
} } }
}
HelloService定义了服务HelloService的五个方法,每个方法包含一个方法名,参数列表和返回类型。每个参数包括参数序号,参数类型以及参数名。包含了在 Hello.thrift 文件中描述的服务 HelloService 的接口定义,即HelloService.Iface 接口,以及服务调用的底层通信细节,包括客户端的调用逻辑HelloService.Client 以及服务器端的处理逻辑HelloService.Processor,用于构建客户端和服务器端的功能。
这样,C#版的接口代码就生成成功了。非常简单,下一篇,会具体的介绍服务器端如何实现调用这些接口和客户端如何调用这些接口。
Thrift是一款由Fackbook开发的可伸缩、跨语言的服务开发框架的更多相关文章
- Thrift 跨服务开发框架
Thrift概述 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP,Ruby, Erla ...
- Dapr + .NET Core实战(十三)跨语言开发
因为基于Dapr的服务架构是不限语言的,我们来看看Dapr的跨语言开发.我们使用golang,python,.NET来实现跨语言的服务调用,拓扑如下 我们继续使用.NET 5的fontend和back ...
- DbUtils是Apache出品一款简化JDBC开发的工具类
DbUtils - DbUtils是Apache出品一款简化JDBC开发的工具类 - 使用DbUtils可以让我们JDBC的开发更加简单 - DbUtils的使用: ...
- 开发者必知的8款App快速开发工具
开发者必知的8款App快速开发工具 “我有一个好创意,就差一个CTO……” “原生APP开发难度大,周期长,成本高,还没上线市场已经被占领了.” “APP版本迭代更新,都是企业的一道难关,没有一个一劳 ...
- Xsoup 是一款基于 Jsoup 开发的
Xsoup 是一款基于Jsoup 开发的,使用XPath抽取Html元素的工具.它被用于作者的爬虫框架 WebMagic 中,进行XPath 解析和抽取. 此次更新主要增加了一些XPath语法的支持. ...
- 开发者不可不知的五款DIY快速开发工具,你造吗
对于非专业的移动开发者,弱化编程能力的快发开发工具实用性够强,无需编程只要借助工具提供的各种功能模块,就能开发出属于自己的应用,而支持DIY更能使应用开发锦上添花,借助快速开发工具开发出属于自己的“能 ...
- 一款基于Netty开发的WebSocket服务器
代码地址如下:http://www.demodashi.com/demo/13577.html 一款基于Netty开发的WebSocket服务器 这是一款基于Netty框架开发的服务端,通信协议为We ...
- 4款java快速开发平台推荐
JBoss Seam JBoss Seam,算得上是Java开源框架里面最优秀的快速开发框架之一. Seam框架非常出色,尤其是他的组件机制设计的很有匠心,真不愧是Gavin King精心打造的框架了 ...
- 今天介绍一下自己的开源项目,一款以spring cloud alibaba为核心的微服务架构项目,为给企业与个人提供一个零开发基础的微服务架构。
LaoCat-Spring-Cloud-Scaffold 一款以spring cloud alibab 为核心的微服务框架,主要目标为了提升自己的相关技术,也为了给企业与个人提供一个零开发基础的微服务 ...
随机推荐
- Spark MLlib LDA 源代码解析
1.Spark MLlib LDA源代码解析 http://blog.csdn.net/sunbow0 Spark MLlib LDA 应该算是比較难理解的,当中涉及到大量的概率与统计的相关知识,并且 ...
- R语言-有负下标里才干有零
1.仅仅有负下标里才干有零 先看一个样例 >a<-c(1,2,3,4) >a[-1:1] > a[-1:1] Error in a[-1:1] : 仅仅有负下标里才干有零 (1 ...
- Font Awesome 图标如何使用
Font Awesome 图标如何使用 一.总结 一句话总结:Font Awesome字体图标非常好用,直接引入font-awesome.css,然后就可以直接使用了,使用的时候是用的i标签. 1.字 ...
- 1.10 Python基础知识 - 序列:列表
在Python中有很多的组合数据类型,其中包括列表,元组,字符串等数据类型,这些数据类型统称为序列类型,用他们可以处理复杂的数据. 列表,是一组有序元素组合的数据结构.列表是可变的数据类型. 列表采用 ...
- 福昕pdf阅读器如何删除所有注释
然后选中第一个 移动到最后按住shift,选择最后一个, 总之就是选中所有的 然后右键,点击删除即可. 不要忘记保存呦
- ThinkPHP5.0的安装
ThinkPHP5.0的安装很简单: 1.下载“phpstudy”安装 2.下载thinkphp源文件 3.把thinkphp源文件解压并放到phpstudy目录下的“WWW”目录 4.然后开启服务并 ...
- 洛谷 P2807 三角形计数
P2807 三角形计数 题目背景 三角形计数(triangle) 递推 题目描述 把大三角形的每条边n等分,将对应的等分点连接起来(连接线分别平行于三条边),这样一共会有多少三角形呢?编程来解决这个问 ...
- poj 1191 棋盘切割 (压缩dp+记忆化搜索)
一,题意: 中文题 二.分析: 主要利用压缩dp与记忆化搜索思想 三,代码: #include <iostream> #include <stdio.h> #include & ...
- amazeui学习笔记--css(布局相关1)--网格Grid
amazeui学习笔记--css(布局相关1)--网格Grid 一.总结 基本使用 1.div+class布局:amaze里面采取的就是div+class的布局方式 <div class=&q ...
- JS学习笔记 - 透明度运动框
该练习的笔记如下: 1. var cur=0; //先声明一个变量. 2. parseInt会舍掉小数,而opacity值恰恰是小数,所以对于opacity,必须用parseFloat. cur ...