ApiBoot - ApiBoot Resource Load 使用文档

ApiBoot Resource Load

ApiBoot Resource Load是一款资源与业务彻底分离的基础框架,能够整合微服务(Feign、OpenFeign)进行负载均衡读取固定类型、固定所属业务的资源信息,遵循必定的资源存储规则完成自动化资源读取、添加、更新、删除、缓存等。java

使用场景

  • 业务图片存储
  • 业务音频、视频文件存储
  • 业务文件
  • 其余资源文件...

引入 ApiBoot Resource Load

pom.xml配置文件内添加以下依赖:git

<!--ApiBoot Resource Load-->
<dependency>
  <groupId>org.minbox.framework</groupId>
  <artifactId>api-boot-starter-resource-load</artifactId>
</dependency>
复制代码

ApiBoot所提供的依赖都不须要添加版本号,可是须要添加版本依赖,具体查看ApiBoot版本依赖github

了解ApiBootResourceStoreDelegate

ApiBootResourceStoreDelegate是一个资源数据读取的委托驱动接口,在使用ApiBoot Resource Load时,须要实现该接口完成资源的读取方法loadResourceUrl(),该方法的参数以下所示:api

  1. 第一个参数sourceFieldValue,是查询资源的业务编号,具体的配置详见下面的示例。
  2. 第二个参数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方法须要返回根据resourceFieldValueresourceType字段查询到的资源列表。负载均衡

内置注解

  • @ResourceLoad框架

    标注方法须要进行ApiBoot Resource Load自动化读取资源信息,该注解必须添加,且只能添加在方法上。ide

  • @ResourceFields微服务

    @ResourceField注解的集合测试

  • @ResourceField

    配置@ResourceLoad标注的方法具体有哪些字段须要进行资源的自动映射,参数解释以下所示:

    • name:查询资源后设置到类内Field的名称
    • source:查询资源所需的业务逻辑编号类内Field的名称
    • type:资源类型,自行定义
    • isArray:接收查询后资源的Field类型是否为array,true:array
    • isList:接收查询后资源的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_IMAGESHORT_IMAGE,并且配置的业务资源编号都是userId字段,这两个字段也就是会传递给ApiBootResourceStoreDelegate#loadResourceUrl方法做为参数。

其中HEAD_IMAGE读取到的资源路径设置到SampleUserInfo类内的headImageSHORT_IMAGE读取到的资源路径设置到SampleUserInfo类内的shortImage字段。

注意:若是你的方法返回对象只有一个资源对象须要映射,能够单独配置使用@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;
}
复制代码

在上面,会为返回值list内的每个SampleUserInfo对象进行设置查询的资源信息。

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类型做为返回值时,其中注意map -> value必须是对象类型。

若是你有想要的使用方式,你就能够提交issuse!!!

相关文章
相关标签/搜索