Skip to content

Commit fe782bc

Browse files
committed
ApiBoot Resource Load 文档.
1 parent 62ec6bd commit fe782bc

File tree

1 file changed

+169
-0
lines changed
  • api-boot-samples/api-boot-sample-resource-load

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
## ApiBoot Resource Load
2+
`ApiBoot Resource Load`是一款资源与业务完全分离的基础框架,可以整合`微服务(Feign、OpenFeign)`进行负载均衡读取固定类型、固定所属业务的资源信息,遵循一定的资源`存储规则`完成`自动化`资源读取、添加、更新、删除、缓存等。
3+
4+
### 使用场景
5+
6+
- 业务图片存储
7+
- 业务音频、视频文件存储
8+
- 业务文件
9+
- 其他资源文件...
10+
11+
### 引入 ApiBoot Resource Load
12+
`pom.xml`配置文件内添加如下依赖:
13+
14+
```xml
15+
<!--ApiBoot Resource Load-->
16+
<dependency>
17+
<groupId>org.minbox.framework</groupId>
18+
<artifactId>api-boot-starter-resource-load</artifactId>
19+
</dependency>
20+
```
21+
> `ApiBoot`所提供的依赖都不需要添加版本号,但是需要添加版本依赖,具体查看[ApiBoot版本依赖](https://github.com/hengboy/api-boot/blob/master/README.md#%E6%B7%BB%E5%8A%A0%E7%89%88%E6%9C%AC%E4%BE%9D%E8%B5%96)
22+
23+
### 了解ApiBootResourceStoreDelegate
24+
25+
`ApiBootResourceStoreDelegate`是一个资源数据读取的委托驱动接口,在使用`ApiBoot Resource Load`时,需要实现该接口完成资源的读取方法`loadResourceUrl()`,该方法的参数如下所示:
26+
27+
1. 第一个参数`sourceFieldValue`,是查询资源的业务编号,具体的配置详见下面的示例。
28+
2. 第二个参数`resourceType`,是查询资源的类型,相同的业务编号下很有可能存在多种类型,比如:用户编号对应用户头像、用户封面等。
29+
30+
**ApiBootResourceStoreDelegate示例:**
31+
32+
```java
33+
// 示例
34+
@Service
35+
public class ResourceLoadService implements ApiBootResourceStoreDelegate {
36+
/**
37+
* logger instance
38+
*/
39+
static Logger logger = LoggerFactory.getLogger(ResourceLoadService.class);
40+
41+
@Override
42+
public List<String> loadResourceUrl(String sourceFieldValue, String resourceType) throws ApiBootException {
43+
logger.info("查询资源的业务逻辑字段值:{}", sourceFieldValue);
44+
logger.info("资源类型:{}", resourceType);
45+
return Arrays.asList(new String[]{"http://test.oss.com/111.png"});
46+
}
47+
}
48+
```
49+
50+
> `loadResourceUrl`方法需要返回根据`resourceFieldValue``resourceType`字段查询到的资源列表。
51+
52+
53+
54+
### 内置注解
55+
56+
57+
- `@ResourceLoad`
58+
59+
标注方法需要进行`ApiBoot Resource Load`自动化读取资源信息,该注解必须添加,且只能添加在方法上。
60+
61+
- `@ResourceFields`
62+
63+
`@ResourceField`注解的集合
64+
65+
- `@ResourceField`
66+
67+
配置`@ResourceLoad`标注的方法具体有哪些字段需要进行资源的自动映射,参数解释如下所示:
68+
69+
- `name`:查询资源后设置到类内`Field`的名称
70+
- `source`:查询资源所需的业务逻辑编号类内`Field`的名称
71+
- `type`:资源类型,自行定义
72+
- `isArray`:接收查询后资源的`Field`类型是否为`array`,true:array
73+
- `isList`:接收查询后资源的`Field`类型是否为`list`,true:list
74+
75+
### 单对象资源加载
76+
77+
资源加载一般都是实体类的方式进行返回的,下面我们先来创建一个实体类方便示例测试,如下所示:
78+
79+
```java
80+
/**
81+
* 示例对象
82+
*/
83+
@Data
84+
class SampleUserInfo {
85+
public SampleUserInfo(String userId, int age) {
86+
this.userId = userId;
87+
this.age = age;
88+
}
89+
90+
private String userId;
91+
private String headImage;
92+
private String shortImage;
93+
private int age;
94+
}
95+
```
96+
97+
**返回值为单对象资源加载示例:**
98+
99+
```java
100+
/**
101+
* 返回值为单个对象的示例
102+
*
103+
* @return
104+
*/
105+
@ResourceLoad
106+
@ResourceFields({
107+
@ResourceField(name = "headImage", source = "userId", type = "HEAD_IMAGE"),
108+
@ResourceField(name = "shortImage", source = "userId", type = "SHORT_IMAGE")
109+
})
110+
public SampleUserInfo singleObjectSample() {
111+
return new SampleUserInfo("yuqiyu", 24);
112+
}
113+
```
114+
115+
> 在上面,我们配置读取两种类型的资源,分别是:`HEAD_IMAGE``SHORT_IMAGE`,而且配置的业务资源编号都是`userId`字段,这两个字段也就是会传递给`ApiBootResourceStoreDelegate#loadResourceUrl`方法作为参数。
116+
>
117+
> 其中`HEAD_IMAGE`读取到的资源路径设置到`SampleUserInfo`类内的`headImage``SHORT_IMAGE`读取到的资源路径设置到`SampleUserInfo`类内的`shortImage`字段。
118+
119+
> 注意:如果你的方法返回对象只有一个资源对象需要映射,可以单独配置使用`@ResourceField`注解。
120+
121+
### List集合资源加载
122+
123+
```java
124+
/**
125+
* 返回值为list集合的示例
126+
*
127+
* @return
128+
*/
129+
@ResourceLoad
130+
@ResourceFields({
131+
@ResourceField(name = "headImage", source = "userId", type = "HEAD_IMAGE"),
132+
@ResourceField(name = "shortImage", source = "userId", type = "SHORT_IMAGE")
133+
})
134+
public List<SampleUserInfo> listSample() {
135+
List<SampleUserInfo> users = new ArrayList();
136+
users.add(new SampleUserInfo("yuqiyu", 24));
137+
users.add(new SampleUserInfo("hengboy", 24));
138+
return users;
139+
}
140+
```
141+
142+
在上面,会为返回值`list`内的每一个`SampleUserInfo`对象进行设置查询的资源信息。
143+
144+
### Map集合资源加载
145+
146+
```java
147+
/**
148+
* 返回值为map集合的示例
149+
*
150+
* @return
151+
*/
152+
@ResourceLoad
153+
@ResourceFields({
154+
@ResourceField(name = "headImage", source = "userId", type = "HEAD_IMAGE"),
155+
@ResourceField(name = "shortImage", source = "userId", type = "SHORT_IMAGE")
156+
})
157+
public Map<String, SampleUserInfo> mapSample() {
158+
Map<String, SampleUserInfo> users = new HashMap<>(2);
159+
users.put("yuqiyu", new SampleUserInfo("yuqiyu", 24));
160+
users.put("hengboy", new SampleUserInfo("hengboy", 24));
161+
return users;
162+
}
163+
```
164+
165+
`Map`类型作为返回值时,其中注意`map -> value`必须是对象类型。
166+
167+
168+
169+
> 如果你有想要的使用方式,你就可以提交issuse!!!

0 commit comments

Comments
 (0)