==> 學習彙總(持續更新)
==> 從零搭建後端基礎設施系列(一)-- 背景介紹
因爲第一部分只是將最小系統骨架搭建出來,能部署,接口調通就行。
PS:目前我也沒想到有什麼有難度,又有趣的項目,所以在寫第一部分的時候,就先搭個骨架,第二部分再想想寫個什麼好,總得給自己找點樂趣,哈哈。
一、編寫thrift文件,生成java文件
1.創建一個maven工程
什麼都不選,一路next
2.創建完後,將src目錄刪掉
創建之後
刪掉src之後
3.接着右鍵單擊thriftservice文件夾,選擇新建一個module,也是選擇maven工程
創建完成後如下所示
4.工程建好之後,接下來開始編寫thrift文件了,首先先把thrift生成插件引入,以及thrift靜態庫引入
<dependencies>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.acme.plugins</groupId>
<artifactId>gen-thrift</artifactId>
<version>1.0.0-SNAPSHOT</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
開始編寫thrift文件
Dto.thrift
namespace java com.acme.service.Dto
include 'Enums.thrift'
struct Expression {
1:required Enums.OPERATE op;
2:double num1;
3:double num2;
}
struct ExpressionResult {
1:double result;
}
Enums.thrift
namespace java com.acme.service.Enums
enum OPERATE {
ADD,
SUB,
MUL,
DIV,
}
Exception.thrift
namespace java com.acme.service.Exception
exception MinSystemException {
1:i32 code;
2:string message;
}
Service.thrift
namespace java com.acme.service.service
include 'Dto.thrift'
include 'Exception.thrift'
service TCalculate {
Dto.ExpressionResult calculate(1:Dto.Expression exp) throws (1:Exception.MinSystemException ex);
}
注意:如果不加上namespace包名,那麼打成jar包的時候,別的項目也只能在java包下的類能引用到。
編寫完,結構如圖所示
接下來運行插件,生成java文件
運行完如下圖所示(嘿嘿,還挺好用,就是第一次run的時候,gen-java目錄老是亂入,需要再run一次,以後改進一下)
5.最後將thrift-api打成jar包
記得設置版本號和打包方式
然後用maven插件打包即可
二、編寫thrift服務
其實就是實現thrift定義的接口
1.創建springboot工程
新建一個module
因爲是thrift服務,所以不需要web組件,就選一個lombok就行了,這個後續再介紹,這個只是寫一些domain的時候,更簡潔。
創建完成的結構如圖
2.實現thrift接口
min-system-service引入min-system-core的包
<dependency>
<groupId>com.acme.service</groupId>
<artifactId>min-system-core</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
編寫TCalculateImpl類,實現TCalculate.Iface同步接口
package com.acme.service.minsystemservice.thriftservice;
import com.acme.service.Dto.Expression;
import com.acme.service.Dto.ExpressionResult;
import com.acme.service.Exception.MinSystemException;
import com.acme.service.service.TCalculate;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
/**
* @author acme
* @date 2019/7/24 11:41 PM
*/
@Service
public class TCalculateImpl implements TCalculate.Iface {
@Override
public ExpressionResult calculate(Expression exp) throws MinSystemException, TException {
switch (exp.getOp()){
case ADD: return new ExpressionResult(exp.getNum1() + exp.getNum2());
case SUB: return new ExpressionResult(exp.getNum1() - exp.getNum2());
case MUL: return new ExpressionResult(exp.getNum1() * exp.getNum2());
case DIV: {
if(exp.getNum2() == 0){
throw new MinSystemException(1000, "除數不能爲0");
}
return new ExpressionResult(exp.getNum1() / exp.getNum2());
}
}
return null;
}
}
3.編寫thrift服務端啓動代碼
@SpringBootApplication
public class MinSystemServiceApplication {
public static void main(String[] args) throws TTransportException {
SpringApplication.run(MinSystemServiceApplication.class, args);
start();
}
public static void start() throws TTransportException {
//創建一個處理器,看代碼最後new TCalculateImpl()就知道,thrift需要知道,是哪個類來處理接口的請求。
TProcessor tProcessor = new TCalculate.Processor<TCalculate.Iface>(new TCalculateImpl());
//創建一個服務端socket,最簡單的,只需要指定綁定的端口
TServerSocket serverTransport = new TServerSocket(8080);
//暫且理解爲使用thrift的默認傳輸協議,我也沒具體研究
TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
//將參數設置進去
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(tProcessor);
tArgs.protocolFactory(protocolFactory);
// 創建 TServer
TServer server = new TSimpleServer(tArgs);
// 啓動 thrift.Server
System.out.println("[info] min system bootup successful on ip:");
server.serve();
}
}
4.編寫thrift客戶端測試代碼
@Test
public void testConnect() throws TException {
// 創建 TTransport
TTransport transport = new TSocket("127.0.0.1", 8080, 2000);
// 創建 TProtocol
TProtocol protocol = new TBinaryProtocol(transport);
// 創建客戶端.
TCalculate.Client client = new TCalculate.Client(protocol);
// 打開 TTransport
transport.open();
// 調用服務方法
ExpressionResult result = client.calculate(new Expression(OPERATE.ADD, 1, 2));
System.out.println(result.getResult());
transport.close();
}
5.測試接口
先啓動服務端,然後啓動測試代碼調用接口
啓動成功,接着啓動測試代碼
測試1+2
測試1/0
總結搭建thrift後端服務步驟:
- 編寫thrift文件,生成java文件
- 將生成的java文件打成jar包
- 創建springboot模塊,引入上面的core包
- 實現接口
- 編寫服務端啓動代碼
- 編寫客戶端測試代碼
- 測試接口
==> CODE