项目地址和示例代码: https://github.com/lvyahui8/spring-boot-data-aggregator
背景
接口开发是后端开发中最常见的场景, 可能是RESTFul接口, 也可能是RPC接口. 接口开发往往是从各处捞出数据, 然后组装成结果, 特别是那些偏业务的接口.
如何方便快速的开发高性能的接口, 是一个必须思考的问题.
例如, 我现在需要实现一个接口, 拉取用户基础信息
+用户的博客列表
+用户的粉丝数据
的整合数据, 假设已经有如下三个接口可以使用, 分别用来获取 用户基础信息
,用户博客列表
, 用户的粉丝数据
.
用户基础信息
@Service
public class UserServiceImpl implements UserService {
@Override
public User get(Long id) {
try {
Thread.sleep(1000L);} catch (InterruptedException e) {
}
/* mock a user*/
User user = new User();
user.setId(id);
user.setEmail("lvyahui8@gmail.com");
user.setUsername("lvyahui8");
return user;
}
}
用户博客列表
@Service
public class PostServiceImpl implements PostService {
@Override
public List<Post> getPosts(Long userId) {
try {
Thread.sleep(1000L); } catch (InterruptedException e) {
}
Post post = new Post();
post.setTitle("spring data aggregate example");
post.setContent("No active profile set, falling back to default profiles");
return Collections.singletonList(post);
}
}
用户的粉丝数据
@Service
public class FollowServiceImpl implements FollowService {
@Override
public List<User> getFollowers