关于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两个方法。 、
1 2 3 4 5 6 7 8 9 10 | struct User { 1: i32 ID 2: string Name } service UserService { User GetUserByID(1:i32 userID) list<User> GetAllUser() } |
然后,通过上面下载的Thrif.exe命令行工具,进行代码的生成,windows平台,cmd进入thrift.exe所在目录,执行如下命令:
1 | thrift --gen csharp user.thrift |
命令格式如:
1 | thrift --gen <language> <Thrift filename> |
也可以把thrift.exe放到指定目录或C盘位置,配置环境变量Path,然后可以直接执行以上命令。
执行完以上命令后,会在当前目录下,生成一个名为gen-csharp的文件夹,里面包含了2个类(User.cs和UserService.cs)。
User.cs代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | /** * 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代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 | /** * 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服务端吧。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /// <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中定义的成员。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /// <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客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /// <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(); } |
完成以上步骤后,让我们启动服务端和客户端,来看看效果吧!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?