In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "what is the method to let API call in parallel". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
When the amount of data is large, it will be split by sub-database and table to share the pressure of reading and writing. After the sub-database sub-table is more troublesome is the query problem, if not directly according to the fragment key to query, it is necessary to query multiple tables.
In some complex business scenarios, such as order search, in addition to the order number, users, merchants and other commonly used search conditions, there may be time, products and so on.
At present, the common practice is to synchronize the data to the search framework such as ES for query, and then through the search results, usually the primary key ID, and then go to the specific data table to query the complete data, assemble and return to the caller.
For example, the following code first queries out the article information, and then queries the user's nickname according to the user's ID in the article.
List articleBos = articleDoPage.getRecords () .stream () .map (r-> {String nickname = userManager.getNickname (r.getUserId ()); return articleBoConvert.convertPlus (r, nickname);}) .subscription (Collectors.toList ())
If the article has 10 pieces of data, then the interface provided by the user service needs to be invoked 10 times, and the operation is invoked synchronously.
Of course, we can also use parallel flow to implement concurrent calls, the code is as follows:
List articleBos = articleDoPage.getRecords () .parallelStream () .map (r-> {String nickname = userManager.getNickname (r.getUserId ()); return articleBoConvert.convertPlus (r, nickname);}) .subscription (Collectors.toList ())
The advantage of parallel flow is obvious, and the code does not need to be changed much. It is important to note that if parallel flows are used, it is best to define a separate ForkJoinPool.
In addition to using parallel flows, you can also use batch queries to improve performance and reduce the number of RPC calls, as shown below:
List userIds = articleDoPage.getRecords (). Stream (). Map (article-> article.getUserId ()) .userManager.queryByIds (Collectors.toList ()); Map nickNameMap = userManager.queryByIds (userIds). Stream (). Collect (Collectors.toMap (UserResponse::getId, UserResponse::getNickname)); List articleBos = articleDoPage.getRecords (). Stream (). Map (r-> {String nickname = nickNameMap.containsKey (r.getUserId ())? NickNameMap.get (r.getUserId ()): CommonConstant.DEFAULT_EMPTY_STR; return articleBoConvert.convertPlus (r, nickname);}) .requests (Collectors.toList ())
However, batch query is still in synchronous mode. If CompletableFuture is used to implement asynchronous and concurrent calls, native CompletableFuture can be used directly, but the orchestration ability is not so strong. Here we choose a parallel orchestration box based on CompletableFuture encapsulation to implement.
A little encapsulation is done to provide a more convenient tool class to implement the logic of calling multiple interfaces concurrently.
The first method is suitable for, for example, checking out a batch of ID from ES, then going to the database according to ID or calling RPC to query real data, and finally getting a Map, and you can get the corresponding data according to Key.
Internally, there are multithreaded concurrent calls that wait until all the results are returned.
Public Object aggregationApi () {long s = System.currentTimeMillis (); List ids = new ArrayList (); ids.add ("1"); ids.add ("2"); ids.add ("3"); Map callResult = AsyncTemplate.call (ids, id-> {return userService.getUser (id);}, u-> u.getId (), COMMON_POOL); long e = System.currentTimeMillis () System.out.println ("time consuming:" + (emurs) + "ms"); return ";}
Another scenario is API aggregation, where multiple interfaces need to be called in parallel to assemble the results.
List params = new ArrayList (); AsyncCall goodsQuery = new AsyncCall ("goodsQuery", 1); params.add (goodsQuery); AsyncCall orderQuery = new AsyncCall ("orderQuery", "100"); params.add (orderQuery); UserQuery Q = new UserQuery (); q.setAge (18); q.setName ("yinjihuan"); AsyncCall userQuery = new AsyncCall ("userQuery", Q); params.add (userQuery) AsyncTemplate.call (params, p-> {if (p.getTaskId (). Equals ("goodsQuery")) {AsyncCall query = p; return goodsService.getGoodsName (query.getParam ());} if (p.getTaskId (). Equals ("orderQuery")) {AsyncCall query = p; return orderService.getOrder (query.getParam ()) } if (p.getTaskId () .equals ("userQuery")) {AsyncCall query = p; return userService.getUser (query.getParam ());} return null;})
The parameters and response types are defined in AsyncCall, and the response results are automatically set to AsyncCall after execution. In the call method, you need to do the corresponding processing logic according to taskId, and different taskId calls have different interfaces.
This is the end of the content of "what is the method to let API call in parallel". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.