window go thrift
阿新 • • 發佈:2018-08-31
pen 拷貝 技術分享 main apache mat nts tps [] 代碼在GitHub上托管
org.springframework.boot
spring-boot-starter-parent
1.5.3.RELEASE
4.0.0
java-thrift-server
org.springframework.boot
spring-boot-starter
com.test.thrift
java-thrift-idl
1.0-SNAPSHOT
```
- 添加appliaction.properties配置文件
> server.name=java-thrift-service
server.port=8082
- 更新FormatDataImpl文件,添加@Service註解,也就是交給spring 管理起來
- 創建thirift_socket包,創建ThriftServer文件,編寫Thrift邏輯
- 可能會報錯,如果是could not autowire. no bean of Iface的話,可以不用管
- 更新server文件,添加上@SpringBootApplication 註解,表明是啟動類
- 測試
> 啟動服務端
啟動客戶端
https://github.com/xej520/xingej-thrift/tree/master/hw-thrift
環境說明
- windows 10
- Intellij IDEA
- thrift-0.11.0.exe
- 服務端用java實現
- 客戶端用go實現
- 用例的作用是,客戶端將字符串傳遞給服務器,服務器將字符串轉換成大寫後,返回給客戶端
創建maven工程(父模塊)
刪除自帶的src目錄(目前沒用,刪掉)
準備IDL文件
- 創建目錄thrift
-
創建IDL文件hw.thrift
namespace java com.test.thrift namespace go pkg.service struct Data { 1: string text; } service format_data { Data doFormat(1:Data data); }
創建maven模塊,用於存儲生成的java版本的代碼庫
- 利用thrift來生成java版本的代碼庫
thrift --gen java -out ../java-thrift-idl/src/main/java hw.thrift -
生成的代碼報錯,是由於缺少libthrift庫引起的,因此在pom.xml文件中,添加必要的依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>hw-thrift</artifactId> <groupId>com.test.thrift</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>java-thrift-idl</artifactId> <dependencies> <dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.11.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
</project>
- 添加完依賴@Override 註解報錯的話,可能是由於thrift的版本不一致引起的。 ## 創建go模塊,用於存儲生成go版本的代碼庫 - 利用thrift來生成go版本的代碼庫 thrift --gen go -out ../go-thrift-idl hw.thrift ![](https://note.youdao.com/yws/public/resource/9c8584c6ec980aee585665c38a65bf9d/xmlnote/42EAB0BE237340D6A8989EEF383753DE/20079) - 註意生成的代碼是有問題的[原因具體不詳] - 在hw.go文件中 oprot.Flush()拋異常,not enough arguments in call to oprot.Flush less... (Ctrl+F1) - 解決措施:原因是缺少context.Context類型的參數,剛好方法中已經有,直接添加上就可以了,改成oprot.Flush(ctx) - 將生成的代碼庫pkg 拷貝到gopath的src路徑下,這樣客戶端就可以使用代碼庫了,不然有可能找不到生成的代碼庫 ## 創建maven模塊,用於創建服務端 - 創建maven模塊,java-thrift-server - 更新pom.xml文件,添加對java-thrift-idl的依賴 - 編寫FormatDataImpl實現hw.thrift定義的接口 - 編寫sever, 實現thrift編程 ## 創建go模塊,用於go版本的客戶端 - 創建go模塊,go-thrift-client - 編寫業務邏輯 ## 測試 - 啟動服務器端 - 啟動客戶端 > 將服務端更改spring-boot 版本 > 也就是更新java-thrift-server 模塊 - 更新java-thrift-server模塊的pom文件,為下面的形式
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
window go thrift