項目地址和示例代碼: 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(Long userId) {
try { Thread.sleep(1000L); } catch (InterruptedException e) {}
int size = 10;
List<User> users = new ArrayList<>(size);
for(int i = 0 ; i < size; i++) {
User user = new User();
user.setUsername("name"+i);
user.setEmail("email"+i+"@fox.com");
user.setId((long) i);
users.add(user);
};
return users;
}
}
注意, 每一個方法都sleep了1s以模擬業務耗時.
我們需要再封裝一個接口, 來拼裝以上三個接口的數據.
PS: 這樣的場景實際在工作中很常見, 而且往往我們需要拼湊的數據, 是要走網絡請求調到第三方去的. 另外可能有人會想, 為何不分成3個請求? 實際為了客戶端網絡性能考慮, 往往會在一次網絡請求中, 盡可能多的傳輸數據, 當然前提是這個數據不能太大, 否則傳輸的耗時會影響渲染. 許多APP的首頁, 看着復雜, 實際也只有一個接口, 一次性拉下所有數據, 客戶端開發也簡單.
串行實現
編寫性能優良的接口不僅是每一位后端程序員的技術追求, 也是業務的基本訴求. 一般情況下, 為了保證更好的性能, 往往需要編寫更復雜的代碼實現.
但凡人皆有惰性, 因此, 往往我們會像下面這樣編寫串行調用的代碼
@Component
public class UserQueryFacade {
@Autowired
private FollowService followService;
@Autowired
private PostService postService;
@Autowired
private UserService userService;
public User getUserData(Long userId) {
User user = userService.get(userId);
user.setPosts(postService.getPosts(userId));
user.setFollowers(followService.getFollowers(userId));
return user;
}
}
很明顯, 上面的代碼, 效率低下, 起碼要3s才能拿到結果, 且一旦用到某個接口的數據, 便需要注入相應的service, 復用麻煩.
並行實現
有追求的程序員可能立馬會考慮到, 這幾項數據之間並無強依賴性, 完全可以並行獲取嘛, 通過異步線程+CountDownLatch+Future實現, 就像下面這樣.
@Component
public class UserQueryFacade {
@Autowired
private FollowService followService;
@Autowired
private PostService postService;
@Autowired
private UserService userService;
public User getUserDataByParallel(Long userId) throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newFixedThreadPool(3);
CountDownLatch countDownLatch = new CountDownLatch(3);
Future<User> userFuture = executorService.submit(() -> {
try{
return userService.get(userId);
}finally {
countDownLatch.countDown();
}
});
Future<List<Post>> postsFuture = executorService.submit(() -> {
try{
return postService.getPosts(userId);
}finally {
countDownLatch.countDown();
}
});
Future<List<User>> followersFuture = executorService.submit(() -> {
try{
return followService.getFollowers(userId);
}finally {
countDownLatch.countDown();
}
});
countDownLatch.await();
User user = userFuture.get();
user.setFollowers(followersFuture.get());
user.setPosts(postsFuture.get());
return user;
}
}
上面的代碼, 將串行調用改為並行調用, 在有限並發級別下, 能極大提高性能. 但很明顯, 它過於復雜, 如果每個接口都為了並行執行都寫這樣一段代碼, 簡直是噩夢.
優雅的注解實現
熟悉java的都知道, java有一種非常便利的特性 ~~ 注解. 簡直是黑魔法. 往往只需要給類或者方法上添加一些注解, 便可以實現非常復雜的功能.
有了注解, 再結合Spring依賴自動注入的思想, 那么我們可不可以通過注解的方式, 自動注入依賴, 自動並行調用接口呢? 答案是肯定的.
首先, 我們先定義一個聚合接口
@Component
public class UserAggregate {
@DataProvider(id="userFullData")
public User userFullData(@DataConsumer(id = "user") User user,
@DataConsumer(id = "posts") List<Post> posts,
@DataConsumer(id = "followers") List<User> followers) {
user.setFollowers(followers);
user.setPosts(posts);
return user;
}
}
其中
-
@DataProvider
表示這個方法是一個數據提供者, 數據Id為userFullData
-
@DataConsumer
表示這個方法的參數, 需要消費數據, 數據Id為user
,posts
,followers
.
當然, 原來的3個原子服務 用戶基礎信息
,用戶博客列表
, 用戶的粉絲數據
, 也分別需要添加一些注解
@Service
public class UserServiceImpl implements UserService {
@DataProvider(id = "user")
@Override
public User get(@InvokeParameter("userId") Long id) {
@Service
public class PostServiceImpl implements PostService {
@DataProvider(id = "posts")
@Override
public List<Post> getPosts(@InvokeParameter("userId") Long userId) {
@Service
public class FollowServiceImpl implements FollowService {
@DataProvider(id = "followers")
@Override
public List<User> getFollowers(@InvokeParameter("userId") Long userId) {
其中
@DataProvider
與前面的含義相同, 表示這個方法是一個數據提供者@InvokeParameter
表示方法執行時, 需要手動傳入的參數
這里注意 @InvokeParameter
和 @DataConsumer
的區別, 前者需要用戶在最上層調用時手動傳參; 而后者, 是由框架自動分析依賴, 並異步調用取得結果之后注入的.
最后, 僅僅只需要調用一個統一的門面(Facade)接口, 傳遞數據Id, Invoke Parameters,以及返回值類型. 剩下的並行處理, 依賴分析和注入, 完全由框架自動處理.
@Component
public class UserQueryFacade {
@Autowired
private DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade;
public User getUserFinal(Long userId) throws InterruptedException,
IllegalAccessException, InvocationTargetException {
return dataBeanAggregateQueryFacade.get("userFullData",
Collections.singletonMap("userId", userId), User.class);
}
}
如何用在你的項目中
上面的功能, 筆者已經封裝為一個spring boot starter, 並發布到maven中央倉庫.
只需在你的項目引入依賴.
<dependency>
<groupId>io.github.lvyahui8</groupId>
<artifactId>spring-boot-data-aggregator-starter</artifactId>
<version>1.0.1</version>
</dependency>
並在 application.properties
文件中聲明注解的掃描路徑.
# 替換成你需要掃描注解的包
io.github.lvyahui8.spring.base-packages=io.github.lvyahui8.spring.example
之后, 就可以使用如下注解和 Spring Bean 實現聚合查詢
@DataProvider
@DataConsumer
@InvokeParameter
- Spring Bean
DataBeanAggregateQueryFacade
注意, @DataConsumer
和 @InvokeParameter
可以混合使用, 可以用在同一個方法的不同參數上. 且方法的所有參數必須有其中一個注解, 不能有沒有注解的參數.
項目地址和上述示例代碼: https://github.com/lvyahui8/spring-boot-data-aggregator
后期計划
后續筆者將繼續完善異常處理, 超時邏輯, 解決命名沖突的問題, 並進一步提高插件的易用性, 高可用性, 擴展性