ApiBoot Resource Load
ApiBoot Resource Load
是一款資源與業(yè)務(wù)完全分離的基礎(chǔ)框架,可以整合微服務(wù)(Feign噪服、OpenFeign)
進(jìn)行負(fù)載均衡讀取固定類型邓了、固定所屬業(yè)務(wù)的資源信息恨诱,遵循一定的資源存儲(chǔ)規(guī)則
完成自動(dòng)化
資源讀取、添加骗炉、更新照宝、刪除、緩存等句葵。
使用場(chǎng)景
- 業(yè)務(wù)圖片存儲(chǔ)
- 業(yè)務(wù)音頻厕鹃、視頻文件存儲(chǔ)
- 業(yè)務(wù)文件
- 其他資源文件...
引入 ApiBoot Resource Load
在pom.xml
配置文件內(nèi)添加如下依賴:
<!--ApiBoot Resource Load-->
<dependency>
<groupId>org.minbox.framework</groupId>
<artifactId>api-boot-starter-resource-load</artifactId>
</dependency>
ApiBoot
所提供的依賴都不需要添加版本號(hào),但是需要添加版本依賴乍丈,具體查看ApiBoot版本依賴
了解ApiBootResourceStoreDelegate
ApiBootResourceStoreDelegate
是一個(gè)資源數(shù)據(jù)讀取的委托驅(qū)動(dòng)接口剂碴,在使用ApiBoot Resource Load
時(shí),需要實(shí)現(xiàn)該接口完成資源的讀取方法loadResourceUrl()
轻专,該方法的參數(shù)如下所示:
- 第一個(gè)參數(shù)
sourceFieldValue
忆矛,是查詢資源的業(yè)務(wù)編號(hào),具體的配置詳見下面的示例请垛。 - 第二個(gè)參數(shù)
resourceType
催训,是查詢資源的類型,相同的業(yè)務(wù)編號(hào)下很有可能存在多種類型宗收,比如:用戶編號(hào)對(duì)應(yīng)用戶頭像漫拭、用戶封面等。
ApiBootResourceStoreDelegate示例:
// 示例
@Service
public class ResourceLoadService implements ApiBootResourceStoreDelegate {
/**
* logger instance
*/
static Logger logger = LoggerFactory.getLogger(ResourceLoadService.class);
@Override
public List<String> loadResourceUrl(String sourceFieldValue, String resourceType) throws ApiBootException {
logger.info("查詢資源的業(yè)務(wù)邏輯字段值:{}", sourceFieldValue);
logger.info("資源類型:{}", resourceType);
return Arrays.asList(new String[]{"http://test.oss.com/111.png"});
}
}
loadResourceUrl
方法需要返回根據(jù)resourceFieldValue
混稽、resourceType
字段查詢到的資源列表嫂侍。
內(nèi)置注解
-
@ResourceLoad
標(biāo)注方法需要進(jìn)行
ApiBoot Resource Load
自動(dòng)化讀取資源信息,該注解必須添加荚坞,且只能添加在方法上挑宠。 -
@ResourceFields
@ResourceField
注解的集合 -
@ResourceField
配置
@ResourceLoad
標(biāo)注的方法具體有哪些字段需要進(jìn)行資源的自動(dòng)映射,參數(shù)解釋如下所示:-
name
:查詢資源后設(shè)置到類內(nèi)Field
的名稱 -
source
:查詢資源所需的業(yè)務(wù)邏輯編號(hào)類內(nèi)Field
的名稱 -
type
:資源類型颓影,自行定義 -
isArray
:接收查詢后資源的Field
類型是否為array
各淀,true:array -
isList
:接收查詢后資源的Field
類型是否為list
,true:list
-
單對(duì)象資源加載
資源加載一般都是實(shí)體類的方式進(jìn)行返回的诡挂,下面我們先來創(chuàng)建一個(gè)實(shí)體類方便示例測(cè)試碎浇,如下所示:
/**
* 示例對(duì)象
*/
@Data
class SampleUserInfo {
public SampleUserInfo(String userId, int age) {
this.userId = userId;
this.age = age;
}
private String userId;
private String headImage;
private String shortImage;
private int age;
}
返回值為單對(duì)象資源加載示例:
/**
* 返回值為單個(gè)對(duì)象的示例
*
* @return
*/
@ResourceLoad
@ResourceFields({
@ResourceField(name = "headImage", source = "userId", type = "HEAD_IMAGE"),
@ResourceField(name = "shortImage", source = "userId", type = "SHORT_IMAGE")
})
public SampleUserInfo singleObjectSample() {
return new SampleUserInfo("yuqiyu", 24);
}
在上面临谱,我們配置讀取兩種類型的資源,分別是:
HEAD_IMAGE
奴璃、SHORT_IMAGE
悉默,而且配置的業(yè)務(wù)資源編號(hào)都是userId
字段,這兩個(gè)字段也就是會(huì)傳遞給ApiBootResourceStoreDelegate#loadResourceUrl
方法作為參數(shù)苟穆。其中
HEAD_IMAGE
讀取到的資源路徑設(shè)置到SampleUserInfo
類內(nèi)的headImage
抄课,SHORT_IMAGE
讀取到的資源路徑設(shè)置到SampleUserInfo
類內(nèi)的shortImage
字段。
注意:如果你的方法返回對(duì)象只有一個(gè)資源對(duì)象需要映射雳旅,可以單獨(dú)配置使用
@ResourceField
注解跟磨。
List集合資源加載
/**
* 返回值為list集合的示例
*
* @return
*/
@ResourceLoad
@ResourceFields({
@ResourceField(name = "headImage", source = "userId", type = "HEAD_IMAGE"),
@ResourceField(name = "shortImage", source = "userId", type = "SHORT_IMAGE")
})
public List<SampleUserInfo> listSample() {
List<SampleUserInfo> users = new ArrayList();
users.add(new SampleUserInfo("yuqiyu", 24));
users.add(new SampleUserInfo("hengboy", 24));
return users;
}
在上面,會(huì)為返回值list
內(nèi)的每一個(gè)SampleUserInfo
對(duì)象進(jìn)行設(shè)置查詢的資源信息攒盈。
Map集合資源加載
/**
* 返回值為map集合的示例
*
* @return
*/
@ResourceLoad
@ResourceFields({
@ResourceField(name = "headImage", source = "userId", type = "HEAD_IMAGE"),
@ResourceField(name = "shortImage", source = "userId", type = "SHORT_IMAGE")
})
public Map<String, SampleUserInfo> mapSample() {
Map<String, SampleUserInfo> users = new HashMap<>(2);
users.put("yuqiyu", new SampleUserInfo("yuqiyu", 24));
users.put("hengboy", new SampleUserInfo("hengboy", 24));
return users;
}
Map
類型作為返回值時(shí)抵拘,其中注意map -> value
必須是對(duì)象類型。
如果你有想要的使用方式型豁,你就可以提交issuse=┲搿!迎变!