使用Thrift實現(xiàn)跨語言RPC的調(diào)用
前言
前面我們在Thrift入門里面實現(xiàn)了Thrift實現(xiàn)RPC調(diào)用的簡單案例,而Thrift最大的優(yōu)勢就是可以實現(xiàn)跨語言RPC調(diào)用,尤其在一些大廠,微服務各模塊之間使用不同的語言是很常見的,有用java的,有g(shù)o的,有python的,因此,選用Thrift實現(xiàn)RPC遠程調(diào)用是很不錯的選擇,本節(jié)將在RPC 框架之Thrift入門(一)的案例基礎(chǔ)上,使用java作為服務端,用python作為客戶端,實現(xiàn)不同語言之間的RPC調(diào)用!
IDL代碼
namespace java com.aniu.service namespace py thrift_demo struct Person { // 定義 Person 結(jié)構(gòu)體 1: required string name; // 姓名,必選字段 2: required i32 age; // 年齡,必選字段 3: optional string sex; // 性別,可選字段 } service PersonService { // 定義 PersonService 服務接口 Person getByName(1: string name); // 根據(jù)姓名獲取 Person 信息 bool save(1: Person person); // 保存 Person 信息 }
分別使用以下代碼生成java和python的代碼
thrift --gen java person.thrift
thrift --gen py person.thrift
代碼
服務端與業(yè)務邏輯
服務端代碼和業(yè)務邏輯就是RPC 框架之Thrift入門(一)的案例里面的
package com.aniu.server; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; import com.aniu.service.PersonService; import com.aniu.service.impl.PersonServiceImpl; public class Server { public static void main(String[] args) { try{ // 創(chuàng)建一個新的 Thrift 服務端套接字,監(jiān)聽在端口 9000 上 TServerSocket socket = new TServerSocket(9000); // 創(chuàng)建一個 PersonService 的 Processor。Processor 是 Thrift 中用于處理請求的接口,它需要一個實現(xiàn)了 PersonService 接口的對象作為參數(shù)。 PersonService.Processor<PersonServiceImpl> processor = new PersonService.Processor<>(new PersonServiceImpl()); // 創(chuàng)建一個二進制協(xié)議工廠對象。Thrift 支持多種協(xié)議,如 TBinaryProtocol、TCompactProtocol、TJSONProtocol 等,這里選擇的是二進制協(xié)議。 TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory(); // 創(chuàng)建一個 TSimpleServer 的參數(shù)對象 args1,并將之前創(chuàng)建的套接字、Processor 和協(xié)議工廠設(shè)置為其屬性。 TServer.Args args1 = new TSimpleServer.Args(socket); args1.processor(processor); args1.protocolFactory(factory); // 使用之前設(shè)置好的參數(shù)創(chuàng)建 TSimpleServer 對象 TSimpleServer tSimpleServer = new TSimpleServer(args1); // 開始執(zhí)行 TSimpleServer,開始監(jiān)聽并處理客戶端的請求。 tSimpleServer.serve(); }catch (Exception e){ System.out.println(e); } } }
package com.aniu.service.impl; import com.aniu.service.Person; import com.aniu.service.PersonService; import org.apache.thrift.TException; public class PersonServiceImpl implements PersonService.Iface { @Override public Person getByName(String name) throws TException { return new Person(name,18); } @Override public boolean save(Person person) throws TException { return false; } }
客戶端
接下來我們就用python編寫Thrift客戶端來實現(xiàn)RPC調(diào)用 java服務端首先安裝thrift包:
pip install thrift
將生成的python代碼拷貝到項目,然后編寫python客戶端!
from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift_demo import PersonService # 創(chuàng)建Thrift服務端的地址和端口 host = 'localhost' port = 9000 # 創(chuàng)建Thrift傳輸層和協(xié)議層 t_socket = TSocket.TSocket(host, port) transport = TTransport.TBufferedTransport(t_socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) # 創(chuàng)建Thrift客戶端 client = PersonService.Client(protocol) # 打開Thrift傳輸層連接 transport.open() # 調(diào)用Thrift客戶端提供的接口 try: # 調(diào)用getName方法 person = client.getByName("aniu") print(person) except Exception as e: print('Error:', str(e)) # 關(guān)閉Thrift傳輸層連接 transport.close()
首先啟動java服務端,然后啟動python客戶端,可以看到調(diào)用成功!
結(jié)語
對于thrift的基本案例就寫完了,這些案例使用的thrift服務端模型都是TSimpleServer,同時只支持一個socket連接,用于我們這些小案例測試,后續(xù)總結(jié)springboot整合thrift的代碼,我們使用其他thrift服務端模型!
到此這篇關(guān)于使用Thrift實現(xiàn)跨語言RPC的調(diào)用的文章就介紹到這了,更多相關(guān)Thrift跨語言RPC調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot整合springsecurity與mybatis-plus的簡單實現(xiàn)
Spring Security基于Spring開發(fā),項目中如果使用Spring作為基礎(chǔ),配合Spring Security做權(quán)限更加方便,而Shiro需要和Spring進行整合開發(fā)。因此作為spring全家桶中的Spring Security在java領(lǐng)域很常用2021-10-10Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來的
這篇文章主要介紹了Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06深入研究spring boot集成kafka之spring-kafka底層原理
這篇文章主要深入研究了spring boot集成kafka如何實現(xiàn)spring-kafka的底層原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02SpringBoot整合Quartz實現(xiàn)動態(tài)配置的代碼示例
這篇文章將介紹如何把Quartz定時任務做成接口,實現(xiàn)以下功能的動態(tài)配置添加任務,修改任務,暫停任務,恢復任務,刪除任務,任務列表,任務詳情,文章通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-07-07Java 如何實現(xiàn)照片轉(zhuǎn)化為回憶中的照片
本文主要介紹了可以對圖片進行色彩處理的Java工具類,讓圖片變成回憶中的畫面,主要將圖片做黑白與褐色的處理。代碼具有一定價值,感興趣的童鞋可以關(guān)注一下2021-11-11