前面介紹過 Thrift 安裝和使用,介紹了Thrift服務的發佈和客戶端調用,能夠查看我以前的文章:https://www.cnblogs.com/zhangweizhong/category/1006119.htmlhtml
可是,以前介紹的都是單向的客戶端發送消息,服務端接收消息。而客戶端卻得不到服務器的響應。web
那若是咱們要實現雙向通訊(即:客戶端發送請求,服務端處理返回,服務端發送消息,客戶端處理返回)的功能,該怎麼實現呢?api
其實在不涉及語言平臺的制約,WebService或是webapi 就能夠實現這種客戶端發起請求,服務端的處理的單向流程。服務器
然而,實際場景中,可能咱們的某些業務需求,更須要服務端可以響應請求並處理數據。下面我經過一個demo案例,介紹下Thrift 是如何實現雙向通訊的。框架
這裏再也不贅述,戳這裏查看我上篇文章的介紹:https://www.cnblogs.com/zhangweizhong/category/1006119.htmlsocket
編寫thrift腳本,命名爲student.thrift 以下:測試
service HelloWorldService{ void SayHello(1:string msg); }
生成service 的方法,以前的文章有介紹,這裏就不介紹了。spa
HelloWorldBidirectionServer實現了Iface接口用於接收客戶端消息,並有一個客戶端傳輸層對象集合用於記錄全部已鏈接的客戶端。線程
public class HelloWorldBidirectionServer : HelloWorldBidirectionService.Iface { public void Run(int port) { try { TServerTransport transport = new TServerSocket(port); TTransportFactory transportFac = new TTransportFactory(); TProtocolFactory inputProtocolFactory = new TBinaryProtocol.Factory(); TThreadPoolServer server = new TThreadPoolServer(getProcessorFactory(), transport, transportFac, inputProtocolFactory); server.Serve(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static List<TTransport> TransportCollection = new List<TTransport>(); public void SayHello(string msg) { Console.WriteLine(string.Format("{0:yyyy/MM/dd hh:mm:ss} 服務端接收到消息: {1}", DateTime.Now, msg)); } public void SayToClient(string msg) { try { foreach (TTransport trans in TransportCollection) { TBinaryProtocol protocol = new TBinaryProtocol(trans); HelloWorldBidirectionService.Client client = new HelloWorldBidirectionService.Client(protocol); //Thread.Sleep(1000); client.SayHello(msg); //Console.WriteLine("發給了客戶端喲"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } public TProcessorFactory getProcessorFactory() { return new HelloWorldBidirectionProcessor(); } } public class HelloWorldBidirectionProcessor : TProcessorFactory { public TProcessor GetProcessor(TTransport trans, TServer server = null) { if (trans.IsOpen) { HelloWorldBidirectionServer.TransportCollection.Add(trans); Console.WriteLine("客戶端連上。"); } HelloWorldBidirectionServer srv = new HelloWorldBidirectionServer(); return new global::HelloWorldBidirectionService.Processor(srv); } }
首先定義一個能夠接收服務端消息的類,裏面只有一個實現Iface接口的方法:code
public class HelloWorldBidirectionClient { static HelloWorldBidirectionService.Client client = null; public void ConnectAndListern(int port, string ip = "127.0.0.1") { //Tsocket: TCP/IP Socket接口 TSocket tSocket = new TSocket(ip, port); //消息結構協議 TProtocol protocol = new TBinaryProtocol(tSocket); try { if (client == null) { client = new global::HelloWorldBidirectionService.Client(protocol); tSocket.Open();//創建鏈接 StartListern(tSocket);//啓動監聽線程 } } catch (Exception ex) { Console.WriteLine(ex.Message); } } public void Say(string msg) { if (client != null) client.SayHello(msg); } void StartListern(TSocket tSocket) { Thread t = new Thread(new ParameterizedThreadStart(Run)); t.Start(tSocket); } public void Run(object tSocket) { HelloWorldBidirectionService.Processor process = new HelloWorldBidirectionService.Processor(new HelloWorldBidirectionFace()); try { while (process.Process(new TBinaryProtocol((TSocket)tSocket), new TBinaryProtocol((TSocket)tSocket))) { Console.WriteLine("消息接收完成,等下一波,阻塞中......"); } } catch (Exception ex) { Console.WriteLine("鏈接斷開..." + ex.Message); } } } class HelloWorldBidirectionFace : HelloWorldBidirectionService.Iface { public void SayHello(string msg) { Console.WriteLine(string.Format("{0:yyyy/MM/dd hh:mm:ss} 收到服務端響應消息 {1}", DateTime.Now, msg)); } }
實現客戶端,ConnectAndListern方法能夠與服務端創建鏈接,並開啓客戶端端口監聽來自服務端的信息。Say方法可將消息發送至服務端。
1. 關於使用Thrift 構建咱們本身的rpc 的方法,這裏基本講完了。其餘的方法本文就再也不演示了,調用起來都是同樣。
2. 後續會簡單討論一下Thrift 框架的通訊原理。
3. 源代碼下載,HelloThrift.rar