ApiBoot Resource Load
是一款资源与业务彻底分离的基础框架,能够整合微服务(Feign、OpenFeign)
进行负载均衡读取固定类型、固定所属业务的资源信息,遵循必定的资源存储规则
完成自动化
资源读取、添加、更新、删除、缓存等。java
在pom.xml
配置文件内添加以下依赖:git
<!--ApiBoot Resource Load-->
<dependency>
<groupId>org.minbox.framework</groupId>
<artifactId>api-boot-starter-resource-load</artifactId>
</dependency>
复制代码
ApiBoot
所提供的依赖都不须要添加版本号,可是须要添加版本依赖,具体查看ApiBoot版本依赖github
ApiBootResourceStoreDelegate
是一个资源数据读取的委托驱动接口,在使用ApiBoot Resource Load
时,须要实现该接口完成资源的读取方法loadResourceUrl()
,该方法的参数以下所示:api
sourceFieldValue
,是查询资源的业务编号,具体的配置详见下面的示例。resourceType
,是查询资源的类型,相同的业务编号下颇有可能存在多种类型,好比:用户编号对应用户头像、用户封面等。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("查询资源的业务逻辑字段值:{}", sourceFieldValue);
logger.info("资源类型:{}", resourceType);
return Arrays.asList(new String[]{"http://test.oss.com/111.png"});
}
}
复制代码
loadResourceUrl
方法须要返回根据resourceFieldValue
、resourceType
字段查询到的资源列表。负载均衡
@ResourceLoad
框架
标注方法须要进行ApiBoot Resource Load
自动化读取资源信息,该注解必须添加,且只能添加在方法上。ide
@ResourceFields
微服务
@ResourceField
注解的集合测试
@ResourceField
配置@ResourceLoad
标注的方法具体有哪些字段须要进行资源的自动映射,参数解释以下所示:
name
:查询资源后设置到类内Field
的名称source
:查询资源所需的业务逻辑编号类内Field
的名称type
:资源类型,自行定义isArray
:接收查询后资源的Field
类型是否为array
,true:arrayisList
:接收查询后资源的Field
类型是否为list
,true:list资源加载通常都是实体类的方式进行返回的,下面咱们先来建立一个实体类方便示例测试,以下所示:
/** * 示例对象 */
@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;
}
复制代码
返回值为单对象资源加载示例:
/** * 返回值为单个对象的示例 * * @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
,并且配置的业务资源编号都是userId
字段,这两个字段也就是会传递给ApiBootResourceStoreDelegate#loadResourceUrl
方法做为参数。其中
HEAD_IMAGE
读取到的资源路径设置到SampleUserInfo
类内的headImage
,SHORT_IMAGE
读取到的资源路径设置到SampleUserInfo
类内的shortImage
字段。
注意:若是你的方法返回对象只有一个资源对象须要映射,能够单独配置使用
@ResourceField
注解。
/** * 返回值为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;
}
复制代码
在上面,会为返回值list
内的每个SampleUserInfo
对象进行设置查询的资源信息。
/** * 返回值为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
类型做为返回值时,其中注意map -> value
必须是对象类型。
若是你有想要的使用方式,你就能够提交issuse!!!